Commit cb9cab7
proposal(environments): SupportsPersistence Protocol for multi-turn sessions (#40)
* feat(local-repl): add multi-context support for persistent REPL
* feat(core): add persistent flag for multi-turn REPL conversations
Add persistent=True option to RLM that reuses the environment across
completion() calls instead of creating/destroying for each call. This
enables multi-turn conversations where variables and contexts persist.
- Add persistent parameter to RLM constructor
- Reuse environment when persistent=True, store as _persistent_env
- Add close() method and context manager support (__enter__/__exit__)
- Environment cleanup only on explicit close() when persistent
* feat(prompts): inform model about multiple contexts in multi-turn sessions
Add context_count parameter to build_user_prompt() so the model knows
when multiple contexts are available (context_0, context_1, etc.) during
persistent REPL sessions.
* fix(core): validate environment supports persistent mode before use
Add validation to prevent AttributeError when persistent=True is used
with environments that don't implement the required methods
(update_handler_address, add_context, get_context_count).
- Add _validate_persistent_environment_support() called at init time
- Add _env_supports_persistence() for runtime capability checking
- Add defensive runtime check before calling persistence methods
- Raise clear ValueError if unsupported environment is configured
* feat(local-repl): add message history storage for multi-turn sessions
Store conversation histories as versioned variables (history_0, history_1, etc.)
in the REPL environment, making them accessible for subsequent queries. This
enables models to reference prior conversation context in persistent sessions.
* feat(environments): add persistent parameter to all REPL classes
Add persistent parameter to BaseEnv, IsolatedEnv, and NonIsolatedEnv.
DockerREPL, ModalREPL, and PrimeREPL raise NotImplementedError when
persistent=True since they don't yet support it. LocalREPL passes
the parameter through to the base class.
Also makes DockerREPL cleanup more defensive with hasattr checks.
* test(local-repl): replace multi-context tests with non-persistent simulation tests
Replace TestLocalREPLMultiContext and TestLocalREPLHistory with
TestLocalREPLSimulatingRLMNoPersistence. New tests verify that
environments reset between RLM completions when persistent=False.
* test(local-repl): add persistent mode unit tests
Add TestLocalREPLMultiContext, TestLocalREPLHistory, and
TestLocalREPLPersistentState test classes for LocalREPL's
persistent mode features including multi-context versioning
and message history storage.
* test(rlm): add multi-turn integration tests
Add comprehensive integration tests for persistent RLM sessions:
- Environment reuse across completion calls
- Context and history accumulation
- Variable persistence between completions
- Prompt awareness of contexts/histories
- Resource cleanup on close
- Validation of unsupported environments
* refactor(tests): replace ScriptedMockLM with standard unittest.mock
Remove custom ScriptedMockLM class (58 lines) and replace with
create_mock_lm() helper (14 lines) using standard Mock patterns.
* refactor(environments): add SupportsPersistence Protocol
Replace scattered hasattr checks with a runtime-checkable Protocol
that defines the persistence capability contract. This provides
type checker enforcement and IDE autocomplete support.
* docs(environments): add comprehensive SupportsPersistence Protocol docs
Document expected behavior to implement persistence:
- Versioning behavior (context_0, context_1, ...)
- Aliasing behavior (context -> context_0)
- Method contracts with detailed docstrings
- References to tests and example implementation
* add ruff lint on test
* fix linting on tests
---------
Co-authored-by: Alex Zhang <alex.lx.zhang@gmail.com>1 parent a30805b commit cb9cab7
File tree
11 files changed
+1017
-48
lines changed- rlm
- core
- environments
- utils
- tests
11 files changed
+1017
-48
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | | - | |
| 16 | + | |
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| |||
51 | 51 | | |
52 | 52 | | |
53 | 53 | | |
| 54 | + | |
54 | 55 | | |
55 | 56 | | |
56 | 57 | | |
| |||
66 | 67 | | |
67 | 68 | | |
68 | 69 | | |
| 70 | + | |
69 | 71 | | |
70 | 72 | | |
71 | 73 | | |
| |||
84 | 86 | | |
85 | 87 | | |
86 | 88 | | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
87 | 97 | | |
88 | 98 | | |
89 | 99 | | |
| |||
108 | 118 | | |
109 | 119 | | |
110 | 120 | | |
111 | | - | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
112 | 124 | | |
113 | 125 | | |
114 | 126 | | |
| |||
122 | 134 | | |
123 | 135 | | |
124 | 136 | | |
125 | | - | |
126 | | - | |
127 | | - | |
128 | | - | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
129 | 154 | | |
130 | | - | |
131 | | - | |
| 155 | + | |
| 156 | + | |
132 | 157 | | |
133 | 158 | | |
134 | 159 | | |
135 | 160 | | |
136 | | - | |
137 | 161 | | |
138 | | - | |
| 162 | + | |
139 | 163 | | |
140 | 164 | | |
141 | 165 | | |
| |||
177 | 201 | | |
178 | 202 | | |
179 | 203 | | |
180 | | - | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
181 | 217 | | |
182 | 218 | | |
183 | 219 | | |
| |||
201 | 237 | | |
202 | 238 | | |
203 | 239 | | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
204 | 245 | | |
205 | 246 | | |
206 | 247 | | |
| |||
223 | 264 | | |
224 | 265 | | |
225 | 266 | | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
226 | 272 | | |
227 | 273 | | |
228 | 274 | | |
| |||
292 | 338 | | |
293 | 339 | | |
294 | 340 | | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
| 3 | + | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
| 7 | + | |
6 | 8 | | |
7 | 9 | | |
8 | 10 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
| 2 | + | |
2 | 3 | | |
3 | 4 | | |
4 | 5 | | |
| |||
9 | 10 | | |
10 | 11 | | |
11 | 12 | | |
12 | | - | |
| 13 | + | |
| 14 | + | |
13 | 15 | | |
14 | 16 | | |
15 | 17 | | |
| |||
31 | 33 | | |
32 | 34 | | |
33 | 35 | | |
34 | | - | |
35 | | - | |
| 36 | + | |
| 37 | + | |
36 | 38 | | |
37 | 39 | | |
38 | 40 | | |
| |||
54 | 56 | | |
55 | 57 | | |
56 | 58 | | |
57 | | - | |
58 | | - | |
| 59 | + | |
| 60 | + | |
59 | 61 | | |
60 | 62 | | |
61 | 63 | | |
| |||
68 | 70 | | |
69 | 71 | | |
70 | 72 | | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
180 | 180 | | |
181 | 181 | | |
182 | 182 | | |
| 183 | + | |
183 | 184 | | |
184 | 185 | | |
185 | | - | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
186 | 191 | | |
187 | 192 | | |
188 | 193 | | |
| |||
292 | 297 | | |
293 | 298 | | |
294 | 299 | | |
295 | | - | |
| 300 | + | |
296 | 301 | | |
297 | 302 | | |
298 | | - | |
| 303 | + | |
299 | 304 | | |
300 | 305 | | |
301 | | - | |
| 306 | + | |
302 | 307 | | |
303 | 308 | | |
304 | 309 | | |
| |||
0 commit comments