Skip to content

Commit 33e6342

Browse files
committed
test(core): make BulletCounter genuinely stateful via delta extraction
The previous implementation overwrote _bullet_count from the full accumulated chunk on each call — equivalent to a pure function with no real dependency on prior state. Use _seen_len to extract only the new portion of each accumulated chunk, accumulating the count additively. This genuinely requires prior-call state to know where to slice, making the test name "accumulates_state" accurate. Assisted-by: Claude Code Signed-off-by: Nigel Jones <jonesn@uk.ibm.com>
1 parent c1f3ab1 commit 33e6342

1 file changed

Lines changed: 13 additions & 6 deletions

File tree

test/core/test_stream_validate.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,24 @@ async def test_stream_validate_idempotent():
8787

8888
@pytest.mark.asyncio
8989
async def test_stateful_subclass_accumulates_state():
90-
"""Stateful subclass correctly accumulates state across stream_validate calls."""
90+
"""Stateful subclass correctly accumulates state across stream_validate calls.
91+
92+
Uses delta extraction (via _seen_len) to count only new bullet points per call —
93+
a pattern that genuinely requires state from prior calls.
94+
"""
9195

9296
class BulletCounter(Requirement):
9397
def __init__(self) -> None:
9498
super().__init__(description="no more than 3 bullets")
99+
self._seen_len = 0
95100
self._bullet_count = 0
96101

97102
async def stream_validate(
98103
self, chunk: str, *, backend: Backend, ctx: Context
99104
) -> PartialValidationResult:
100-
self._bullet_count = chunk.count("\n-")
105+
delta = chunk[self._seen_len :]
106+
self._seen_len = len(chunk)
107+
self._bullet_count += delta.count("\n-")
101108
if self._bullet_count > 3:
102109
return PartialValidationResult(
103110
"fail", reason=f"{self._bullet_count} bullets exceeds limit"
@@ -110,15 +117,15 @@ async def stream_validate(
110117
await req.stream_validate("intro text", backend=None, ctx=None) # type: ignore[arg-type]
111118
assert req._bullet_count == 0
112119

113-
await req.stream_validate("intro\n- one\n- two", backend=None, ctx=None) # type: ignore[arg-type]
114-
assert req._bullet_count == 2
120+
await req.stream_validate("intro text\n- one\n- two", backend=None, ctx=None) # type: ignore[arg-type]
121+
assert req._bullet_count == 2 # delta added 2 new bullets
115122

116123
result = await req.stream_validate(
117-
"intro\n- one\n- two\n- three\n- four",
124+
"intro text\n- one\n- two\n- three\n- four",
118125
backend=None,
119126
ctx=None, # type: ignore[arg-type]
120127
)
121-
assert req._bullet_count == 4
128+
assert req._bullet_count == 4 # delta added 2 more
122129
assert result.success == "fail"
123130
assert result.reason is not None and "4" in result.reason
124131

0 commit comments

Comments
 (0)