Skip to content

Refactor memory extract v2#1045

Merged
MaojiaSheng merged 57 commits intomainfrom
refactor_memory_extract
Mar 30, 2026
Merged

Refactor memory extract v2#1045
MaojiaSheng merged 57 commits intomainfrom
refactor_memory_extract

Conversation

@chenjw
Copy link
Copy Markdown
Collaborator

@chenjw chenjw commented Mar 27, 2026

Description

主要功能

  1. 记忆提取系统重构 🔄
    - 引入 ExtractContextProvider 抽象
    - 简化 ExtractLoop 逻辑
    - 优化内存 prompt 模板
    - 实现 ReAct orchestrator 记忆模板系统
  2. Bug 修复 🐛
    - 修复 memory_type 未传递到 _apply_write 的问题
    - 修复 MEMORY_FIELDS 在向量索引中出现的问题
    - 修复内存提取过程中锁获取失败问题
    - 修复 extract_context 未定义问题
    - 修复 VolcEngine VLM 响应解析和缓存问题
  3. 模板系统 📝
    - skills.yaml 和 tools.yaml 使用 Jinja2 模板语法
    - 使用 DebugUndefined 处理模板未定义变量
    - 简化 events.yaml 模板
    - 优化工具记忆模板和压缩器 v2
  4. 功能增强 ✨
    - 支持 events ranges 单个索引格式
    - 通过 start_index 和 end_index 获取原文内容
    - 预取搜索基于用户消息
    - 添加 edit_overview 支持
    - 记忆文件向量化
  5. 其他 🔧
    - 重构 VolcEngine VLM cache 实现
    - 添加 FaultTolerantBaseModel 重构容错逻辑
    - 优化测试输出,隐藏 cache_control 日志

Related Issue

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Refactoring (no functional changes)
  • Performance improvement
  • Test update

Changes Made

Testing

  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • I have tested this on the following platforms:
    • Linux
    • macOS
    • Windows

Checklist

  • My code follows the project's coding style
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

Screenshots (if applicable)

Additional Notes

chenjw and others added 30 commits March 18, 2026 00:45
…on design document

- Add bilingual (English/Chinese) design document for memory templating system
- Include YAML-based MemoryTypeRegistry with 8 built-in types
- Detail ReAct 3+1 phase flow with pre-fetch optimization
- Describe 3-operation Schema: write/edit/delete
- Document RoocodePatch SEARCH/REPLACE format
- Explain dual-mode design: simple mode vs template mode
- Cover pre-fetch optimization: ls directories + read .abstract.md/.overview.md + search once
- Include merge operations: patch, sum, avg, immutable
- Address #578: allow custom prompt template addition and specification

Co-Authored-By: Claude Opus 4.6 <[email protected]>
- Add YAML-configurable memory schemas (cards, events, entities, etc.)
- Implement MemoryReAct with tool use (read/find/ls)
- Add schema-driven memory operations (write_uris/edit_uris/delete_uris)
- Implement memory patch handler for incremental updates
- Add comprehensive test suite
## Summary

