Skip to content

Commit 67b7410

Browse files
kmadclaude
andcommitted
refactor: remove build_context_code, keep only DataFrame-specific codegen
Remove the unified build_context_code() which handled strings, dicts, and DataFrames with fragile escaping. Keep only build_dataframe_context_code() for DataFrame-specific Parquet-over-base64 codegen. Restore original per-environment string/dict handling in Modal, Prime, and Daytona REPLs. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 7125a80 commit 67b7410

5 files changed

Lines changed: 48 additions & 109 deletions

File tree

‎rlm/environments/daytona_repl.py‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -610,9 +610,23 @@ def _handle_llm_request(self, req_data: dict) -> dict:
610610

611611
def load_context(self, context_payload: ContextPayload):
612612
"""Load context into the sandbox environment."""
613-
from rlm.utils.dataframe_utils import build_context_code
613+
from rlm.utils.dataframe_utils import (
614+
build_dataframe_context_code,
615+
dataframe_to_parquet_b64,
616+
get_dataframe_type,
617+
)
614618

615-
self.execute_code(build_context_code(context_payload))
619+
df_type = get_dataframe_type(context_payload)
620+
if df_type is not None:
621+
parquet_b64, df_type = dataframe_to_parquet_b64(context_payload)
622+
self.execute_code(build_dataframe_context_code(parquet_b64, df_type))
623+
elif isinstance(context_payload, str):
624+
escaped = context_payload.replace("\\", "\\\\").replace('"""', '\\"\\"\\"')
625+
self.execute_code(f'context = """{escaped}"""')
626+
else:
627+
context_json = json.dumps(context_payload)
628+
escaped_json = context_json.replace("\\", "\\\\").replace("'", "\\'")
629+
self.execute_code(f"import json; context = json.loads('{escaped_json}')")
616630

617631
def execute_code(self, code: str) -> REPLResult:
618632
"""Execute code in the Daytona sandbox and return result."""

‎rlm/environments/modal_repl.py‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,9 +438,23 @@ def _handle_llm_request(self, req_data: dict) -> dict:
438438

439439
def load_context(self, context_payload: ContextPayload):
440440
"""Load context into the sandbox environment."""
441-
from rlm.utils.dataframe_utils import build_context_code
441+
from rlm.utils.dataframe_utils import (
442+
build_dataframe_context_code,
443+
dataframe_to_parquet_b64,
444+
get_dataframe_type,
445+
)
442446

443-
self.execute_code(build_context_code(context_payload))
447+
df_type = get_dataframe_type(context_payload)
448+
if df_type is not None:
449+
parquet_b64, df_type = dataframe_to_parquet_b64(context_payload)
450+
self.execute_code(build_dataframe_context_code(parquet_b64, df_type))
451+
elif isinstance(context_payload, str):
452+
escaped = context_payload.replace("\\", "\\\\").replace('"""', '\\"\\"\\"')
453+
self.execute_code(f'context = """{escaped}"""')
454+
else:
455+
context_json = json.dumps(context_payload)
456+
escaped_json = context_json.replace("\\", "\\\\").replace("'", "\\'")
457+
self.execute_code(f"import json; context = json.loads('{escaped_json}')")
444458

445459
def execute_code(self, code: str) -> REPLResult:
446460
"""Execute code in the Modal sandbox and return result."""

‎rlm/environments/prime_repl.py‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -504,9 +504,23 @@ def _handle_llm_request(self, req_data: dict) -> dict:
504504

505505
def load_context(self, context_payload: ContextPayload):
506506
"""Load context into the sandbox environment."""
507-
from rlm.utils.dataframe_utils import build_context_code
507+
from rlm.utils.dataframe_utils import (
508+
build_dataframe_context_code,
509+
dataframe_to_parquet_b64,
510+
get_dataframe_type,
511+
)
508512

509-
self.execute_code(build_context_code(context_payload))
513+
df_type = get_dataframe_type(context_payload)
514+
if df_type is not None:
515+
parquet_b64, df_type = dataframe_to_parquet_b64(context_payload)
516+
self.execute_code(build_dataframe_context_code(parquet_b64, df_type))
517+
elif isinstance(context_payload, str):
518+
escaped = context_payload.replace("\\", "\\\\").replace('"""', '\\"\\"\\"')
519+
self.execute_code(f'context = """{escaped}"""')
520+
else:
521+
context_json = json.dumps(context_payload)
522+
escaped_json = context_json.replace("\\", "\\\\").replace("'", "\\'")
523+
self.execute_code(f"import json; context = json.loads('{escaped_json}')")
510524

511525
def execute_code(self, code: str) -> REPLResult:
512526
"""Execute code in the Prime sandbox and return result."""

