77 _CYCLE_SENTINEL ,
88 DATACLASS_MARKER ,
99 MODEL_MARKER ,
10+ PRESERVED_MARKER ,
11+ decode_checkpoint_value ,
1012 encode_checkpoint_value ,
1113)
1214
@@ -296,43 +298,44 @@ def test_encode_list_with_self_reference() -> None:
296298
297299
298300# --- Tests for reserved keyword handling ---
299- # Note: Security is enforced at deserialization time by validating class types,
300- # not at serialization time. This allows legitimate encoded data to be re-encoded .
301+ # User data containing marker keys + "value" is preserved in a special envelope
302+ # during serialization and recovered during deserialization .
301303
302304
303- def test_encode_allows_dict_with_model_marker_and_value () -> None :
304- """Test that encoding a dict with MODEL_MARKER and 'value' is allowed.
305-
306- Security is enforced at deserialization time, not serialization time.
307- """
305+ def test_encode_preserves_dict_with_model_marker_and_value () -> None :
306+ """Test that user dict with MODEL_MARKER and 'value' is preserved in envelope."""
308307 data = {
309308 MODEL_MARKER : "some.module:SomeClass" ,
310309 "value" : {"data" : "test" },
311310 }
312311 result = encode_checkpoint_value (data )
313- assert MODEL_MARKER in result
312+ # Should be wrapped in preservation envelope
313+ assert PRESERVED_MARKER in result
314+ assert result [PRESERVED_MARKER ] is True
314315 assert "value" in result
316+ # The inner value should contain the original dict
317+ assert MODEL_MARKER in result ["value" ]
318+ assert result ["value" ]["value" ] == {"data" : "test" }
315319
316320
317- def test_encode_allows_dict_with_dataclass_marker_and_value () -> None :
318- """Test that encoding a dict with DATACLASS_MARKER and 'value' is allowed.
319-
320- Security is enforced at deserialization time, not serialization time.
321- """
321+ def test_encode_preserves_dict_with_dataclass_marker_and_value () -> None :
322+ """Test that user dict with DATACLASS_MARKER and 'value' is preserved in envelope."""
322323 data = {
323324 DATACLASS_MARKER : "some.module:SomeClass" ,
324325 "value" : {"field" : "test" },
325326 }
326327 result = encode_checkpoint_value (data )
327- assert DATACLASS_MARKER in result
328+ # Should be wrapped in preservation envelope
329+ assert PRESERVED_MARKER in result
330+ assert result [PRESERVED_MARKER ] is True
328331 assert "value" in result
332+ # The inner value should contain the original dict
333+ assert DATACLASS_MARKER in result ["value" ]
334+ assert result ["value" ]["value" ] == {"field" : "test" }
329335
330336
331- def test_encode_allows_nested_dict_with_marker_keys () -> None :
332- """Test that encoding nested dict with marker keys is allowed.
333-
334- Security is enforced at deserialization time, not serialization time.
335- """
337+ def test_encode_preserves_nested_dict_with_marker_keys () -> None :
338+ """Test that nested dict with marker keys is preserved in envelope."""
336339 nested_data = {
337340 "outer" : {
338341 MODEL_MARKER : "some.module:SomeClass" ,
@@ -341,27 +344,61 @@ def test_encode_allows_nested_dict_with_marker_keys() -> None:
341344 }
342345 result = encode_checkpoint_value (nested_data )
343346 assert "outer" in result
344- assert MODEL_MARKER in result ["outer" ]
347+ # The nested dict should be wrapped in preservation envelope
348+ assert PRESERVED_MARKER in result ["outer" ]
349+ assert result ["outer" ][PRESERVED_MARKER ] is True
350+
351+
352+ def test_decode_recovers_preserved_dict_with_model_marker () -> None :
353+ """Test that preserved dict with MODEL_MARKER is recovered correctly."""
354+ original_data = {
355+ MODEL_MARKER : "some.module:SomeClass" ,
356+ "value" : {"data" : "test" },
357+ }
358+ encoded = encode_checkpoint_value (original_data )
359+ decoded = decode_checkpoint_value (encoded )
360+ # Should recover the original dict structure
361+ assert MODEL_MARKER in decoded
362+ assert decoded [MODEL_MARKER ] == "some.module:SomeClass"
363+ assert decoded ["value" ] == {"data" : "test" }
364+
365+
366+ def test_decode_recovers_preserved_dict_with_dataclass_marker () -> None :
367+ """Test that preserved dict with DATACLASS_MARKER is recovered correctly."""
368+ original_data = {
369+ DATACLASS_MARKER : "some.module:SomeClass" ,
370+ "value" : {"field" : "test" },
371+ }
372+ encoded = encode_checkpoint_value (original_data )
373+ decoded = decode_checkpoint_value (encoded )
374+ # Should recover the original dict structure
375+ assert DATACLASS_MARKER in decoded
376+ assert decoded [DATACLASS_MARKER ] == "some.module:SomeClass"
377+ assert decoded ["value" ] == {"field" : "test" }
345378
346379
347380def test_encode_allows_marker_without_value () -> None :
348- """Test that a dict with marker key but without 'value' key is allowed ."""
381+ """Test that a dict with marker key but without 'value' key is NOT preserved ."""
349382 data = {
350383 MODEL_MARKER : "some.module:SomeClass" ,
351384 "other_key" : "allowed" ,
352385 }
353386 result = encode_checkpoint_value (data )
387+ # Should NOT be wrapped (no "value" key present)
388+ assert PRESERVED_MARKER not in result
354389 assert MODEL_MARKER in result
355390 assert result ["other_key" ] == "allowed"
356391
357392
358393def test_encode_allows_value_without_marker () -> None :
359- """Test that a dict with 'value' key but without marker is allowed ."""
394+ """Test that a dict with 'value' key but without marker is NOT preserved ."""
360395 data = {
361396 "value" : {"nested" : "data" },
362397 "other_key" : "allowed" ,
363398 }
364399 result = encode_checkpoint_value (data )
400+ # Should NOT be wrapped (no marker key present)
401+ assert PRESERVED_MARKER not in result
365402 assert "value" in result
366403 assert result ["other_key" ] == "allowed"
367404
0 commit comments