fix: audit records <stream> placeholder for streamed bodies (#4)#8
Conversation
- Add comprehensive test cases for stream scenarios - Implement _safe_get_request_body() for request stream detection - Implement _safe_get_response_body() for response stream detection - Return '<stream>' placeholder for file-like objects, generators, and streamed responses - Preserve existing behavior for strings and bytes - Follows Option 1 (Skip/Empty approach) from issue analysis Fixes #4 Co-authored-by: Henrique Bastos <henriquebastos@users.noreply.github.com>
The original fix keyed response streaming off response.raw.isclosed(), which is False for ordinary responses and produced a false <stream>. Use the stream kwarg that Audit.send already receives from Session.send instead, and emit <stream> only when stream=True. The file-like request test relied on `responses`, which reads the BytesIO into a string before audit sees it; replace it with a canned adapter that leaves request.body untouched. Conform the 2025 code to the current lint rules.
Move issue #4 to Done, mark the completed branch cleanup, refresh Current Focus/Active now that all pending fixes have landed, and flip the audit-stream decision to landed with its detection mechanism.
|
Claude finished @henriquebastos's task —— View job Pull Request Review Complete ✅Review Checklist:
🎯 Overall Assessment: EXCELLENTThis PR demonstrates exceptional code quality and engineering practices. The implementation is clean, well-tested, and thoroughly documented. I found no significant issues and several commendable practices. 📋 Code Quality & Best PracticesStrengths:
Minor observations:
🐛 Bugs & Issues: NONE FOUNDThe implementation correctly handles all stream detection scenarios:
The original bug with 🚀 Performance ConsiderationsExcellent performance design:
The 🔒 Security: NO CONCERNS
🧪 Test Coverage: COMPREHENSIVEThe test suite in New tests cover all scenarios:
Testing innovation: The 📚 Documentation QualityExcellent documentation updates:
🔧 Minor Suggestions
🎉 Commendable Practices
Recommendation: ✅ APPROVE This PR is ready to merge. It successfully solves the streaming audit problem with an elegant, well-tested solution that maintains backwards compatibility and follows excellent engineering practices. |
Why
Closes #4.
Audit.audit()materialized every request/response body (request.body/response.text). For streamed bodies (file-like/generator uploads, orstream=Truedownloads) that either crashes or defeats streaming by loading everything into memory. Audit must never break or distort the request it observes.What
Records a
<stream>placeholder instead of materializing streamed bodies (decision recorded indecisions.md, option 1).Builds on the original 2025 fix, reworked so it actually passes its tests on current
main:streamkwargAudit.sendalready receives fromSession.send— the originalresponse.raw.isclosed()sniff wasFalsefor ordinary responses and produced a false<stream>.responses, which reads theBytesIOinto a string before audit sees it — replaced with a small canned adapter that leavesrequest.bodyintact.streammade keyword-only for FBT002).Folds Ariad docs: roadmap → Done (incl. completed branch cleanup),
decisions.md→ landed.Validation
pytest: 50 passed locally (all 6TestAuditStreamsgreen); CI runs lint + py3.10–3.13.pre-commit(ruff 0.11.9) clean.