‎rlm/utils/dataframe_utils.py‎

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import base64
22
import io
3-
import json
43
from typing import Any, Literal
54

65
DataFrameType = Literal["pandas"]
@@ -83,25 +82,3 @@ def build_dataframe_context_code(
8382
f"_parquet_bytes = base64.b64decode('{parquet_b64}')\n"
8483
f"{var_name} = pd.read_parquet(io.BytesIO(_parquet_bytes))"
8584
)
86-
87-
88-
def build_context_code(context_payload: Any, var_name: str = "context") -> str:
89-
"""Build Python code that reconstructs *context_payload* as a variable.
90-
91-
Handles three payload shapes:
92-
- pandas DataFrame → Parquet-over-base64 round-trip
93-
- str → triple-quoted string literal
94-
- other (dict/list) → ``json.loads`` from a JSON string literal
95-
"""
96-
df_type = get_dataframe_type(context_payload)
97-
if df_type is not None:
98-
parquet_b64, df_type = dataframe_to_parquet_b64(context_payload)
99-
return build_dataframe_context_code(parquet_b64, df_type, var_name=var_name)
100-
101-
if isinstance(context_payload, str):
102-
escaped = context_payload.replace("\\", "\\\\").replace('"""', '\\"\\"\\"')
103-
return f'{var_name} = """{escaped}"""'
104-
105-
context_json = json.dumps(context_payload)
106-
escaped_json = context_json.replace("\\", "\\\\").replace("'", "\\'")
107-
return f"import json; {var_name} = json.loads('{escaped_json}')"

‎tests/test_dataframe_utils.py‎

Lines changed: 0 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22

33
import base64
44
import io
5-
import json
65

76
import pytest
87

98
from rlm.utils.dataframe_utils import (
10-
build_context_code,
119
build_dataframe_context_code,
1210
dataframe_metadata,
1311
dataframe_to_parquet_b64,
@@ -157,81 +155,3 @@ def test_custom_var_name(self):
157155
exec(code, ns)
158156
assert "my_df" in ns
159157
assert ns["my_df"]["v"].tolist() == [1]
160-
161-
162-
# ---------------------------------------------------------------------------
163-
# Unified dispatch — build_context_code()
164-
# ---------------------------------------------------------------------------
165-
166-
167-
class TestBuildContextCode:
168-
"""Tests for the unified build_context_code() dispatcher."""
169-
170-
def test_string_payload(self):
171-
code = build_context_code("hello world")
172-
ns = {}
173-
exec(code, ns)
174-
assert ns["context"] == "hello world"
175-
176-
def test_dict_payload(self):
177-
payload = {"key": "value", "num": 42}
178-
code = build_context_code(payload)
179-
ns = {}
180-
exec(code, ns)
181-
assert ns["context"] == payload
182-
183-
def test_list_payload(self):
184-
payload = [1, 2, "three"]
185-
code = build_context_code(payload)
186-
ns = {}
187-
exec(code, ns)
188-
assert ns["context"] == payload
189-
190-
def test_dataframe_payload(self):
191-
pd = pytest.importorskip("pandas")
192-
df = pd.DataFrame({"a": [1, 2], "b": [3, 4]})
193-
code = build_context_code(df)
194-
ns = {}
195-
exec(code, ns)
196-
assert ns["context"]["a"].tolist() == [1, 2]
197-
assert ns["context"]["b"].tolist() == [3, 4]
198-
199-
def test_empty_string(self):
200-
code = build_context_code("")
201-
ns = {}
202-
exec(code, ns)
203-
assert ns["context"] == ""
204-
205-
def test_empty_dict(self):
206-
code = build_context_code({})
207-
ns = {}
208-
exec(code, ns)
209-
assert ns["context"] == {}
210-
211-
def test_empty_list(self):
212-
code = build_context_code([])
213-
ns = {}
214-
exec(code, ns)
215-
assert ns["context"] == []
216-
217-
def test_special_chars_in_string(self):
218-
payload = 'He said "hello" and she said \'goodbye\'\nNew line\ttab'
219-
code = build_context_code(payload)
220-
ns = {}
221-
exec(code, ns)
222-
assert ns["context"] == payload
223-
224-
def test_custom_var_name(self):
225-
code = build_context_code("data", var_name="my_var")
226-
ns = {}
227-
exec(code, ns)
228-
assert ns["my_var"] == "data"
229-
assert "context" not in ns
230-
231-
def test_nested_dict(self):
232-
payload = {"outer": {"inner": [1, 2, 3]}, "flag": True}
233-
code = build_context_code(payload)
234-
ns = {}
235-
exec(code, ns)
236-
assert ns["context"]["outer"]["inner"] == [1, 2, 3]
237-
assert ns["context"]["flag"] is True

0 commit comments

Comments
 (0)