11"""Tests for the slime backend's agent-payload conversion.
22
33``_sample_to_payload`` is the contract between slime's ``Sample`` object
4- and the agent's ``@rollout_entrypoint`` payload. Post the data-contract
5- change, the rule is: the agent receives ``Sample.metadata`` verbatim
6- (shallow-copied). These tests pin that rule so the contract can't
7- regress silently.
4+ and the agent's ``@rollout_entrypoint`` payload. The rule: the agent
5+ receives ``Sample.metadata`` verbatim (shallow-copied).
86"""
97
108from types import SimpleNamespace
1311
1412
1513def test_metadata_is_returned_verbatim ():
16- sample = SimpleNamespace (metadata = {"task_id" : "t1" , "prompt" : "hi" })
17- assert _sample_to_payload (sample ) == {"task_id" : "t1" , "prompt" : "hi" }
14+ """Core contract: the agent payload is Sample.metadata as-is.
1815
19-
20- def test_empty_metadata_returns_empty_dict ():
21- sample = SimpleNamespace (metadata = {})
22- assert _sample_to_payload (sample ) == {}
23-
24-
25- def test_missing_metadata_attr_returns_empty_dict ():
26- sample = SimpleNamespace ()
27- assert _sample_to_payload (sample ) == {}
28-
29-
30- def test_none_metadata_returns_empty_dict ():
31- sample = SimpleNamespace (metadata = None )
32- assert _sample_to_payload (sample ) == {}
33-
34-
35- def test_non_dict_metadata_returns_empty_dict ():
36- sample = SimpleNamespace (metadata = "not a dict" )
37- assert _sample_to_payload (sample ) == {}
16+ Fields on Sample outside metadata (prompt, label) are slime's own
17+ concern and must not leak into the payload.
18+ """
19+ sample = SimpleNamespace (
20+ prompt = "slime-side prompt" ,
21+ label = "slime-side label" ,
22+ metadata = {"task_id" : "t1" , "answer" : "42" },
23+ )
24+ assert _sample_to_payload (sample ) == {"task_id" : "t1" , "answer" : "42" }
3825
3926
4027def test_returned_dict_is_a_shallow_copy ():
41- """Mutations to the returned payload must not leak into Sample.metadata.
28+ """Mutations to the payload must not leak back into Sample.metadata.
4229
43- _process_one_episode mutates ``Sample.metadata`` (e.g. injects
44- ``task_metadata``) downstream ; the agent's view must remain stable.
30+ `` _process_one_episode`` later injects keys into ``Sample.metadata``
31+ (e.g. ``task_metadata``); the agent's view must stay stable.
4532 """
46- metadata = {"prompt" : "hi" , "answer" : "42" }
33+ metadata = {"prompt" : "hi" }
4734 sample = SimpleNamespace (metadata = metadata )
4835
4936 payload = _sample_to_payload (sample )
@@ -52,15 +39,11 @@ def test_returned_dict_is_a_shallow_copy():
5239 assert "injected" not in metadata
5340
5441
55- def test_prompt_and_label_on_sample_are_ignored ():
56- """Top-level sample.prompt / sample.label are no longer part of the payload.
57-
58- They exist on ``Sample`` for slime's own use (tokenization, logging).
59- Post the data-contract change, only ``metadata`` reaches the agent.
60- """
61- sample = SimpleNamespace (
62- prompt = "slime-side prompt" ,
63- label = "slime-side label" ,
64- metadata = {"foo" : "bar" },
65- )
66- assert _sample_to_payload (sample ) == {"foo" : "bar" }
42+ def test_missing_or_invalid_metadata_returns_empty_dict ():
43+ """Defensive fallback when metadata is absent or not a dict."""
44+ for sample in [
45+ SimpleNamespace (), # attribute absent
46+ SimpleNamespace (metadata = None ), # explicit None
47+ SimpleNamespace (metadata = "not a dict" ), # wrong type
48+ ]:
49+ assert _sample_to_payload (sample ) == {}
0 commit comments