|
| 1 | +"""Tests for the slime backend's agent-payload conversion. |
| 2 | +
|
| 3 | +``_sample_to_payload`` is the contract between slime's ``Sample`` object |
| 4 | +and the agent's ``@rollout_entrypoint`` payload. The rule: the agent |
| 5 | +receives ``Sample.metadata`` verbatim (shallow-copied). |
| 6 | +""" |
| 7 | + |
| 8 | +from types import SimpleNamespace |
| 9 | + |
| 10 | +from agentcore_rl_toolkit.backends.slime.integration.rollout import _sample_to_payload |
| 11 | + |
| 12 | + |
| 13 | +def test_metadata_is_returned_verbatim(): |
| 14 | + """Core contract: the agent payload is Sample.metadata as-is. |
| 15 | +
|
| 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"} |
| 25 | + |
| 26 | + |
| 27 | +def test_returned_dict_is_a_shallow_copy(): |
| 28 | + """Mutations to the payload must not leak back into Sample.metadata. |
| 29 | +
|
| 30 | + ``_process_one_episode`` later injects keys into ``Sample.metadata`` |
| 31 | + (e.g. ``task_metadata``); the agent's view must stay stable. |
| 32 | + """ |
| 33 | + metadata = {"prompt": "hi"} |
| 34 | + sample = SimpleNamespace(metadata=metadata) |
| 35 | + |
| 36 | + payload = _sample_to_payload(sample) |
| 37 | + payload["injected"] = True |
| 38 | + |
| 39 | + assert "injected" not in metadata |
| 40 | + |
| 41 | + |
| 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