fix: 修复 s09 memory pipeline 无法处理非 text 响应块的问题#300
Open
neystan wants to merge 2 commits into
Open
Conversation
|
@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. |
Contributor
There was a problem hiding this comment.
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 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 |
|
|
||
| def _memory_signature(mem: dict) -> tuple[str, str, str]: | ||
| return ( | ||
| str(mem.get("name", "")).strip().lower(), |
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 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 |
| messages.append({"role": "user", | ||
| "content": "<reminder>Update your todos.</reminder>"}) | ||
| rounds_since_todo = 0 | ||
|
|
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.
问题背景
s09_memory/code.py中的记忆流程在多个位置直接读取response.content[0].text。当模型返回的第一个 block 不是 text,而是ThinkingBlock等其他类型时,会直接抛错,导致记忆选择、记忆抽取或记忆合并流程中断。相关逻辑原本类似:
由于记忆抽取流程中断,后续不会继续执行
.memory/*.md写入和MEMORY.md索引更新,因此表现上会出现“助手说记住了,但实际没有写入记忆”的情况。问题表现
运行过程中可能出现类似报错:
出现该问题后,记忆内容无法正常写入
.memory/,也无法被后续对话正确加载。修复方式
本次修复将响应内容读取方式改为统一提取所有
textblock,而不是假设第一个 block 一定带有.text属性。修复后即使模型返回
ThinkingBlock或其他非 text block,记忆流程仍然可以继续完成文本提取、结果解析、记忆写入和索引更新: