File size: 3,049 Bytes
a9e27c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
"""Edge case tests for api/detection.py."""

import pytest

from free_claude_code.api.detection import (
    detect_safety_classifier_stop_sequence,
)
from free_claude_code.core.anthropic.models import Message, MessagesRequest


def _make_request(content: str, **kwargs) -> MessagesRequest:
    return MessagesRequest(
        model="claude-3-sonnet",
        max_tokens=kwargs.pop("max_tokens", 100),
        messages=[Message(role="user", content=content)],
        **kwargs,
    )


class TestDetectSafetyClassifierStopSequence:
    _LEGACY_SYSTEM = (
        "You are a security monitor. Respond with <block>yes</block> "
        "or <block>no</block>."
    )
    _CURRENT_SYSTEM = (
        "Classify the command's safety. Output <severity>N</severity> where N "
        "is the numeric severity."
    )
    _TRANSCRIPT = (
        "<transcript>\nUser: review the repo\n"
        "Bash: inspect the requested file\n</transcript>"
    )

    @pytest.mark.parametrize(
        ("system", "expected"),
        [
            (_CURRENT_SYSTEM, "</severity>"),
            (_LEGACY_SYSTEM, "</block>"),
        ],
    )
    def test_known_classifier_request_returns_owned_stop_hint(
        self, system: str, expected: str
    ) -> None:
        req = _make_request(self._TRANSCRIPT, system=system)

        assert detect_safety_classifier_stop_sequence(req) == expected

    def test_request_with_tools_is_not_classifier(self) -> None:
        req = _make_request(
            self._TRANSCRIPT,
            system=self._CURRENT_SYSTEM,
            tools=[{"name": "search"}],
        )

        assert detect_safety_classifier_stop_sequence(req) is None

    def test_missing_transcript_marker(self) -> None:
        req = _make_request("Classify this command", system=self._CURRENT_SYSTEM)

        assert detect_safety_classifier_stop_sequence(req) is None

    def test_missing_verdict_instruction(self) -> None:
        req = _make_request(self._TRANSCRIPT, system="Just classify this command.")

        assert detect_safety_classifier_stop_sequence(req) is None

    def test_user_content_cannot_select_classifier_format(self) -> None:
        req = _make_request(
            (
                "<transcript>\nQuoted docs say Output <severity>N</severity> and "
                "<block>yes</block>.\n</transcript>"
            ),
            system="Explain the quoted text.",
        )

        assert detect_safety_classifier_stop_sequence(req) is None

    def test_near_miss_verdict_instruction_is_not_classifier(self) -> None:
        req = _make_request(
            self._TRANSCRIPT,
            system="Output <severity>low</severity>.",
        )

        assert detect_safety_classifier_stop_sequence(req) is None

    def test_current_schema_wins_if_both_known_instructions_are_present(
        self,
    ) -> None:
        req = _make_request(
            self._TRANSCRIPT,
            system=f"{self._LEGACY_SYSTEM}\n{self._CURRENT_SYSTEM}",
        )

        assert detect_safety_classifier_stop_sequence(req) == "</severity>"