Implement memory templating system (GitHub Issue #578) - a complete
rewrite of the memory extractor subsystem to support YAML-configurable
memory types instead of hardcoded categories.

## Key Changes

### Architecture
- Replace hardcoded 8 memory types with YAML-configurable schema system
- Add MemoryTypeRegistry to load memory type definitions from YAML files
- Dynamic Pydantic model generation from schema for type safety
- Field-level merge operations: PATCH, SUM, IMMUTABLE

### Memory Extraction Flow
- Implement ReAct orchestrator for single-pass memory updates
- MemoryUpdater for applying operations to storage
- Memory tools (read, search, ls) for ReAct loop
- Stable JSON parser with 5-layer fault tolerance

### File Naming & Storage
- Semantic filenames from template ({topic}.md instead of random IDs)
- Two memory modes: simple mode and template mode
- MEMORY_FIELDS HTML comment for structured metadata

### Configuration
- 9 YAML templates in openviking/prompts/templates/memory/
- memory_config.py for memory system configuration
- Dual-threshold compact upload mechanism in design doc

### Deletions
- Remove old memory_content.py, memory_data.py, memory_operations.py
- Remove memory_types.py, memory_utils.py, memory_patch.py
- Remove corresponding old test files

### Updated Components
- VLM backends (litellm, openai, volcengine) for new interfaces
- Session and service core integration
- Test suite for new architecture

Co-Authored-By: Claude Opus 4.6 <[email protected]>
## Summary

Fix missing parameters in commit_async() when calling extract_long_term_memories().
The synchronous commit() method correctly passes these parameters, but the async
version was missing them, causing memory extraction to be skipped.

## Changes

- Pass user=self.user, session_id=self.session_id, ctx=self.ctx
  in commit_async() when calling extract_long_term_memories()

Co-Authored-By: Claude Opus 4.6 <[email protected]>
## Summary

Fix JSON serialization error by converting FindResult object to dict
using its to_dict() method before returning from MemorySearchTool.

## Changes

- In MemorySearchTool.execute(), return search_result.to_dict()
  instead of search_result directly

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Also rename schema_models.py to schema_model_generator.py for clarity.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
…zation

- Add edit_overview_operations to MemoryUpdater for updating .overview.md files
- Optimize MemoryTypeRegistry initialization in SessionCompressorV2 (load once)
- Various memory templating system improvements

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Also fix duplicate line in system prompt.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
- MemoryUpdater now vectorizes written/edited memory files after apply_operations
- MemoryReAct generates overview following semantic.overview_generation.yaml format
- Auto-extract and write .abstract.md from overview in memory_updater
- Fix import: VikingURI is from openviking_cli.utils.uri

Co-Authored-By: Claude Opus 4.6 <[email protected]>
- Change vikingbot gateway to openviking-server --with-bot
- Change vikingbot chat to ov chat
- Update --no-markdown to --no-format
- Remove --logs flag (not available)
- Simplify CLI Reference table
- Also update Chinese README_CN.md

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Convert from pytest to standalone script with:
- SyncHTTPClient instead of AsyncHTTPClient
- Rich for pretty console output
- Phase control (ingest/verify/all)
- Better error handling and progress display

Co-Authored-By: Claude Opus 4.6 <[email protected]>
- Remove duplicate logger and create_session_compressor in session/__init__.py
- Remove duplicate MemoryConfig import in open_viking_config.py

Co-Authored-By: Claude Opus 4.6 <[email protected]>
- Parse function_call type responses (Responses API format)
- Fix cache key logic to use consistent "current" messages
- Fix previous_response_id not being passed when tools exist
- Fix tool call parsing to handle both tc.name and tc.function.name
- Preserve tool role info and image content in message conversion

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@github-actions
Copy link
Copy Markdown

Failed to generate code suggestions for PR

chenjw and others added 19 commits March 28, 2026 01:55
- search 工具无论是否有结果都记录到 messages 中
- refetch 时如果已达最大迭代次数,允许额外增加一次迭代
- 使用局部变量 max_iterations 避免修改实例属性

Co-Authored-By: Claude Opus 4.6 <[email protected]>
- 优化 tools.py 工具定义和消息格式
- memory_react.py 支持 refetch 时额外迭代
- 更新 memory_updater, patch, utils 等模块
- 更新测试文件

Co-Authored-By: Claude Opus 4.6 <[email protected]>
- 参考 vikingdb BaseModelCompat 创建 FaultTolerantBaseModel
- 在 model_validator(mode='before') 中自动做字段容错
- schema_model_generator 动态模型继承 FaultTolerantBaseModel
- extract_loop 删除 fallback 代码
- 修复 skills.yaml 模板变量缺失问题

Co-Authored-By: Claude Opus 4.6 <[email protected]>
在模板变量中始终传入 extract_context,避免 Jinja2 访问时 undefined

Co-Authored-By: Claude Opus 4.6 <[email protected]>
- 简化模板,移除复杂表达式计算
- 添加 default 过滤器处理缺失变量

Co-Authored-By: Claude Opus 4.6 <[email protected]>
移除 extract_context 调用,添加 default 过滤器

Co-Authored-By: Claude Opus 4.6 <[email protected]>
用 {% if extract_context %} 判断避免 None 时报错

Co-Authored-By: Claude Opus 4.6 <[email protected]>
- 使用 jinja2.DebugUndefined,未定义变量保留在输出中而不是报错
- 修复测试文件添加 extract_context 参数

Co-Authored-By: Claude Opus 4.6 <[email protected]>
使用 parse_memory_file_with_fields 清理内容后再提取 abstract

Co-Authored-By: Claude Opus 4.6 <[email protected]>
- 使用 parse_memory_file_with_fields 清理内容后再提取 abstract
- 修复 _extract_abstract_from_overview 和向量索引两处

Co-Authored-By: Claude Opus 4.6 <[email protected]>
支持 "7,9,11,13" 格式的单个索引,与范围格式混用

Co-Authored-By: Claude Opus 4.6 <[email protected]>
- 定义 ResolvedOperation dataclass 包含 model, uri, memory_type
- 修改 ResolvedOperations 使用 ResolvedOperation 列表替代元组
- 修改 apply_operations 传递 memory_type 参数到 _apply_write
- 修复 validate_operations_uris 中的元组解包问题
- 更新测试用例使用 dataclass 属性访问

Co-Authored-By: Claude Opus 4.6 <[email protected]>
## 核心变更

- 新增 ExtractContextProvider 抽象类,将 schema 加载和 context 提供分离
- 新增 SessionExtractContextProvider 实现,从会话消息中提取记忆
- ExtractLoop 现在接受 context_provider 而非 registry

## 模板优化

- events.yaml: 支持 ranges 解析和消息时间提取 (first_message_time)
- 简化 tools.yaml 和 skills.yaml 模板,移除冗余的历史调用描述

## 其他优化

- volcengine_vlm.py: 添加 timeout 参数支持
- sessions.py: 优化会话相关路由
- 清理 json_parser.py 中未使用的函数
- 简化 schema_model_generator.py 中的模型生成逻辑

Co-Authored-By: Claude Opus 4.6 <[email protected]>
主要改动:
1. 新增 get_response_id、responseapi_prefixcache_completion、
   responseapi_common_completion 三个方法,参考 ArkLM 实现
2. 统一工具调用消息格式:role=tool_call, content={tool_call_name, args, result}
3. 在 optimize_tool_result 中对 read 工具的 content 字段做截断
4. 修复 cache_control 逻辑:找到最后一个 breakpoint,从头到该位置为 static
5. 简化 volcengine_vlm.py,删除旧的 cache 相关方法

Co-Authored-By: Claude Opus 4.6 <[email protected]>
- 移除 ExtractLoop 中的重复逻辑,简化代码结构
- 优化 events/preferences/skills/tools 等 memory prompt 模板
- 清理 core.py 中未使用的代码
- 更新相关测试用例

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@chenjw chenjw changed the title [WIP](先别和入)Refactor memory extract Refactor memory extract Mar 30, 2026
@chenjw chenjw changed the title Refactor memory extract Refactor memory extract round2 Mar 30, 2026
@chenjw chenjw changed the title Refactor memory extract round2 Refactor memory extract 2 Mar 30, 2026
@chenjw chenjw changed the title Refactor memory extract 2 Refactor memory extract v2 Mar 30, 2026
@MaojiaSheng MaojiaSheng merged commit a66a1a6 into main Mar 30, 2026
6 of 7 checks passed
@MaojiaSheng MaojiaSheng deleted the refactor_memory_extract branch March 30, 2026 06:05
@github-project-automation github-project-automation bot moved this from Backlog to Done in OpenViking project Mar 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants