项目文件夹

文件
Ali Khokhar f0b31065ee Preserve mid-conversation system messages through provider conversion (#1125)
## Problem

FCC hoisted inline Anthropic `system` messages into the top-level system
prompt during request validation. Mid-conversation system messages are
position-sensitive, so this applied later instructions retroactively,
changed the existing prompt/cache prefix, and prevented provider
conversion from seeing the original transcript.

## Changes

- Preserve inline `system` messages, content, metadata, and ordering in
Messages and token-count requests while keeping the top-level system
prompt distinct.
- Convert text-only inline system messages to OpenAI Chat `system`
messages at the same transcript position; reject unrepresentable inline
blocks before streaming instead of silently dropping them.
- Remove the lossy normalization path and its unused role enum, and
document protocol-model versus target-conversion ownership in
`ARCHITECTURE.md`.
- Cover API routing, model serialization, cache-prefix stability, text
blocks, tool-result ordering, invalid content, and token counting; bump
the package to `4.6.2`.
- Verify all five local CI checks (2,287 tests) and the ordered
transcript against NVIDIA NIM, OpenRouter, Gemini, DeepSeek, Mistral,
and Hugging Face.

<!-- greptile_comment -->

<details open><summary><h3>Greptile Summary</h3></summary>

This PR preserves inline Anthropic system messages through provider
conversion. The main changes are:

- Keeps top-level and inline system content separate and ordered.
- Converts text-only inline system messages without moving them.
- Rejects system blocks that OpenAI Chat cannot represent safely.
- Updates request detection to ignore system context when counting user
turns.
- Adds serialization, routing, token-counting, and conversion coverage.
- Updates the package version and architecture documentation.
</details>

<h3>Confidence Score: 5/5</h3>

This looks safe to merge.

The leading-system detection path ignores system entries when counting
user turns. Inline system content remains ordered for provider
conversion. Unsupported system blocks fail explicitly instead of being
dropped. No blocking issues were found in the changed code.

No files require attention.

<details><summary><h3><a href="https://www.greptile.com/trex"><img
alt="T-Rex"
src="https://greptile-static-assets.s3.amazonaws.com/trex/trex_green.svg"
height="20" align="absmiddle"></a> T-Rex Logs</h3></summary>

**What T-Rex did**
- Validated that the transcript roles now follow the order user,
assistant, system, user and that the top-level prompt remains separate.
- Verified that inline system content is no longer counted in message
tokens and that cache\_control metadata survives parsing.
- Confirmed that the converted OpenAI transcript preserves position and
cache prefix.
- Observed that a system message following a tool result is converted as
assistant, tool, system.
- Ran the focused pytest and confirmed 209 passed in 2.82s with exit
code 0.

<a
href="https://app.greptile.com/trex/runs/14526513/artifacts"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://greptile-static-assets.s3.amazonaws.com/badges/ViewAllArtifactsDark.svg?v=4"><source
media="(prefers-color-scheme: light)"
srcset="https://greptile-static-assets.s3.amazonaws.com/badges/ViewAllArtifacts.svg?v=4"><img
alt="View all artifacts"
src="https://greptile-static-assets.s3.amazonaws.com/badges/ViewAllArtifacts.svg?v=4"></picture></a>

<sub><a href="https://www.greptile.com/trex"><img alt="T-Rex"
src="https://greptile-static-assets.s3.amazonaws.com/trex/trex_green.svg"
height="14" align="absmiddle"></a> Ran code and verified through
T-Rex</sub>
</details>

<details open><summary><h3>Important Files Changed</h3></summary>

| Filename | Overview |
|----------|----------|
| src/free_claude_code/core/anthropic/models.py | Preserves system-role
messages in the original transcript instead of hoisting them into the
top-level prompt. |
| src/free_claude_code/core/anthropic/conversion.py | Converts ordered
text-only system messages and rejects unsupported system content before
streaming. |
| src/free_claude_code/api/detection.py | Builds a read-only semantic
view of system context and conversational user turns for local request
detection. |

</details>

<sub>Reviews (2): Last reviewed commit: ["Restore optimizations with
inline
system..."](https://github.com/alishahryar1/free-claude-code/commit/6605ede7f604053381552106489dbd16bcd37987)
| [Re-trigger
Greptile](https://app.greptile.com/api/retrigger?id=44423885)</sub>

<!-- /greptile_comment -->
2026-07-15 03:37:11 -07:00

167 行
6.0 KiB
Python

"""Edge case tests for api/detection.py."""
from unittest.mock import patch
from free_claude_code.api.detection import (
is_filepath_extraction_request,
is_prefix_detection_request,
is_quota_check_request,
is_safety_classifier_request,
is_title_generation_request,
)
from free_claude_code.core.anthropic.models import Message, MessagesRequest
def _make_request(
content: str, *, inline_system: str | None = None, **kwargs
) -> MessagesRequest:
messages = []
if inline_system is not None:
messages.append(Message(role="system", content=inline_system))
messages.append(Message(role="user", content=content))
return MessagesRequest(
model="claude-3-sonnet",
max_tokens=kwargs.pop("max_tokens", 100),
messages=messages,
**kwargs,
)
def test_quota_detection_ignores_inline_system_context() -> None:
request = _make_request(
"Check my quota", inline_system="Current request context", max_tokens=1
)
assert is_quota_check_request(request) is True
def test_title_detection_reads_inline_system_context() -> None:
request = _make_request(
"Summarize this session",
inline_system=(
"Generate a concise, sentence-case title for this coding session. "
'Return JSON with a single "title" field.'
),
)
assert is_title_generation_request(request) is True
class TestIsPrefixDetectionRequest:
def test_inline_system_context_does_not_hide_single_user_turn(self):
req = _make_request(
"<policy_spec> Command: git status",
inline_system="Current request context",
)
assert is_prefix_detection_request(req) == (True, "git status")
def test_output_marker_handling(self):
"""Content with Command: but Output: after cmd_start; output has < or \\n\\n."""
content = "<policy_spec> Command:\nls -la\nOutput:\na.txt\nb.txt\n\nmore"
req = _make_request(content)
is_req, cmd = is_prefix_detection_request(req)
assert is_req is True
assert "ls -la" in cmd
def test_prefix_detection_with_empty_command_section(self):
"""Command: at end with no content returns empty command."""
req = _make_request("<policy_spec> Command: ")
is_req, cmd = is_prefix_detection_request(req)
assert is_req is True
assert cmd == ""
def test_exception_in_try_returns_false(self):
"""Exception in try block (e.g. content slice) returns False, ''."""
req = _make_request("<policy_spec> Command: x")
# Return object that raises when sliced - triggers except in is_prefix_detection_request
class BadStr(str):
def __getitem__(self, key):
raise TypeError("bad slice")
with patch(
"free_claude_code.api.detection.extract_text_from_content",
return_value=BadStr("<policy_spec> Command: x"),
):
is_req, cmd = is_prefix_detection_request(req)
assert is_req is False
assert cmd == ""
class TestIsSafetyClassifierRequest:
_SYSTEM = (
"You are a security monitor. Respond with <block>yes</block> "
"or <block>no</block>."
)
_USER = (
"<transcript>\nUser: review the repo\n"
"WebFetch https://example.com: fetch\n</transcript>\n<block> immediately."
)
def test_classifier_request_detected(self):
req = _make_request(self._USER, system=self._SYSTEM)
assert is_safety_classifier_request(req) is True
def test_markers_split_across_system_and_user(self):
req = _make_request(
"<transcript>\nWebFetch x\n</transcript>", system=self._SYSTEM
)
assert is_safety_classifier_request(req) is True
def test_request_with_tools_is_not_classifier(self):
req = _make_request(self._USER, system=self._SYSTEM, tools=[{"name": "search"}])
assert is_safety_classifier_request(req) is False
def test_missing_transcript_marker(self):
req = _make_request("<block> immediately", system=self._SYSTEM)
assert is_safety_classifier_request(req) is False
def test_missing_verdict_instruction(self):
req = _make_request(
"<transcript>\nWebFetch x\n</transcript>", system="just chatting"
)
assert is_safety_classifier_request(req) is False
def test_xml_content_without_verdict_instruction(self):
req = _make_request(
"Explain this format: <transcript> ... </transcript> and a <block> tag."
)
assert is_safety_classifier_request(req) is False
class TestIsFilepathExtractionRequest:
def test_inline_system_context_preserves_filepath_detection(self):
req = _make_request(
"Command:\nls\nOutput:\na.txt",
inline_system="Extract any file paths that this command reads or modifies.",
)
assert is_filepath_extraction_request(req) == (True, "ls", "a.txt")
def test_output_marker_minus_one_returns_false(self):
"""Output: not found after Command: returns False."""
content = "Command:\nls\nfilepaths"
req = _make_request(content)
is_fp, cmd, out = is_filepath_extraction_request(req)
assert is_fp is False
assert cmd == ""
assert out == ""
def test_output_has_angle_bracket_splits(self):
"""Output containing < is split and first part used."""
content = "Command:\nls\nOutput:\na.txt b.txt <extra>\nfilepaths"
req = _make_request(content)
is_fp, _cmd, out = is_filepath_extraction_request(req)
assert is_fp is True
assert "<" not in out
assert out == "a.txt b.txt"
def test_output_has_double_newline_splits(self):
"""Output containing \\n\\n is split and first part used."""
content = "Command:\nls\nOutput:\na.txt\nb.txt\n\nmore text\nfilepaths"
req = _make_request(content)
is_fp, _cmd, out = is_filepath_extraction_request(req)
assert is_fp is True
assert "more" not in out