Fix #1845: fix: 修一下util里的timer_with_status的问题#1846
Open
Memtensor-AI wants to merge 1 commit into
Open
Conversation
`timed_with_status` previously swallowed exceptions silently when no `fallback` was supplied, returning `None` to the caller. This is the exact footgun the inline note in `src/memos/mem_os/utils/format_utils.py:1410-1415` calls out: `clean_json_response received None — upstream LLM call likely failed silently (check timed_with_status / generate() error handling)`. Behaviour after the fix: - `fallback` supplied → unchanged: fallback's return value is returned, status is logged as FAILED. - `fallback` is `None` → original exception is re-raised after the `finally` block has emitted the `[TIMER_WITH_STATUS] ... status: FAILED` log line. Exception identity and chain (`__cause__` / `__context__`) are preserved. Affected production call-sites: `OpenAILLM.generate` and `OpenAILLM.generate_stream` (which already `raise` inside the function body — the decorator now honours that intent). The three call-sites that supply a fallback (`UniversalAPIEmbedder.get_embeddings`, `HttpBGEReranker.rerank`) are unaffected. Also added `@functools.wraps(fn)` to the plain `timed` decorator so it stops clobbering `__name__` / `__doc__`. Tests: - `test_failure_logging_no_fallback` updated to assert the exception propagates while the FAILED log is still emitted. - `test_failure_no_fallback_preserves_original_exception` (new): asserts same-object identity + no synthetic chaining. - `test_preserves_function_metadata` (new) for `@timed`.
5 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
修复
src/memos/utils.py::timed_with_status的静默吞异常 bug。原行为:当未传fallback时,被装饰函数抛出的异常会被except捕获后丢失,包装器返回None——这正是format_utils.py:1410-1415里那条"clean_json_response received None — upstream LLM call likely failed silently"防御注释指控的源头。新行为:未传fallback时,先在finally内照常打[TIMER_WITH_STATUS] ... status: FAILED日志,再把原异常重新抛出,traceback /__cause__/__context__保持不变;传了fallback时路径完全不变。顺手给timed装饰器补了functools.wraps,停止把__name__/__doc__弄丢。生产侧受影响的两个调用点
OpenAILLM.generate/generate_stream函数体内本来就有raise,装饰器现在终于把这个意图传到上层;另外三个用了fallback的调用点 (UniversalAPIEmbedder.get_embeddings、HttpBGEReranker.rerank) 行为零变化。测试改动:把原
test_failure_logging_no_fallback(之前以"应静默"为契约)改成断言异常需向上传播且 FAILED 日志仍要打;新增test_failure_no_fallback_preserves_original_exception(同对象身份 + 无合成链)和test_preserves_function_metadata(@timed元数据)。沙箱内无法跑make test(缺transformers等大依赖),用最小化memos命名空间桩跑pytest tests/test_utils_timing.py得33 passed;并把修复回滚为旧实现重跑两条新用例,两条均按预期DID NOT RAISE红——TDD 证据完整。ruff check/ruff format --check在改动文件上均通过;本地分支已 push 到origin/autodev/MemOS-1845。specs 仓库归档了00-decision.md/01-clarification.md/03-fix.md/04-integration-report.md。Related Issue (Required): Fixes #1845
Type of change
Please delete options that are not relevant.
How Has This Been Tested?
Executor did not report tests.
Checklist
@CarltonXiang, @syzsunshine219 please review this PR.
Reviewer Checklist
📋 opsp 产物
本任务的设计文档、澄清记录、集成报告归档在 specs 仓库:
https://github.com/MemTensor/memos-autodev-specs/tree/main/2026-06-01-1845-fix-修一下util里的timerwithstatus的问题/
(异步推送,短时间内访问可能 404,稍候再试。)