Skip to content

Conversation

@wyruweso
Copy link

While looking into LocalREPL.add_context() during the investigation of how rlm works, I noticed it does an extra temp-file roundtrip for every context payload. It looks like it might have been an attempt at “lazy loading”, but in practice it only adds overhead (the data is still fully read back into memory).

The Problem

The previous implementation wrote context to a temp file and then immediately read it back into memory:

# OLD: Write to file, then read back
with open(context_path, "w") as f:
    f.write(context_payload)
self.execute_code(
    f"with open(r'{context_path}', 'r') as f:\n    {var_name} = f.read()"
)

This approach:

  • Introduced file I/O overhead (write + read)
  • Still loaded the entire context into memory (no lazy-loading benefit)
  • Used exec() for simple variable assignment

The Fix

Store context directly in memory, using deep copy for isolation (as expected by the SupportsPersistence protocol):

# NEW: Direct assignment
if isinstance(context_payload, str):
    self.locals[var_name] = context_payload  # immutable, no copy needed
else:
    self.locals[var_name] = copy.deepcopy(context_payload)  # isolate mutable data

Performance Impact

Context Size Before (file I/O) After (direct) Speedup
1KB string 0.035 ms ~0 ms 241×
1MB string 1.065 ms 0.007 ms 147×
10MB string 12.38 ms 0.093 ms 133×
10K-key dict 5.63 ms 2.31 ms 2.4×

Design Note

I noticed the previous temp-file roundtrip looks like it was aiming at “lazy loading”, but in practice it wasn’t.

If we ever want real lazy / file-backed contexts, that’s a bigger follow-up: today the prompt/examples (and tests) assume the context is an in-memory object you can len(), slice, iterate, call string methods on, etc. To make lazy loading work properly we’d need either a file-backed proxy that preserves those semantics, or a separate API like add_context_from_file(...) + updated prompt/examples/tests that teach the model to read/stream from a path.

This PR just removes the misleading roundtrip that didn’t actually provide lazy loading benefits.

Copilot AI review requested due to automatic review settings January 17, 2026 23:04
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR eliminates unnecessary file I/O overhead in LocalREPL.add_context() by storing context payloads directly in memory instead of writing to temporary files and immediately reading them back.

Changes:

  • Removed file-based roundtrip for context storage (temp file write + read)
  • Implemented direct memory assignment with deep copy for mutable types (dicts/lists) to maintain isolation
  • Removed unused json import

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant