Skip to content

fix(memories): sanitize internal exception detail in batch-delete 413 response - #19520

Merged
kodjima33 merged 2 commits into
BasedHardware:mainfrom
1132dadf:fix/memory-batch-delete-sanitize
Sep 28, 2026
Merged

kodjima33 merged 2 commits into
BasedHardware:mainfrom
1132dadf:fix/memory-batch-delete-sanitize

Conversation

@1132dadf

@1132dadf 1132dadf commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

What changed and why

MemoryService.delete_batch (backing DELETE /v3/memories/batch) mapped an over-limit canonical batch to HTTPException(413, detail=str(exc)) — the only raw exception reflection left among the file's ~120 HTTPException raises. The data layer's internal reason (internal memory id + Firestore transaction-limit wording) was returned verbatim to the API client.

The mapping now logs the reason server-side through utils.log_sanitizer.sanitize with exc_info=True, and answers with a static detail (Memory batch exceeds the supported size limit) while preserving the released 413 status. The internal exception remains chained (from exc).

Bounty proposal: #19519

Product invariants affected

  • INV-MEM-1 — no new tier, collection, or access-policy surface; the change rewrites one error mapping in the canonical batch-delete path.
  • INV-MEM-3 — oversized batches still fail closed with 413; the internal reason is now logged sanitized server-side instead of reflected to the client.
  • INV-MEM-4 — no intake, consolidation, promotion, or authority path is touched; only the batch-delete error mapping changes.

How it was verified

  • python -m pytest tests/unit/test_universal_memory_service.py -q -> 107 passed (includes the new regression test)
  • python -m pytest tests/unit/test_memories_batch_delete.py -q -> 11 passed (router contract: 402/404/413 mapping, rate limit, and route order preserved)
  • python -m pytest tests/unit/test_atomicity_lifecycle_regressions.py -q -> 22 passed
  • black --line-length 120 --skip-string-normalization --check <files> -> unchanged

Tests

New regression test test_delete_batch_limit_error_sanitizes_detail_and_preserves_413 in tests/unit/test_universal_memory_service.py: drives the real service path with an injected CanonicalBatchMutationLimitError that carries an internal memory id and Firestore wording; asserts status 413, the exact static detail, no internal markers in the response, a masked log line, and the chained cause.

Failure class (fixes)

Failure-Class: none
Line-Count-Exception: backend/utils/memory/memory_service.py | 4334 -> 4342 | Sanitize the batch-delete 413 detail: static client message, sanitized server-side log with exc_info, and chained cause; +8 lines, no restructuring.

CI note: a trailing empty commit re-runs the code lane after this description fixed the product-invariants citations and the line-count ratchet exception; the earlier Hygiene failure was that pre-fix run.

Review in cubic

The canonical batch mutation limit error surfaced raw str(exc) as the 413
detail, the only non-static HTTPException detail left in memory_service.py.
The internal reason (memory id + data-layer transaction limits) is now
logged server-side through utils.log_sanitizer.sanitize with exc_info, and
the client keeps the released 413 status with a static message.
@Git-on-my-level

Copy link
Copy Markdown
Member

Thank you for this clean, well-scoped fix — verified against the bounty description (#19519, still open) and the current head.

  • backend/utils/memory/memory_service.py — the CanonicalBatchMutationLimitError handler in delete_batch is the one place I confirmed (against main) that still did HTTPException(413, detail=str(exc)) among the file's ~120 raises. The new mapping is exactly right: the internal reason goes to the server-side log through utils.log_sanitizer.sanitize with exc_info=True (so the full cause chain stays debuggable in logs), while the client gets a static detail with the released 413 status preserved and from exc chaining intact. Importing sanitize alongside the existing sanitize_validation_error follows the file's convention.
  • backend/tests/unit/test_universal_memory_service.py — test_delete_batch_limit_error_sanitizes_detail_and_preserves_413 drives the real service path with an injected error carrying an internal-id marker, then asserts the exact static detail, no marker or "Firestore" leakage into the response, the exception chain, and exactly one masked log call. That covers the regression the bounty asked for.

I also checked for consumers of the previous dynamic wording and found none — the status code is unchanged and the router contract tests pass the service error through generically, so this is contract-safe. Backend checks are green at this head.

Nice catch on the last raw exception reflection in this path. Leaving the merge sign-off to a maintainer given this touches a user-data API error surface.


Feedback generated by AI on behalf of the maintainers. Conclusions verified against the diff and CI logs at head a6ed92b.


by AI on behalf of David — if you need David’s attention urgently, please @Git-on-my-level and escalate with need human response.

@Git-on-my-level Git-on-my-level added backend Backend Task (python) positive-signal Automation verified a genuine fix/quality contribution labels Sep 28, 2026

@kodjima33 kodjima33 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix(backend): sanitizes leaked internal exception detail in batch-delete 413 response; scoped diff, regression test added, no already-fixed conflict.

@kodjima33
kodjima33 merged commit 938141a into BasedHardware:main Sep 28, 2026
51 of 52 checks passed
@1132dadf

Copy link
Copy Markdown
Contributor Author

Hi
Thanks again for reviewing and merging my PR.
Just a quick note regarding the payment/sponsorship: The previous PayPal account I provided was a Mainland China region account, which unfortunately has strict cross-border restrictions and cannot receive international personal transactions.
To make it easier for you, I have set up a new payment link via Ko-fi. You can now complete the payment using your Credit Card or an international PayPal account at:
👉 https://ko-fi.com/fengyuan_cn
Please kindly process the transfer from this new link. Let me know if you encounter any issues during the checkout.
Thanks for your support and time!
Best regards
FengYuan
If you prefer other payment methods, please let me know and we can figure it out.God bless you for your help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend Backend Task (python) positive-signal Automation verified a genuine fix/quality contribution

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants