fix(examples): wrap chat_model ainvoke/astream logging exactly once per instance #186
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 project
No assignees
2 participants
Notifications
Due date
No due date set.
Blocks
Reference
cleveragents/cleveractors-core!186
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "bugfix/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
Fixes #173:
examples/test_app.py's_setup_request_logging()AOP patch re-wrappedchat_model.ainvoke/astreamon every_ensure_chat_model()call instead of once per chat model instance. BecauseLLMAgent.chat_modelre-invokes_ensure_chat_model()on every access (not just once perLLMAgent), each real LLM call in a session stacked another logging layer, so the Nth real call was logged N times inllm_requests.log-- a growing multiplier that made the log actively misleading, with no change to actual outbound API calls or token spend.An instance-level guard (
_request_logging_wrapped, set viaobject.__setattr__the same wayainvoke/astreamalready are) ensures eachchat_modelinstance is wrapped exactly once, regardless of how many times_ensure_chat_model()subsequently runs on the owning agent.Companion TDD issue #174 (merged to
developin commitd7936cd) already added the failing-first Behave scenario tagged@tdd_issue @tdd_issue_173 @tdd_expected_fail. This PR removes@tdd_expected_failnow that the assertion passes normally, and updates the scenario/feature wording to describe the fixed behavior.Test plan
nox -s lint/nox -s format -- --check-- greennox -s typecheck-- green, zero suppressionsnox -s unit_tests-- green, including the now-passing@tdd_issue_173scenarionox -s coverage_report-- 97.3% (>= 96.5% threshold)nox -s security_scan/nox -s dead_code-- greennox -s complexity-- greennox -s integration_tests(Robot Framework) -- greennox -s build-- wheel builds successfullynox -s docs-- MkDocs builds successfullynox -s benchmark/nox -s benchmark_regressionare informational-only (CIcontinue-on-error) and not exercised meaningfully by this change (touches onlyexamples/andfeatures/, notsrc/cleveractors)Closes #173
PR Review: !186 (Ticket #173)
Verdict: Approve
Verified locally on
bugfix/m1-llm-request-log-duplication(commit807cb28):nox -s lint,nox -s typecheck(0 errors), and the full Behave suite vianox -s coverage_reportall pass, with coverage at 97.3% (threshold 96.5%) — matching the PR's claim. The@tdd_issue_173scenario now passes genuinely (6/6 steps, no inversion) after the@tdd_expected_failremoval, which is exactly the step CONTRIBUTING's Bug Fix Workflow prescribes for the fix developer. Process bookkeeping (PR basedevelopvs the ticket DoD's "PR to master" wording, labels, dependency links) is consciously excluded from this review per reviewer instruction; the author's issue comment documents thedeveloptarget explicitly.Critical Issues
None
Major Issues
None
Minor Issues
None
Nits
features/request_logging_wrapper.feature:15— The re-wrap guard is only exercised throughainvoke; no scenario drivesastreamafter two_ensure_chat_model()invocations. Ticket #173's expected behavior also covers the streaming case ("exactly oneLLM STREAM REQUEST:/LLM STREAM RESPONSE:pair"), even though its acceptance criteria only required theainvokescenario, so this is compliant. Both paths share the same early return, so this is purely an optional suite-strengthening suggestion (a mirrored streaming scenario asserting exactly oneLLM STREAM REQUEST/LLM STREAM RESPONSEpair).examples/test_app.py:121-190— The_request_logging_wrappedcheck-and-set is not synchronized: two OS threads reaching thechat_modelproperty concurrently could both pass the guard and stack two wrapper layers (bounded at 2, versus unbounded before the fix). This is unreachable in practice for the single-event-loop example CLI because the wrap section contains noawaitpoints, so it cannot interleave within one event loop; noting only for completeness. If ever hardened, holdself._chat_model_lockaround the check+set.Summary
A small, well-scoped fix that implements exactly what ticket #173 prescribes. The instance-level marker handles every chat-model lifecycle path I traced: first access wraps once; every subsequent
chat_modelproperty access short-circuits on the marker; a model re-injected via thechat_modelsetter, or re-created after cleanup (self._chat_model = NoneinLLMAgent), lacks the marker and is wrapped exactly once; and aVirtualChatModel(the outer instance cached in_chat_model) is wrapped once regardless of backend rotation, so every routed call still logs exactly once. The guard correctly runs after_original_ensure_chat_model(self), so lazy creation is unaffected, and the docstring/feature-file wording accurately describe the fixed behavior.The Behave scenario (from companion TDD PR, merged as
d7936cd) is meaningful, not tautological: it invokes the real patched class-level hook twice, then a real PydanticBaseChatModelfake once, and counts actualloggingrecords — and the TDD-capture merge proves the assertion genuinely failed on unfixed code (the stale-tag policy would have failed CI otherwise). The@tdd_expected_failremoval leaves the required@tdd_issue/@tdd_issue_173tags in place per the tag policy. The CHANGELOG entry is correctly placed under[Unreleased] > Fixedwith the issue reference. Commit hygiene is clean: single atomic commit, first line matches the ticket Metadata's Commit Message verbatim,ISSUES CLOSED: #173footer present, branch name matches Metadata, no merge commits, based on the currentdevelophead. Robot integration tests do not reference the changed files. Robot/integration green status is taken from the author's reported run, per review policy.Approved — the two nits above are non-blocking suggestions.