Repository navigation
fix(mcp): write_cache_text warns on text with a lone surrogate instead of raising - #1344
Conversation
…#1251) write_cache_text caught only OSError, so text that does not encode as UTF-8 (a lone surrogate) raised UnicodeEncodeError out of the URL, Context7 and topics fetches, although its docstring says a failed write is a warning and returns False. It now catches UnicodeEncodeError next to OSError, logs the same warning naming the path and returns False. The text is not stored rather than written with `?` for the surrogate, so the cache never replays text that differs from what was fetched. An existing entry stays as it was and no tmp file is left, because the encode fails before the rename. Signed-off-by: Anton Volkov <antonwolfy@gmail.com>
|
✅ OpenCodeReview: Review complete: 0 finding(s) across 3 selected item(s). CI at Review details
|
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1344 +/- ##
==========================================
- Coverage 96.19% 96.18% -0.01%
==========================================
Files 499 499
Lines 66465 66465
Branches 11542 11542
==========================================
- Hits 63933 63928 -5
- Misses 1563 1565 +2
- Partials 969 972 +3
🚀 New features to boost your workflow:
|
xaleryb
left a comment
There was a problem hiding this comment.
Approved on 4f1d6f0. A lone surrogate in the text or the entry path is now a warning and False with the old entry kept, a non-BMP character round-trips, and it follows the convention of the earlier surrogate writers. Tests and gates are green. Windows smoke had not started (the hosted Windows queue was stuck); every other check passed.
Summary
write_cache_text(dokimi/dokimi/mcp/utils.py) promises that a failed write is a warning and returnsFalse, but it caught onlyOSError. Onmain,write_cache_text(tmp/"k.txt", "bad \ud800 text")raisesUnicodeEncodeError: 'utf-8' codec can't encode character '\ud800' in position 4: surrogates not allowed, and a URL, Context7 or topics fetch whose text holds a lone surrogate (a\ud800from a JSON escape) fails with that traceback instead of returning what it fetched. No tmp file is left.The issue offers two fixes. This PR takes the first: catch
UnicodeEncodeErrornext toOSError, log the same warning and returnFalse. Writing throughutf8_byteswould store?for the surrogate, so the cache would replay text that differs from what was fetched, with no warning. An entry that cannot be stored faithfully is not stored, and the caller keeps what it fetched.The same cache-write contract has three callers (
mcp/context7.py:194,mcp/url_client.py:175,questions/ragas_seed.py:123); all use theFalsereturn as "not stored" and go on, so none needs a change.Closes #1251
Changes
write_cache_textcatches(OSError, UnicodeEncodeError), logsCould not write cache entry <path>: <error>and returnsFalse; the docstring says text that does not encode is not stored.tests/test_doc_caches.py: a lone-surrogate write returnsFalse, keeps the old entry, leaves no tmp file and names the path in the warning; aURLClientfetch of a page with a lone surrogate returns the text and stores nothing; a control that text withéand an emoji is stored and returnsTrue.test_failed_cache_write_keeps_the_old_entry_and_leaves_no_tmp(theOSErrorpath) is unchanged.Risk
None for text that encodes: it is stored as before, and an
OSErroris still a warning andFalse. Text with a lone surrogate was a crash and is now a warning and a cache miss on the next fetch. No doc underdokimi/docs/says the encode error is raised, so none changed.Testing
On Linux, at
4f1d6f05(onmain16738246):make ci-local: 38 passed, 0 failed, 15 skipped (the other Python legs, micromamba/conda-forge, semgrep, fuzz, docs deploy, the Intel matrix jobs and the CLI-surface package step, which need CI or tools this run lacks). 30115 tests passed, 23 skipped; changed-line coverage 100%.main's code the newtest_doc_caches.pytests fail (2 failed, 29 passed, first onUnicodeEncodeError: 'utf-8' codec can't encode character '\ud800' in position 4); with the changetest_doc_caches.pyandtest_writers_keep_group.pypass (41 passed).UnicodeEncodeErrorfrom the except clause failstest_text_with_a_lone_surrogate_is_a_warning_and_keeps_the_old_entryandtest_page_with_a_lone_surrogate_is_returned_and_stores_nothing; writing the text with the surrogate replaced by?(returningTrue) fails the same two (assert True is False, and an entry left in the cache directory).Open questions
None.
Revert
A plain revert restores the
UnicodeEncodeErroron text with a lone surrogate. Nothing is stored.Checklist
pytestpassesruff,mypy, and schema checks passAGENTS.mdraise/exit and test names the doc, contract or issue it traces to.github/.licenserc.yaml)