File size: 7,282 Bytes
b308dc2
 
74221d3
b308dc2
74221d3
b308dc2
 
 
 
 
 
 
 
 
 
 
 
74221d3
b308dc2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74221d3
 
 
b308dc2
b308f46
 
 
 
 
 
 
 
 
 
b308dc2
 
74221d3
b308dc2
 
 
 
 
 
 
 
 
 
 
 
74221d3
b308dc2
b308f46
b308dc2
b308f46
 
74221d3
b308f46
 
 
 
 
 
 
 
 
 
 
 
74221d3
b308f46
 
 
 
 
 
 
74221d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad0932c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b308f46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74221d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
"""Prompt manager tests."""

from pycatan.ai.config import AIConfig, HEBREW_RESOURCE_TERMS_INSTRUCTION
from pycatan.ai.prompt_manager import PromptManager
from pycatan.ai.schemas import ResponseType, SchemaVersion, get_schema_for_response_type


def _game_state(*names):
    return {
        "meta": {"curr": names[0] if names else None, "phase": "NORMAL_PLAY"},
        "H": [],
        "N": [],
        "state": {"bld": [], "rds": []},
        "players": {name: {"vp": 0, "res": {}} for name in names},
    }


def test_three_player_relationship_context_is_coordinated():
    manager = PromptManager()
    state = _game_state("Hadar", "Shon", "Ziv")

    hadar_prompt = manager.create_prompt(
        player_num=0,
        player_name="Hadar",
        player_color="Red",
        game_state=state,
        what_happened="Game start",
        available_actions=[],
    )
    shon_prompt = manager.create_prompt(
        player_num=1,
        player_name="Shon",
        player_color="Blue",
        game_state=state,
        what_happened="Game start",
        available_actions=[],
    )
    ziv_prompt = manager.create_prompt(
        player_num=2,
        player_name="Ziv",
        player_color="White",
        game_state=state,
        what_happened="Game start",
        available_actions=[],
    )

    hadar_background = hadar_prompt["meta_data"]["relationship_context"]
    shon_background = shon_prompt["meta_data"]["relationship_context"]
    ziv_background = ziv_prompt["meta_data"]["relationship_context"]

    for background in [hadar_background, shon_background, ziv_background]:
        assert "Hadar" in background
        assert "Shon" in background
        assert "Ziv" in background
        assert "Ziv warned Hadar" in background
        assert "side trade from Shon" in background

    assert "Shon hurt your trust" in hadar_background
    assert "Hadar has reason to distrust you" in shon_background
    assert "you warned Hadar about Shon" in ziv_background


def test_relationship_context_has_fallback_for_two_players():
    manager = PromptManager()
    state = _game_state("Alice", "Bob")

    prompt = manager.create_prompt(
        player_num=0,
        player_name="Alice",
        player_color="Red",
        game_state=state,
        what_happened="Game start",
        available_actions=[],
    )

    background = prompt["meta_data"]["relationship_context"]
    assert "Bob" in background
    assert "Alice" in background
    assert "strong legal Catan play" in background


def test_relationship_context_includes_all_four_players():
    manager = PromptManager()
    state = _game_state("Alice", "Bob", "Charlie", "Diana")

    prompt = manager.create_prompt(
        player_num=3,
        player_name="Diana",
        player_color="Orange",
        game_state=state,
        what_happened="Game start",
        available_actions=[],
    )

    background = prompt["meta_data"]["relationship_context"]
    assert "Alice" in background
    assert "Bob" in background
    assert "Charlie" in background
    assert "Diana" in background
    assert "robber pressure" in background


def test_relationship_context_includes_recent_updates():
    manager = PromptManager()
    state = _game_state("Alice", "Bob", "Charlie")

    prompt = manager.create_prompt(
        player_num=0,
        player_name="Alice",
        player_color="Red",
        game_state=state,
        what_happened="Game start",
        available_actions=[],
        relationship_updates=[
            {"note": "Bob mocked my blocked ore and refused a fair trade."},
        ],
    )

    context = prompt["meta_data"]["relationship_context"]
    assert "Recent relationship shifts" in context
    assert "Bob mocked my blocked ore" in context


def test_prompt_includes_five_point_game_context():
    manager = PromptManager()
    state = _game_state("Hadar", "Shon")
    state["meta"]["vp_to_win"] = 5

    prompt = manager.create_prompt(
        player_num=0,
        player_name="Hadar",
        player_color="Red",
        game_state=state,
        what_happened="Game start",
        available_actions=[],
    )

    assert prompt["meta_data"]["game_context"] == (
        "CONTEXT: You are playing Catan to 5 victory points. "
        "The first player to reach 5 VP wins."
    )


def test_trade_context_summarizes_resolved_trades_and_keeps_open_trades_structured():
    manager = PromptManager()
    state = _game_state("Shon", "Ziv")

    prompt = manager.create_prompt(
        player_num=0,
        player_name="Shon",
        player_color="Blue",
        game_state=state,
        what_happened="Ziv is deciding.",
        available_actions=[],
        pending_trades=[
            {
                "trade_id": "trade_1",
                "from": "Shon",
                "to": "Ziv",
                "offer": {"wheat": 1},
                "request": {"brick": 1},
                "status": "rejected",
                "timestamp": 123.0,
                "responded_by": "Ziv",
                "resolved_at": 124.0,
            },
            {
                "trade_id": "trade_2",
                "from": "Ziv",
                "to": "Shon",
                "offer": {"sheep": 1},
                "request": {"wood": 1},
                "status": "accepted",
                "timestamp": 125.0,
                "responded_by": "Shon",
                "resolved_at": 126.0,
            },
            {
                "trade_id": "trade_3",
                "from": "Ziv",
                "to": "Shon",
                "offer": {"ore": 1},
                "request": {"wheat": 1},
                "status": "pending",
                "timestamp": 127.0,
            },
        ],
    )

    social = prompt["social_context"]
    assert social["trade_context"] == (
        "Recent trade history: You offered Ziv 1 wheat for 1 brick; Ziv rejected. "
        "Ziv offered you 1 sheep for 1 wood; you accepted."
    )
    assert social["pending_trades"] == [
        {
            "trade_id": "trade_3",
            "from": "Ziv",
            "to": "Shon",
            "offer": {"ore": 1},
            "request": {"wheat": 1},
            "status": "pending",
        }
    ]


def test_hebrew_chat_prompt_requires_exact_resource_terms():
    config = AIConfig()
    config.agent.chat_language = "hebrew"
    manager = PromptManager(config)
    state = _game_state("Hadar", "Shon")

    prompt = manager.create_prompt(
        player_num=0,
        player_name="Hadar",
        player_color="Red",
        game_state=state,
        what_happened="Your turn.",
        available_actions=[{"type": "end_turn", "example_parameters": "{}"}],
    )

    instructions = prompt["task_context"]["instructions"]
    assert HEBREW_RESOURCE_TERMS_INSTRUCTION in instructions
    assert "brick=ื˜ื™ื˜" in instructions
    assert "ore=ืื‘ืŸ" in instructions
    assert "sheep=ื›ื‘ืฉื”" in instructions
    assert "wheat=ื—ื™ื˜ื”" in instructions


def test_hebrew_chat_schema_requires_exact_resource_terms():
    schema = get_schema_for_response_type(
        ResponseType.ACTIVE_TURN,
        SchemaVersion.V2,
        chat_language="hebrew",
    )

    description = schema["properties"]["say_outloud"]["description"]
    assert HEBREW_RESOURCE_TERMS_INSTRUCTION in description