test(examples): capture duplicate LLM request/response log entries bug (#173) #179
No reviewers
Labels
No labels
auto/blocked-by-deps
auto/ci-timeout
auto/claimed-implementer
auto/claimed-merge
auto/claimed-reviewer
auto/driver-down
auto/invariant-violation
auto/last-attempt-tier-0
auto/last-attempt-tier-1
auto/last-attempt-tier-2
auto/last-attempt-tier-min
Automation Tracking
auto/needs-conflict-resolution
auto/needs-implementer
auto/postmortem
auto/ready-to-merge
auto/restart-throttled
auto/revert
auto/sentinel
auto/stale-inactivity
auto/unstable
Blocked
Bounty
$100
Bounty
$1000
Bounty
$10000
Bounty
$20
Bounty
$2000
Bounty
$250
Bounty
$50
Bounty
$500
Bounty
$5000
Bounty
$750
MoSCoW
Could have
MoSCoW
Must have
MoSCoW
Should have
Needs Feedback
Points
1
Points
13
Points
2
Points
21
Points
3
Points
34
Points
5
Points
55
Points
8
Points
88
Priority
Backlog
Priority
CI Blocker
Priority
Critical
Priority
High
Priority
Low
Priority
Medium
Signed-off: Owner
Signed-off: Scrum Master
Signed-off: Tech Lead
Spike
State
Completed
State
Duplicate
State
In Progress
State
In Review
State
Paused
State
Unverified
State
Verified
State
Wont Do
Type
Automation
Type
Bug
Type
Discussion
Type
Documentation
Type
Epic
Type
Feature
Type
Legendary
Type
Refactor
Type
Support
Type
Task
Type
Testing
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Blocks
Reference
cleveragents/cleveractors-core!179
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "tdd/m1-llm-request-log-duplication"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
TDD issue-capture test for bug #173:
examples/test_app.py's_setup_request_logging()monkey-patchesLLMAgent._ensure_chat_modelwith_patched_ensure_chat_model, which re-wrapschat_model.ainvoke/astreamon every call with no per-instance guard. Each call to_ensure_chat_model()stacks a fresh logging layer on top of any previous one, so the Nth call on the same chat model produces N nested wrappers and one real LLM call ends up logged N times inllm_requests.log.Adds
features/request_logging_wrapper.feature/features/steps/request_logging_wrapper_steps.py, a new pair (no existing step file coversexamples/test_app.py's logging patch). The scenario is tagged@tdd_issue @tdd_issue_173 @tdd_expected_fail: it installs the real patch against anLLMAgentwired to aFakeBackendChatModel(no real network call), invokes the patched_ensure_chat_model()twice on the same instance, callsainvoke()once, and asserts exactly oneLLM REQUEST/LLM RESPONSElog line pair is recorded. The assertion fails on the current (unfixed) code — two layers produce two of each — so CI passes only via the@tdd_expected_failinversion until the real fix (#173) lands.Per the project's mandatory Bug Fix TDD Workflow, this TDD test must merge before the fix PR for #173.
Test plan
nox -s unit_tests— 160 features, 3327 scenarios, 0 failed (new scenario included, passes via@tdd_expected_failinversion)nox -s coverage_report— COVERAGE OK: 97.2% (threshold: 96.5%)nox -s lint— cleannox -s format -- --check— cleannox -s typecheck— 0 errorsnox -s security_scan— no issuesnox -s dead_code— no issuesCloses #174
PR Review: !179 (Ticket #174)
Verdict: Approve
This is a test-only TDD capture PR for bug #173, and it fully satisfies ticket #174's acceptance criteria. The bug reproduction was verified locally: the
Thenstep's assertion fails on unfixed code with exactly the expected evidence (got 2log pairs for oneainvokecall), and the@tdd_expected_failinversion flips it to a pass. A full localnox -s unit_testsrun is green (3327 scenarios, 0 failed — matching the PR's claim exactly), andnox -s lint/nox -s typecheckpass. No critical or major issues found.Critical Issues
None
Major Issues
None
Minor Issues
_restore—features/steps/request_logging_wrapper_steps.py:75-81_setup_request_logging()(examples/test_app.py:182, 208, 223, 97) mutates five pieces of global state, but_restoreonly undoes three: it restoresLLMAgent._ensure_chat_model, the_request_logging_configuredflag, and logger handlers — but notLLMAgent._run_pruning_pass, notToolAgent.process_message(both stay logging-wrapped for the rest of the Behave process), and not thecleveractors.requestslogger level (stays atDEBUG). The PR description's claim that "nothing leaks into later scenarios" is therefore overstated.Impact today: zero — the leaked wrappers are pass-throughs that only log, and after cleanup the logger has no handlers so INFO records go nowhere; the full suite (including all scenarios alphabetically after this one) runs green. It remains a latent hygiene hazard for future tests that assert on that logger or on method identity, and the PR body shouldn't claim complete cleanup.
Recommendation: capture and restore
LLMAgent._run_pruning_pass,ToolAgent.process_message, and the logger's original level in_restore(ideally in the same commit or a fast-follow before merge).Nits
context.invocation_responsecaptured but never asserted —features/steps/request_logging_wrapper_steps.py:113The captured response is unused. Asserting e.g.
context.invocation_response.content == "duplicate-logging-fake"in theThenstep would explicitly prove the wrapper chain is a pass-through returning the real fake result (currently only implied by the RESPONSE log lines being emitted).Summary
Spec compliance / ticket coverage — all acceptance criteria of ticket #174 are met: scenario tagged
@tdd_issue @tdd_issue_173 @tdd_expected_fail✓; assertions use bareassert/AssertionErroronly ✓; the scenario fails on unfixed code and passes CI only via inversion ✓ (verified live);nox -s unit_testsgreen ✓; no new mocks outsidefeatures/mocks/✓ (reusesFakeBackendChatModel, so the "add a fake if new" subtask was correctly unnecessary). The bug-issue dependency required by the DoD exists (#173 is blocked-by #174) ✓. Commit message first line matches the issue's Metadata exactly and the footer references #174; branch name matches Metadata; single atomic commit, no merge commits.Correctness — the scenario faithfully reproduces the reported mechanism: patch installed →
chat_modelinjected via the property setter → patched_ensure_chat_model()invoked twice stacks wrapper₂(wrapper₁(real)) → oneainvokeyields 2 REQUEST + 2 RESPONSE records →== 1assertions fail. Substring counting is safe ("LLM STREAM REQUEST"does not contain"LLM REQUEST"), and onlyainvokeis exercised so no STREAM lines exist anyway. No step-text collisions with existing step files;context.scenario_tempis always created inbefore_scenario;context._active_patchescleanup calls.stop(), compatible with theSimpleNamespace(stop=_restore)registration. The temporaryos.chdiris correctly restored infinally, and the FileHandler created insidescenario_tempis removed and closed, so nollm_requests.logleaks into the repo root.Tests — the new feature/step pair follows the step-file naming rule (
foo.feature↔foo_steps.py) and is the first coverage ofexamples/test_app.py's logging patch, as claimed. Coverage is unaffected by construction ([tool.coverage] source issrconly); the PR's reported 97.2% ≥ 96.5% threshold stands. TheAsyncMockRuntimeWarning visible at the end of the full-suite run also occurs with pre-existing scenarios and did not appear in an isolated run of the new feature, so it is not attributable to this PR.PR-discussion check — the PR has no comments, reviews, or review threads yet, and neither #174 nor #173 records any decision marking an item out-of-scope or deferred, so nothing was excluded on those grounds. Per reviewer instructions, red-tape items (labels, base-branch workflow choice —
developis where recent PRs #172/#169 merged) were excluded from this review.Verification performed locally:
nox -s unit_tests(full suite: 3327 scenarios, 0 failed), isolated run offeatures/request_logging_wrapper.feature(assertion correctly fails withgot 2, inverted to pass),nox -s lint(clean),nox -s typecheck(0 errors).@ -0,0 +72,4 @@and handler not in pre_existing_file_handlers]def _restore() -> None:Minor:
_setup_request_logging()also patchesLLMAgent._run_pruning_passandToolAgent.process_message(test_app.py:208/223) and sets thecleveractors.requestslogger to DEBUG (line 97), none of which are restored here. Impact today is zero (pass-through wrappers, no handlers after cleanup — full suite verified green), but they leak for the rest of the Behave process and the PR body's "nothing leaks into later scenarios" claim is overstated. Suggest restoring these too.@ -0,0 +110,4 @@async def _run() -> Any:return await context.fake_chat_model.ainvoke([HumanMessage(content="hi")])context.invocation_response = asyncio.run(_run())Nit:
context.invocation_responseis captured but never asserted. Assertingcontext.invocation_response.content == "duplicate-logging-fake"in the Then step would explicitly prove the stacked wrapper chain is a pass-through returning the real fake result.dbcea1025ad7936cdd84@hurui200320 Thanks for the review (#9876) — both findings checked out and are addressed in the latest push (
d7936cd). Before applying anything, I cross-checked both againstdocs/index.md, thedocs/adr/*ADRs, and issues #173/#174: none of them coverexamples/test_app.pyor this test harness (it's example/tooling code, explicitly out of core-spec scope per #173's own "Supporting information" section), so there was no specification conflict to resolve either way — both were purely test-hygiene findings and both are correct as written.Minor — incomplete global-state restore (addressed)
Confirmed by reading
_setup_request_logging()directly: it mutates five pieces of global state, and_restore()only undid three. Fixed infeatures/steps/request_logging_wrapper_steps.py'sstep_install_request_logging_patch—_restore()now also captures and restores:LLMAgent._run_pruning_pass(back to the pre-patch original)ToolAgent.process_message(back to the pre-patch original)cleveractors.requestslogger's level (back to whatever it was beforesetLevel(logging.DEBUG))alongside the three it already handled (
LLMAgent._ensure_chat_model,_request_logging_configured, the addedFileHandlers). The PR body's "nothing leaks into later scenarios" claim is now actually true rather than overstated.Nit — unasserted
invocation_response(addressed)Added a new
Thenstep,the agent's chat model invocation should return the fake backend's response, assertingcontext.invocation_response.content == "duplicate-logging-fake"— wired into the scenario infeatures/request_logging_wrapper.featureright after the existing log-count assertion, exactly as suggested. One transparency note: in the current (unfixed) state this new step doesn't actually execute yet, because the preceding log-count assertion still fails first and Behave skips the remaining steps in the scenario — the suite still passes overall only via the@tdd_expected_failinversion. The assertion is correctly wired and will exercise for real once the actual fix lands onbugfix/m1-llm-request-log-duplicationand the preceding step starts passing.Not changed
Nothing from this review was skipped or rejected — both findings were accepted as-is, with no spec objection raised against either.
Verification
nox -s lint— cleannox -s typecheck— 0 errorsnox -s unit_tests— 160 features / 3351 scenarios / 15424 steps passed, 0 failed. The TDD scenario still fails its underlying assertion pre-fix (got 2) exactly as before, confirming the fix didn't accidentally paper over the bug it's supposed to prove.features/steps/request_logging_wrapper_steps.py+ the.featurefile), and[tool.coverage]scopes Slipcover tosrconly, so there is no production coverage delta from this change.CHANGELOG.mdentry added — this is a test-only commit and the project's changelog rule exempts tests.The local commit was amended in place (no new commit; this branch carries a single atomic commit) and force-pushed. PR head is now
d7936cd.