-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_extractor.py
More file actions
193 lines (166 loc) · 6.95 KB
/
Copy pathtest_extractor.py
File metadata and controls
193 lines (166 loc) · 6.95 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
"""
tests/test_extractor.py — Quick smoke tests for the S(t) Extractor + RPR
Run:
python -m pytest tests/
or just:
python tests/test_extractor.py
"""
from __future__ import annotations
import os
import sys
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
from lars.llm import MockLLM
from lars.extractor import StateExtractor
from lars.metrics import rpr
from lars.state import StateVector, ReasoningStep, Decision
def make_state(**overrides) -> StateVector:
"""Build a StateVector with sensible defaults for testing."""
base = dict(
goal="test goal",
steps_completed=[
ReasoningStep(step_id=1, description="step 1", status="completed", dependencies=[]),
ReasoningStep(step_id=2, description="step 2", status="completed", dependencies=[1]),
],
steps_pending=[
ReasoningStep(step_id=3, description="step 3", status="pending", dependencies=[2]),
],
assumptions=["assumption A"],
decisions=[Decision(decision="choice X", rationale="because")],
confidence=0.7,
)
base.update(overrides)
return StateVector(**base)
def test_extractor_with_mock():
"""Extractor produces a valid StateVector with the MockLLM."""
llm = MockLLM(fixtures={
"GOAL": {
"goal": "do X",
"steps_completed": [
{"step_id": 1, "description": "first step", "status": "completed", "dependencies": []}
],
"steps_pending": [
{"step_id": 2, "description": "second step", "status": "pending", "dependencies": [1]}
],
"assumptions": [],
"decisions": [],
"confidence": 0.6,
}
})
extractor = StateExtractor(llm)
sv = extractor.extract("do X", "Step 1: first step. Now starting step 2.")
assert isinstance(sv, StateVector)
assert sv.goal == "do X"
assert len(sv.steps_completed) == 1
assert len(sv.steps_pending) == 1
assert sv.raw_cot is not None # the extractor should preserve raw CoT
print("✓ test_extractor_with_mock")
def test_rpr_identical_states():
"""RPR of a state with itself is 1.0."""
s = make_state()
assert rpr(s, s) == 1.0
print("✓ test_rpr_identical_states")
def test_rpr_total_loss():
"""RPR is 0.0 when the new state shares nothing with the old."""
s_old = StateVector(
goal="old goal",
steps_completed=[ReasoningStep(step_id=1, description="unique-old-content-xyz", status="completed", dependencies=[])],
steps_pending=[],
assumptions=["unique-old-assumption-xyz"],
decisions=[Decision(decision="unique-old-decision-xyz", rationale="unique-rationale-xyz")],
confidence=0.5,
)
s_new = StateVector(
goal="new goal",
steps_completed=[ReasoningStep(step_id=1, description="totally-different-text-abc", status="completed", dependencies=[])],
steps_pending=[],
assumptions=["unique-new-assumption-abc"],
decisions=[Decision(decision="unique-new-decision-abc", rationale="unique-rationale-abc")],
confidence=0.5,
)
# exact mode: 0.0
assert rpr(s_old, s_new, match="exact") == 0.0
print("✓ test_rpr_total_loss")
def test_rpr_jaccard_catches_paraphrase():
"""Jaccard mode is robust to minor rewording."""
s_old = StateVector(
goal="test",
steps_completed=[ReasoningStep(step_id=1, description="Analyze the market for fitness apps in Egypt", status="completed", dependencies=[])],
steps_pending=[],
assumptions=[],
decisions=[],
confidence=0.5,
)
s_new = StateVector(
goal="test",
steps_completed=[ReasoningStep(step_id=1, description="Analyze the market for fitness apps in Cairo", status="completed", dependencies=[])],
steps_pending=[],
assumptions=[],
decisions=[],
confidence=0.5,
)
# exact: 0 (the only string differs)
assert rpr(s_old, s_new, match="exact") == 0.0
# jaccard: should be > 0 (heavy token overlap)
score = rpr(s_old, s_new, match="jaccard", threshold=0.3)
assert score > 0.5, f"expected high jaccard preservation, got {score}"
print(f"✓ test_rpr_jaccard_catches_paraphrase (RPR={score})")
def test_state_vector_bumped():
"""bumped() returns a copy with version+1."""
s = make_state()
s2 = s.bumped()
assert s2.version == s.version + 1
assert s2.goal == s.goal
print("✓ test_state_vector_bumped")
def test_state_vector_summary():
"""summary() is a one-liner with key info."""
s = make_state()
line = s.summary()
assert "goal=" in line
assert "conf=" in line
print(f"✓ test_state_vector_summary ('{line[:60]}...')")
def test_extractor_refresh_pending_updates_descriptions():
"""
v0.5.0: refresh_pending() should rewrite pending step descriptions
to include the new keywords from the latest interrupt. Fails soft
if the LLM doesn't return valid JSON (returns s unchanged).
"""
s = StateVector(
goal="Marketing plan for fitness app in Egypt",
steps_completed=[
ReasoningStep(step_id=1, description="Analyze market", status="completed", dependencies=[]),
],
steps_pending=[
ReasoningStep(step_id=2, description="Choose channels.", latest_cot=None, dependencies=[]),
ReasoningStep(step_id=3, description="Allocate budget.", latest_cot=None, dependencies=[]),
],
assumptions=["Egypt is the target market"],
decisions=[],
confidence=0.5,
)
# Use a mock LLM that returns a valid _RefreshResp
from lars.llm import LLMClient
from lars.extractor import _RefreshResp, _RefreshUpdate
class _StubLLM(LLMClient):
def complete_json(self, system, user, schema):
return _RefreshResp(updates=[
_RefreshUpdate(step_id=2, new_description="Choose marketing channels in Cairo."),
_RefreshUpdate(step_id=3, new_description="Allocate budget for Cairo campaign."),
])
e = StateExtractor(_StubLLM())
s_new = e.refresh_pending(s, "scope_narrow", "Cairo")
assert s_new.version == s.version + 1, "Version should increment after refresh"
assert len(s_new.steps_pending) == len(s.steps_pending), "No steps should be added/dropped"
step2 = next(p for p in s_new.steps_pending if p.step_id == 2)
assert "Cairo" in step2.description, f"Step 2 should mention Cairo: {step2.description}"
step3 = next(p for p in s_new.steps_pending if p.step_id == 3)
assert "Cairo" in step3.description, f"Step 3 should mention Cairo: {step3.description}"
print("✓ test_extractor_refresh_pending_updates_descriptions")
if __name__ == "__main__":
test_extractor_with_mock()
test_rpr_identical_states()
test_rpr_total_loss()
test_rpr_jaccard_catches_paraphrase()
test_state_vector_bumped()
test_state_vector_summary()
test_extractor_refresh_pending_updates_descriptions()
print("\nAll tests passed.")