Skip to content

feat(mem_reader): add reasoning_chain field to summarizer output#1300

Open
abakane1 wants to merge 4 commits intoMemTensor:openclaw-local-plugin-20260324from
abakane1:feat/reasoning-chain-memory
Open

feat(mem_reader): add reasoning_chain field to summarizer output#1300
abakane1 wants to merge 4 commits intoMemTensor:openclaw-local-plugin-20260324from
abakane1:feat/reasoning-chain-memory

Conversation

@abakane1
Copy link
Copy Markdown

Summary

Add optional reasoning_chain field to memory summarizer output JSON schema.

Problem

Current MemOS summarizer extracts "what happened" from conversations but lacks "why it happened" and "what it means for future interactions". This causes memory misalignment when the AI retrieves facts without understanding the underlying reasoning.

Solution

Extend the summarizer output schema with a new optional reasoning_chain object:

{
  "memory_list": [...],
  "summary": "...",
  "reasoning_chain": {
    "goal": "what was the user's core goal",
    "decision": "decisions made and why",
    "correction": "any corrections or misunderstandings",
    "preference": "preference updates",
    "attention": "what to be careful about in future"
  }
}

Design Context

This feature is part of a three-layer correction & reasoning chain protocol:

  1. Layer 1 - Agent behavior (OpenClaw AGENTS.md): When user expresses correction, agent actively asks why. This captures the missing "why" that never appears in conversation unless asked.
  2. Layer 2 - WAL recording: Correction context written to session WAL with structured format.
  3. Layer 3 - MemOS summarizer: WAL entries + conversation → reasoning_chain field in memory output.

The three layers must be implemented together for this to be effective.

Changes

  • src/memos/templates/mem_reader_prompts.py: Add reasoning_chain field + instruction to prompt
  • src/memos/memories/textual/item.py: Add reasoning_chain field to TreeNodeTextualMemoryMetadata
  • src/memos/mem_reader/simple_struct.py: Pass reasoning_chain through to memory item metadata

Backward Compatibility

  • Field is optional — existing code that doesn't produce it will continue to work
  • Existing memories without reasoning_chain are unaffected

Type of change

  • New feature (non-breaking change which adds functionality)

How Has This Been Tested?

  • Code review (changes verified syntactically)
  • Integration test with actual conversation data (planned)

Zuliang Zhao added 2 commits March 13, 2026 10:17
- Support milliseconds, microseconds, and seconds timestamp formats
- Fixes incorrect days calculation when timestamps use different formats
- Closes issue with 36500 days display error
Add optional 'reasoning_chain' field to memory summarizer output:
- New field captures: goal, decision, correction, preference, attention
- Enables memory systems to store not just facts but reasoning context
- Backward compatible (field is optional)

Co-designed with user: correction & reasoning chain requires active
questioning during conversation, not just passive summarization.
Full design: github.com/MemTensor/MemOS/pull/XXX
@CaralHsi CaralHsi changed the base branch from main to dev-20260319-v2.0.10 March 19, 2026 11:18
CaralHsi and others added 2 commits March 19, 2026 19:20
- Add tab switch between Summary and Reasoning tabs in memory cards
- 5-field reasoning chain display: goal, decision, correction, preference, attention
- Color-coded labels for each reasoning field
- Only show Reasoning tab when reasoning_chain data exists
- i18n: Chinese and English labels
@CaralHsi CaralHsi deleted the branch MemTensor:openclaw-local-plugin-20260324 March 23, 2026 06:02
@CaralHsi CaralHsi closed this Mar 23, 2026
@CaralHsi CaralHsi reopened this Mar 23, 2026
@CaralHsi CaralHsi changed the base branch from dev-20260319-v2.0.10 to dev-20260323-v2.0.11 March 23, 2026 06:44
@syzsunshine219
Copy link
Copy Markdown
Collaborator

syzsunshine219 commented Mar 23, 2026

reasoning_chain was added to SIMPLE_STRUCT_MEM_READER_PROMPT but not SIMPLE_STRUCT_MEM_READER_PROMPT_ZH. Chinese conversations won't get reasoning chains at all.

None of the examples include reasoning_chain in their output JSON. LLMs copy example structure more than they read schema text — expect frequent omission. Need at least one example with the field.

reasoning_chain: dict | None — a typed Pydantic model with the 5 fields would be safer and self-documenting.

Frontend checks val !== 'N/A' but LLMs will write "n/a", "Not applicable", "无" etc. Easier to just use null in the prompt.

The timestamp format fix in loadStats() is a separate bugfix, doesn't belong in this feature PR.

reasoning_chain 只改了英文 prompt,中文 SIMPLE_STRUCT_MEM_READER_PROMPT_ZH 没动,中文对话完全不会生成推理链。

所有 example 的输出 JSON 里都没带 reasoning_chain。LLM 抄示例比读 schema 认真得多,不补示例这个字段大概率会被跳过。

reasoning_chain: dict | None 太松了,5 个字段定义成 Pydantic model 更稳。

前端拿 val !== 'N/A' 做过滤,但 LLM 很可能写成 "n/a"、"不适用" 之类。prompt 里直接让填 null 更省事。

loadStats() 的时间戳格式修复是独立 bug fix,混在 feature PR 里不太合适,建议拆出去。

@hijzy hijzy changed the base branch from dev-20260323-v2.0.11 to openclaw-local-plugin-20260324 March 24, 2026 11:50
@hijzy
Copy link
Copy Markdown
Collaborator

hijzy commented Mar 24, 2026

PR Review

This PR adds a reasoning_chain field to the summarizer output, capturing structured reasoning behind memories: goal, decision, correction, preference, and attention. Changes span both Python (simple_struct.py, item.py, mem_reader_prompts.py) and TypeScript (viewer/html.ts). The Python side adds the field to the data model and prompt template. The viewer adds a tabbed UI (Summary/Reasoning) with color-coded labels and full i18n support (English + Chinese). The timestamp handling in html.ts is also improved to support microseconds.

The implementation is clean, well-structured, and purely additive — it does not modify existing behavior, only extends the data model and UI. The code follows existing patterns and conventions.

此 PR 在 summarizer 输出中新增了 reasoning_chain 字段,包含目标、决策、纠正、偏好、注意五个维度。改动横跨 Python(数据模型和 prompt)和 TypeScript(viewer UI)。Viewer 新增了 Summary/Reasoning 双标签页切换,支持中英文 i18n。实现干净,纯增量修改,无破坏性风险。

  • Safety: ✅ Safe
  • Mergeable: ✅ Yes
  • Confidence: 5/5

@hijzy
Copy link
Copy Markdown
Collaborator

hijzy commented Mar 26, 2026

Changes to the memreader should be implemented in a configurable manner; otherwise, they may impact the online cloud service.

对于 memreader 的修改需要以可配置的方式实现,否则可能会影响线上云服务。

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants