Skip to content

fix(mem_os): strip only markdown fences in clean_json_response - #2421

Closed
simpleqt wants to merge 1 commit into
MemTensor:mainfrom
simpleqt:sq/clean-json-fence
Closed

simpleqt wants to merge 1 commit into
MemTensor:mainfrom
simpleqt:sq/clean-json-fence

Conversation

@simpleqt

Copy link
Copy Markdown

Description

Fixes #2420

clean_json_response stripped markdown fences with a global replace("```", ""), so JSON string values containing fenced code were silently corrupted before json.loads in api/handlers/chat_handler.py and api/handlers/suggestion_handler.py:

clean_json_response('```json\n{"text": "```py\\ncode```"}\n```')
# before: '{"text": "py\\ncode"}'   <- value's fences gone
# after:  '{"text": "```py\\ncode```"}'

Implementation: strip only an opening fence — ^```[^\n]*\n? (covers the optional json info string) — and a closing fence — \n?```\s*$ — via a single anchored re.sub. Interior occurrences are untouched. No dependency changes.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

  • Unit Test

Added four pytest cases in tests/mem_os/test_format_utils.py: interior fences preserved (red/green discriminator — fails against unpatched main), plain fences still stripped, fence-less input unchanged, and opening-fence-only input.

  • Test Script Or Test Steps
poetry run pytest tests/mem_os/test_format_utils.py -v

ruff check and ruff format --check pass on both touched files.

Checklist

  • I have performed a self-review of my own code | 我已自行检查了自己的代码
  • I have commented my code in hard-to-understand areas | 我已在难以理解的地方对代码进行了注释
  • I have added tests that prove my fix is effective or that my feature works | 我已添加测试以证明我的修复有效或功能正常
  • I have linked the issue to this PR (if applicable) | 我已将 issue 链接到此 PR(如果适用)

clean_json_response replaced every triple-backtick run in the response,
so JSON string values that themselves contain fenced code (common when
an LLM returns content with markdown snippets) were silently corrupted
before json.loads. Strip only an opening fence (with its optional info
string) at the very start and a closing fence at the very end.

Red/green verified: the new test fails against the old implementation
(interior fences removed) and passes with the fix.

Signed-off-by: simpleqt <89645338+simpleqt@users.noreply.github.com>
Copilot AI lite review requested due to automatic review settings September 28, 2026 07:40
@Memtensor-AI Memtensor-AI added area:core MOS 编排层 / 框架底座 / 跨模块问题 status:in-progress Someone or AI is working on it | 人工或 AI 正在处理 labels Sep 28, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@Memtensor-AI

Copy link
Copy Markdown
Collaborator

🤖 Open Code Review

Target: PR #2421
Task: 8c90519b26209ab0
Base: main
Head: sq/clean-json-fence

✅ OpenCodeReview: Review complete: 0 finding(s) across 2 selected item(s).

Generated by cloud-assistant via Open Code Review.

@Memtensor-AI

Copy link
Copy Markdown
Collaborator

✅ Automated Test Results: PASSED

All tests passed (12/12 executed). memos_python_core/changed-repo-python: 12/12. Duration: 8s

Branch: sq/clean-json-fence

@Memtensor-AI Memtensor-AI added status:ready Ready for implementation; waiting for assignee or AI dispatch | 可进入实现,等待认领或派发 and removed status:in-progress Someone or AI is working on it | 人工或 AI 正在处理 labels Sep 28, 2026
@github-actions

Copy link
Copy Markdown
Contributor

This PR has been automatically marked as stale due to inactivity.

@github-actions github-actions Bot added the status:stale Auto-marked stale after 30d inactivity; closes 7d later unless exempt. label Oct 31, 2026
@github-actions

github-actions Bot commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

This PR has been automatically closed due to inactivity.

@github-actions github-actions Bot closed this Nov 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core MOS 编排层 / 框架底座 / 跨模块问题 status:ready Ready for implementation; waiting for assignee or AI dispatch | 可进入实现,等待认领或派发 status:stale Auto-marked stale after 30d inactivity; closes 7d later unless exempt.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

clean_json_response corrupts JSON values containing code fences

4 participants