Skip to content

fix: 修复 s09 memory pipeline 无法处理非 text 响应块的问题#300

Open
neystan wants to merge 2 commits into
shareAI-lab:mainfrom
neystan:fix/s09-memory-pipeline
Open

fix: 修复 s09 memory pipeline 无法处理非 text 响应块的问题#300
neystan wants to merge 2 commits into
shareAI-lab:mainfrom
neystan:fix/s09-memory-pipeline

Conversation

@neystan
Copy link
Copy Markdown

@neystan neystan commented May 23, 2026

问题背景

s09_memory/code.py 中的记忆流程在多个位置直接读取 response.content[0].text。当模型返回的第一个 block 不是 text,而是 ThinkingBlock 等其他类型时,会直接抛错,导致记忆选择、记忆抽取或记忆合并流程中断。

相关逻辑原本类似:

text = response.content[0].text.strip()

由于记忆抽取流程中断,后续不会继续执行 .memory/*.md 写入和 MEMORY.md 索引更新,因此表现上会出现“助手说记住了,但实际没有写入记忆”的情况。

问题表现

运行过程中可能出现类似报错:

[Memory extraction failed] 'ThinkingBlock' object has no attribute 'text'

出现该问题后,记忆内容无法正常写入 .memory/,也无法被后续对话正确加载。

修复方式

本次修复将响应内容读取方式改为统一提取所有 text block,而不是假设第一个 block 一定带有 .text 属性。

修复后即使模型返回 ThinkingBlock 或其他非 text block,记忆流程仍然可以继续完成文本提取、结果解析、记忆写入和索引更新:

def _response_text(blocks) -> str:
    parts = []
    for block in blocks:
        if getattr(block, "type", None) == "text":
            parts.append(getattr(block, "text", ""))
    return "\n".join(p for p in parts if p).strip()

Copilot AI review requested due to automatic review settings May 23, 2026 15:16
@vercel
Copy link
Copy Markdown

vercel Bot commented May 23, 2026

@neystan is attempting to deploy a commit to the crazyboym's projects Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR refactors memory extraction/parsing to be more resilient (handling non-text blocks, reporting failures) and adds basic de-duplication when writing memory files. It also changes how the “update todos” reminder is injected into the agent loop.

Changes:

  • Add helpers for response text extraction and memory de-duplication during storage.
  • Improve extractor error handling/logging and reuse the new response-text helper across memory flows.
  • Modify the agent loop to append a reminder message and reset the reminder counter.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

File Description
s09_memory/code.py Adds _response_text and _store_memories, updates memory selection/extraction/consolidation to use them, and improves logging/de-duping.
s07_skill_loading/code.py Changes reminder injection strategy in agent_loop to append a new user message and reset the counter.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread s09_memory/code.py
Comment on lines +151 to +165
for mem in items:
name = str(mem.get("name", "")).strip() or f"memory-{int(time.time())}"
mem_type = mem.get("type", "user")
desc = str(mem.get("description", "")).strip()
body = str(mem.get("body", "")).strip()
if mem_type not in MEMORY_TYPES or not desc or not body:
continue

sig = _memory_signature({"name": name, "description": desc, "body": body})
if sig in seen:
continue

write_memory_file(name, mem_type, desc, body)
seen.add(sig)
count += 1
Comment thread s09_memory/code.py

def _memory_signature(mem: dict) -> tuple[str, str, str]:
return (
str(mem.get("name", "")).strip().lower(),
Comment thread s09_memory/code.py
Comment on lines 300 to 305
# Extract JSON array from response
match = re.search(r'\[.*\]', text, re.DOTALL)
if not match:
print("\033[90m[Memory: no JSON returned by extractor]\033[0m")
return
items = json.loads(match.group())
Comment thread s07_skill_loading/code.py
Comment on lines 346 to +349
if rounds_since_todo >= 3 and messages:
last = messages[-1]
if last["role"] == "user" and isinstance(last.get("content"), list):
last["content"].insert(0, {
"type": "text",
"text": "<reminder>Update your todos.</reminder>",
})

messages.append({"role": "user",
"content": "<reminder>Update your todos.</reminder>"})
rounds_since_todo = 0
Comment thread s07_skill_loading/code.py
messages.append({"role": "user",
"content": "<reminder>Update your todos.</reminder>"})
rounds_since_todo = 0

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants