Repository navigation
Expand file tree
/
Copy pathtest_depth_metadata.py
More file actions
653 lines (522 loc) · 23.9 KB
/
Copy pathtest_depth_metadata.py
File metadata and controls
653 lines (522 loc) · 23.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
"""Tests verifying depth=1 functionality is preserved and depth>1 metadata propagation works.
1. depth=1: completion loop, limit checks, logger metadata all work as before
2. depth>1: child RLM gets its own logger, metadata flows back through RLMChatCompletion
"""
from unittest.mock import Mock, patch
import pytest
import rlm.core.rlm as rlm_module
from rlm import RLM
from rlm.core.types import ModelUsageSummary, UsageSummary
from rlm.logger import RLMLogger
from rlm.utils.exceptions import (
BudgetExceededError,
ErrorThresholdExceededError,
TimeoutExceededError,
TokenLimitExceededError,
)
def create_mock_lm(responses: list[str], model_name: str = "mock-model") -> Mock:
"""Create a mock LM that returns responses in order."""
mock = Mock()
mock.model_name = model_name
mock.completion.side_effect = list(responses)
mock.get_usage_summary.return_value = UsageSummary(
model_usage_summaries={
model_name: ModelUsageSummary(
total_calls=1, total_input_tokens=100, total_output_tokens=50
)
}
)
mock.get_last_usage.return_value = mock.get_usage_summary.return_value
return mock
# ========================================================================
# depth=1 tests: verify existing behavior is preserved
# ========================================================================
class TestDepth1CompletionLoop:
"""Verify depth=1 completion loop works identically to before refactoring."""
def test_basic_completion_with_final_answer(self):
"""depth=1 RLM should complete normally with FINAL() answer."""
with patch.object(rlm_module, "get_client") as mock_get_client:
mock_lm = create_mock_lm(["FINAL(42)"])
mock_get_client.return_value = mock_lm
rlm = RLM(
backend="openai",
backend_kwargs={"model_name": "test-model"},
max_depth=1,
)
result = rlm.completion("What is the answer?")
assert result.response == "42"
assert result.root_model == "test-model"
def test_multi_iteration_before_final(self):
"""depth=1 should iterate multiple times before finding FINAL()."""
with patch.object(rlm_module, "get_client") as mock_get_client:
mock_lm = create_mock_lm(
[
"Let me think...\n```repl\nx = 1 + 1\nprint(x)\n```",
"Now I know.\n```repl\ny = x * 2\nprint(y)\n```",
"FINAL(4)",
]
)
mock_get_client.return_value = mock_lm
rlm = RLM(
backend="openai",
backend_kwargs={"model_name": "test-model"},
max_depth=1,
)
result = rlm.completion("Compute 2*2")
assert result.response == "4"
def test_no_subcall_fn_at_depth_1(self):
"""depth=1 (max_depth=1) should NOT pass subcall_fn to environment."""
with patch.object(rlm_module, "get_client") as mock_get_client:
mock_lm = create_mock_lm(["FINAL(done)"])
mock_get_client.return_value = mock_lm
rlm = RLM(
backend="openai",
backend_kwargs={"model_name": "test-model"},
max_depth=1,
)
# Patch get_environment to capture kwargs without running full loop
with patch.object(rlm_module, "get_environment") as mock_get_env:
# Make get_environment raise to short-circuit after capturing args
mock_get_env.side_effect = lambda env_type, kwargs: (_ for _ in ()).throw(
RuntimeError("captured")
)
try:
rlm.completion("test")
except RuntimeError:
pass
call_args = mock_get_env.call_args
env_kwargs = call_args[0][1]
assert "subcall_fn" not in env_kwargs
def test_subcall_fn_passed_at_depth_gt_1(self):
"""max_depth>1 SHOULD pass subcall_fn to environment."""
with patch.object(rlm_module, "get_client") as mock_get_client:
mock_lm = create_mock_lm(["FINAL(done)"])
mock_get_client.return_value = mock_lm
rlm = RLM(
backend="openai",
backend_kwargs={"model_name": "test-model"},
max_depth=2,
)
with patch.object(rlm_module, "get_environment") as mock_get_env:
mock_get_env.side_effect = lambda env_type, kwargs: (_ for _ in ()).throw(
RuntimeError("captured")
)
try:
rlm.completion("test")
except RuntimeError:
pass
call_args = mock_get_env.call_args
env_kwargs = call_args[0][1]
assert "subcall_fn" in env_kwargs
assert env_kwargs["subcall_fn"] is not None
class TestDepth1LimitChecks:
"""Verify limit checks work correctly in the refactored helpers."""
def test_timeout_check_raises(self):
"""_check_timeout should raise TimeoutExceededError when exceeded."""
import time
rlm = RLM(
backend="openai",
backend_kwargs={"model_name": "test"},
max_timeout=10.0,
)
# Simulate start time 15 seconds ago
fake_start = time.perf_counter() - 15.0
with pytest.raises(TimeoutExceededError) as exc_info:
rlm._check_timeout(0, fake_start)
assert exc_info.value.elapsed > 10.0
assert exc_info.value.timeout == 10.0
def test_timeout_check_no_raise_within_limit(self):
"""_check_timeout should not raise when within limit."""
import time
rlm = RLM(
backend="openai",
backend_kwargs={"model_name": "test"},
max_timeout=100.0,
)
fake_start = time.perf_counter() - 1.0
# Should not raise
rlm._check_timeout(0, fake_start)
def test_timeout_check_noop_when_none(self):
"""_check_timeout should be a no-op when max_timeout is None."""
import time
rlm = RLM(
backend="openai",
backend_kwargs={"model_name": "test"},
max_timeout=None,
)
# Even with a very old start time, should not raise
rlm._check_timeout(0, time.perf_counter() - 99999)
def test_error_threshold_check(self):
"""_check_iteration_limits should raise on consecutive errors."""
from rlm.core.types import CodeBlock, REPLResult, RLMIteration
rlm = RLM(
backend="openai",
backend_kwargs={"model_name": "test"},
max_errors=2,
)
mock_handler = Mock()
mock_handler.get_usage_summary.return_value = UsageSummary(
model_usage_summaries={
"test": ModelUsageSummary(
total_calls=1, total_input_tokens=10, total_output_tokens=10
)
}
)
error_result = REPLResult(stdout="", stderr="SyntaxError: bad", locals={}, rlm_calls=[])
error_iteration = RLMIteration(
prompt="test", response="code", code_blocks=[CodeBlock(code="bad", result=error_result)]
)
# First error
rlm._check_iteration_limits(error_iteration, 0, mock_handler)
assert rlm._consecutive_errors == 1
# Second error should raise
with pytest.raises(ErrorThresholdExceededError) as exc_info:
rlm._check_iteration_limits(error_iteration, 1, mock_handler)
assert exc_info.value.error_count == 2
assert exc_info.value.threshold == 2
def test_error_count_resets_on_success(self):
"""Consecutive error count should reset on a successful iteration."""
from rlm.core.types import CodeBlock, REPLResult, RLMIteration
rlm = RLM(
backend="openai",
backend_kwargs={"model_name": "test"},
max_errors=3,
)
mock_handler = Mock()
mock_handler.get_usage_summary.return_value = UsageSummary(
model_usage_summaries={
"test": ModelUsageSummary(
total_calls=1, total_input_tokens=10, total_output_tokens=10
)
}
)
error_result = REPLResult(stdout="", stderr="Error!", locals={}, rlm_calls=[])
error_iter = RLMIteration(
prompt="test", response="code", code_blocks=[CodeBlock(code="bad", result=error_result)]
)
ok_result = REPLResult(stdout="ok", stderr="", locals={}, rlm_calls=[])
ok_iter = RLMIteration(
prompt="test", response="code", code_blocks=[CodeBlock(code="good", result=ok_result)]
)
# Two errors
rlm._check_iteration_limits(error_iter, 0, mock_handler)
rlm._check_iteration_limits(error_iter, 1, mock_handler)
assert rlm._consecutive_errors == 2
# Success resets
rlm._check_iteration_limits(ok_iter, 2, mock_handler)
assert rlm._consecutive_errors == 0
def test_budget_check_raises(self):
"""_completion_turn syncs handler cost; _check_iteration_limits detects overspend."""
from rlm.core.types import REPLResult
rlm_inst = RLM(
backend="openai",
backend_kwargs={"model_name": "test"},
max_budget=0.01,
)
# Mock handler: completion returns no code blocks, handler spent $0.05
mock_handler = Mock()
mock_handler.completion.return_value = "No code to run."
mock_handler.get_usage_summary.return_value = UsageSummary(
model_usage_summaries={
"test": ModelUsageSummary(
total_calls=1, total_input_tokens=0, total_output_tokens=0, total_cost=0.05
)
}
)
mock_env = Mock()
# _completion_turn calls _update_handler_cost → adds $0.05 to _cumulative_cost
iteration = rlm_inst._completion_turn(
prompt=[{"role": "user", "content": "test"}],
lm_handler=mock_handler,
environment=mock_env,
)
assert rlm_inst._cumulative_cost == 0.05
with pytest.raises(BudgetExceededError) as exc_info:
rlm_inst._check_iteration_limits(iteration, 0, mock_handler)
assert exc_info.value.spent == 0.05
assert exc_info.value.budget == 0.01
def test_budget_includes_child_cost_after_iteration(self):
"""Regression: _cumulative_cost must include both handler and child subcall costs.
Exercises the real flow: _completion_turn calls _update_handler_cost
(syncing handler cost delta into _cumulative_cost), then executes code
blocks where _subcall adds child cost to _cumulative_cost.
_check_iteration_limits should see the accumulated total.
"""
from rlm.core.types import REPLResult
rlm_inst = RLM(
backend="openai",
backend_kwargs={"model_name": "test"},
max_budget=5.0,
)
# Mock handler: completion returns a response with a code block,
# handler spent $1.0
mock_handler = Mock()
mock_handler.completion.return_value = (
"Running subcall\n```repl\nrlm_query('hello')\n```"
)
mock_handler.get_usage_summary.return_value = UsageSummary(
model_usage_summaries={
"test": ModelUsageSummary(
total_calls=1, total_input_tokens=0, total_output_tokens=0, total_cost=1.0
)
}
)
# Mock environment: execute_code simulates _subcall adding $9 child cost
mock_env = Mock()
def execute_with_child_cost(code_str):
rlm_inst._cumulative_cost += 9.0
return REPLResult(stdout="", stderr="", locals={})
mock_env.execute_code.side_effect = execute_with_child_cost
# _completion_turn:
# 1. lm_handler.completion() → response with code block
# 2. _update_handler_cost() → adds $1 handler delta
# 3. execute_code() → child adds $9 via side effect
iteration = rlm_inst._completion_turn(
prompt=[{"role": "user", "content": "test"}],
lm_handler=mock_handler,
environment=mock_env,
)
# Total: $1 (handler) + $9 (child) = $10 > $5 budget
assert rlm_inst._cumulative_cost == 10.0
with pytest.raises(BudgetExceededError) as exc_info:
rlm_inst._check_iteration_limits(iteration, 0, mock_handler)
assert exc_info.value.spent == 10.0
assert exc_info.value.budget == 5.0
def test_fallback_lm_cost_tracked(self):
"""At max_depth, _subcall does a plain LM completion. Its cost must
still be added to _cumulative_cost for budget tracking."""
rlm_inst = RLM(
backend="openai",
backend_kwargs={"model_name": "test"},
max_budget=5.0,
max_depth=1, # next_depth=1 >= 1 → fallback path
)
assert rlm_inst._cumulative_cost == 0.0
# Mock get_client to return a client that reports $3 cost
mock_client = Mock()
mock_client.model_name = "test"
mock_client.completion.return_value = "fallback answer"
mock_client.get_last_usage.return_value = ModelUsageSummary(
total_calls=1, total_input_tokens=100, total_output_tokens=50, total_cost=3.0
)
with patch("rlm.core.rlm.get_client", return_value=mock_client):
result = rlm_inst._subcall("test prompt")
assert result.response == "fallback answer"
assert result.usage_summary.total_cost == 3.0
# Fallback LM cost must be tracked in parent's _cumulative_cost
assert rlm_inst._cumulative_cost == 3.0
def test_token_limit_check_raises(self):
"""_check_iteration_limits should raise TokenLimitExceededError when tokens exceeded."""
from rlm.core.types import RLMIteration
rlm = RLM(
backend="openai",
backend_kwargs={"model_name": "test"},
max_tokens=100,
)
mock_handler = Mock()
mock_handler.get_usage_summary.return_value = UsageSummary(
model_usage_summaries={
"test": ModelUsageSummary(
total_calls=1,
total_input_tokens=80,
total_output_tokens=80,
)
}
)
iteration = RLMIteration(prompt="test", response="code", code_blocks=[])
with pytest.raises(TokenLimitExceededError) as exc_info:
rlm._check_iteration_limits(iteration, 0, mock_handler)
assert exc_info.value.tokens_used == 160
assert exc_info.value.token_limit == 100
class TestDepth1LoggerMetadata:
"""Verify depth=1 logger metadata is captured correctly."""
def test_completion_returns_metadata_with_logger(self):
"""When logger is provided, completion result should have metadata."""
with patch.object(rlm_module, "get_client") as mock_get_client:
mock_lm = create_mock_lm(["FINAL(42)"])
mock_get_client.return_value = mock_lm
logger = RLMLogger()
rlm = RLM(
backend="openai",
backend_kwargs={"model_name": "test-model"},
max_depth=1,
logger=logger,
)
result = rlm.completion("What is the answer?")
assert result.metadata is not None
assert "run_metadata" in result.metadata
assert "iterations" in result.metadata
assert len(result.metadata["iterations"]) == 1
assert result.metadata["run_metadata"]["root_model"] == "test-model"
def test_completion_returns_no_metadata_without_logger(self):
"""When no logger is provided, metadata should be None."""
with patch.object(rlm_module, "get_client") as mock_get_client:
mock_lm = create_mock_lm(["FINAL(42)"])
mock_get_client.return_value = mock_lm
rlm = RLM(
backend="openai",
backend_kwargs={"model_name": "test-model"},
max_depth=1,
)
result = rlm.completion("What is the answer?")
assert result.metadata is None
def test_metadata_has_multiple_iterations(self):
"""Logger should capture all iterations."""
with patch.object(rlm_module, "get_client") as mock_get_client:
mock_lm = create_mock_lm(
[
"Let me compute.\n```repl\nx = 1\n```",
"More work.\n```repl\ny = 2\n```",
"FINAL(done)",
]
)
mock_get_client.return_value = mock_lm
logger = RLMLogger()
rlm = RLM(
backend="openai",
backend_kwargs={"model_name": "test-model"},
max_depth=1,
logger=logger,
)
result = rlm.completion("compute")
assert result.metadata is not None
assert len(result.metadata["iterations"]) == 3
# ========================================================================
# depth>1 tests: verify subcall metadata propagation
# ========================================================================
class TestSubcallLoggerPropagation:
"""Verify child RLM gets a logger when parent has one, and metadata flows back."""
def test_child_gets_logger_when_parent_has_logger(self):
"""When parent has a logger, child RLM should also get a logger."""
captured_child_params = {}
original_rlm_class = rlm_module.RLM
class CapturingRLM(original_rlm_class):
def __init__(self, *args, **kwargs):
captured_child_params.update(kwargs)
super().__init__(*args, **kwargs)
with patch.object(rlm_module, "get_client") as mock_get_client:
mock_lm = create_mock_lm(["FINAL(answer)"])
mock_get_client.return_value = mock_lm
logger = RLMLogger()
parent = RLM(
backend="openai",
backend_kwargs={"model_name": "parent-model"},
max_depth=3,
logger=logger,
)
with patch.object(rlm_module, "RLM", CapturingRLM):
parent._subcall("test prompt")
# Child should have received a logger
child_logger = captured_child_params.get("logger")
assert child_logger is not None
assert isinstance(child_logger, RLMLogger)
# But it should be a DIFFERENT instance from parent's logger
assert child_logger is not logger
parent.close()
def test_child_gets_no_logger_when_parent_has_none(self):
"""When parent has no logger, child should also get None."""
captured_child_params = {}
original_rlm_class = rlm_module.RLM
class CapturingRLM(original_rlm_class):
def __init__(self, *args, **kwargs):
captured_child_params.update(kwargs)
super().__init__(*args, **kwargs)
with patch.object(rlm_module, "get_client") as mock_get_client:
mock_lm = create_mock_lm(["FINAL(answer)"])
mock_get_client.return_value = mock_lm
parent = RLM(
backend="openai",
backend_kwargs={"model_name": "parent-model"},
max_depth=3,
logger=None,
)
with patch.object(rlm_module, "RLM", CapturingRLM):
parent._subcall("test prompt")
assert captured_child_params.get("logger") is None
parent.close()
def test_leaf_subcall_returns_no_metadata(self):
"""At max_depth (leaf), subcall returns plain LM completion with no metadata."""
with patch.object(rlm_module, "get_client") as mock_get_client:
mock_lm = create_mock_lm(["leaf response"] * 3)
mock_get_client.return_value = mock_lm
parent = RLM(
backend="openai",
backend_kwargs={"model_name": "parent-model"},
depth=1,
max_depth=2, # next_depth=2 >= max_depth=2 → leaf
logger=RLMLogger(),
)
result = parent._subcall("test prompt")
# Leaf completions don't use RLM, so no metadata
assert result.metadata is None
assert result.response == "leaf response"
parent.close()
def test_subcall_metadata_has_trajectory(self):
"""When child RLM completes with a logger, the returned RLMChatCompletion should have metadata."""
with patch.object(rlm_module, "get_client") as mock_get_client:
# Need enough responses: parent init + child init + child completion
mock_lm = create_mock_lm(["FINAL(child answer)"] * 5, model_name="test-model")
mock_get_client.return_value = mock_lm
parent = RLM(
backend="openai",
backend_kwargs={"model_name": "test-model"},
depth=0,
max_depth=3, # Allows child at depth=1 to have its own REPL
logger=RLMLogger(),
)
result = parent._subcall("What is 2+2?")
# Child should have returned metadata with trajectory
assert result.metadata is not None
assert "run_metadata" in result.metadata
assert "iterations" in result.metadata
assert len(result.metadata["iterations"]) >= 1
parent.close()
class TestSubcallCustomToolsPropagation:
"""Verify custom_tools propagation to child RLM in _subcall."""
def test_sub_tools_propagated_to_child(self):
"""Child should receive parent's custom_sub_tools as its custom_tools."""
captured_child_params = {}
original_rlm_class = rlm_module.RLM
class CapturingRLM(original_rlm_class):
def __init__(self, *args, **kwargs):
captured_child_params.update(kwargs)
super().__init__(*args, **kwargs)
with patch.object(rlm_module, "get_client") as mock_get_client:
mock_lm = create_mock_lm(["FINAL(answer)"])
mock_get_client.return_value = mock_lm
my_tool = lambda x: x * 2 # noqa: E731
parent = RLM(
backend="openai",
backend_kwargs={"model_name": "test-model"},
max_depth=3,
custom_tools={"double": my_tool},
custom_sub_tools={"double": my_tool},
)
with patch.object(rlm_module, "RLM", CapturingRLM):
parent._subcall("test prompt")
assert "double" in captured_child_params.get("custom_tools", {})
assert "double" in captured_child_params.get("custom_sub_tools", {})
parent.close()
def test_empty_sub_tools_propagated(self):
"""When custom_sub_tools is empty dict, child should get empty dict (no tools)."""
captured_child_params = {}
original_rlm_class = rlm_module.RLM
class CapturingRLM(original_rlm_class):
def __init__(self, *args, **kwargs):
captured_child_params.update(kwargs)
super().__init__(*args, **kwargs)
with patch.object(rlm_module, "get_client") as mock_get_client:
mock_lm = create_mock_lm(["FINAL(answer)"])
mock_get_client.return_value = mock_lm
parent = RLM(
backend="openai",
backend_kwargs={"model_name": "test-model"},
max_depth=3,
custom_tools={"tool": lambda: 1},
custom_sub_tools={}, # Explicitly no tools for children
)
with patch.object(rlm_module, "RLM", CapturingRLM):
parent._subcall("test prompt")
assert captured_child_params.get("custom_tools") == {}
assert captured_child_params.get("custom_sub_tools") == {}
parent.close()