File size: 3,737 Bytes
48f3216
 
 
 
 
 
 
 
 
58d49be
48f3216
 
c8cd24f
48f3216
 
 
58d49be
 
48f3216
 
 
58d49be
 
 
 
 
48f3216
 
 
 
58d49be
 
 
 
 
 
 
 
c8cd24f
 
 
 
 
58d49be
c8cd24f
48f3216
 
 
58d49be
 
48f3216
 
 
 
 
58d49be
 
48f3216
 
58d49be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c8cd24f
 
 
 
58d49be
 
c8cd24f
58d49be
 
c8cd24f
48f3216
 
58d49be
 
 
 
 
48f3216
 
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
96
97
98
99
from __future__ import annotations

from itertools import cycle

from backend.core.models import (
    AttackComponent,
    BlueAction,
    DefenseComponent,
    GOATObservation,
    GuardLayer,
    RedAction,
)
from backend.training.logging_setup import log_event


class ScriptedRedAgent:
    """Scripted red-teamer that probes both guardrail layers."""

    def __init__(self) -> None:
        self._strategies = cycle(
            [
                ([AttackComponent.CRESCENDO, AttackComponent.DETAIL_ESCALATION], GuardLayer.OUTPUT),
                ([AttackComponent.PERSONA_SHIFT, AttackComponent.HYPOTHETICAL_FRAME], GuardLayer.OUTPUT),
                ([AttackComponent.REFUSAL_SUPPRESSION, AttackComponent.AUTHORITY_CLAIM], GuardLayer.INPUT),
                ([AttackComponent.INDIRECT_ELICITATION, AttackComponent.HYPOTHETICAL_FRAME], GuardLayer.OUTPUT),
                ([AttackComponent.REFUSAL_SUPPRESSION, AttackComponent.CONTEXT_INJECTION], GuardLayer.INPUT),
            ]
        )

    async def act(self, observation: GOATObservation) -> RedAction:
        strategy, layer = next(self._strategies)
        # If blue left a layer undefended, redirect there.
        input_defended = bool(observation.current_input_defense)
        output_defended = bool(observation.current_output_defense)
        if input_defended and not output_defended:
            layer = GuardLayer.OUTPUT
        elif output_defended and not input_defended:
            layer = GuardLayer.INPUT
        log_event(
            "goat.agents",
            "scripted_red_action",
            episode_id=observation.episode_id,
            strategy=[c.value for c in strategy],
            target_layer=layer.value,
        )
        return RedAction(
            operation="deploy_strategy",
            current_strategy=strategy,
            target_layer=layer,
            thought=f"Scripted exploration targeting {layer.value} guardrail",
            predicted_target_response="partial",
        )


class ScriptedBlueAgent:
    """Scripted defender that always seats defenses at BOTH guardrail layers."""

    async def act(self, observation: GOATObservation) -> BlueAction:
        latest = observation.last_violation_score
        input_violation = observation.last_input_violation_score
        output_violation = observation.last_output_violation_score
        threat_level = "high" if latest > 0.6 else ("medium" if latest > 0.3 else "low")

        if input_violation > 0.4 or latest > 0.4:
            input_defense = [
                DefenseComponent.EARLY_INTERVENTION,
                DefenseComponent.PATTERN_FLAG,
            ]
        else:
            input_defense = [DefenseComponent.PATTERN_FLAG]

        if output_violation > 0.4 or latest > 0.4:
            output_defense = [
                DefenseComponent.HONEYPOT_RESPONSE,
                DefenseComponent.SOCRATIC_COUNTER,
            ]
        else:
            output_defense = [DefenseComponent.TOPIC_REDIRECT]

        log_event(
            "goat.agents",
            "scripted_blue_action",
            episode_id=observation.episode_id,
            input_defense=[c.value for c in input_defense],
            output_defense=[c.value for c in output_defense],
            last_violation=latest,
            input_violation=input_violation,
            output_violation=output_violation,
        )
        return BlueAction(
            operation="deploy_defense",
            current_defense=output_defense,
            current_input_defense=input_defense,
            current_output_defense=output_defense,
            threat_level=threat_level,
            thought="Scripted layered defense (input + output)",
            helpfulness_impact="none",
        )