feat(memory): 引入 User/Peer 记忆隔离模型#2236
Draft
qin-ctx wants to merge 1 commit into
Draft
Conversation
Unify agent-scoped memory behavior into user-owned memory spaces, add peer_id compatibility for session and retrieval paths, and wire memory_policy through session commit flows.
PR Reviewer Guide 🔍Here are some key observations to aid the review process:
|
PR Code Suggestions ✨No code suggestions found for the PR. |
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.
Description
本 PR 实现 OpenViking 数据面认证和记忆隔离的新方案:以 API Key 对应的
user_id作为唯一数据归属主体,废弃 OpenViking 内部的 agent 数据主体语义,并引入peer_id表达当前 User 在交互中需要长期区分的外部对象。核心目标是让数据归属、目录落点和检索范围形成闭环:
user_id,不会因为消息里的说话人、历史agent_id或模型抽取结果改变写入 owner。peer_id,用于客服、陪聊、群聊等场景下区分稳定交互对象。agent_id作为兼容字段仍可出现在请求里,但会在数据面入口统一转换为peer_id,内部不再继续传播 agent 身份和 agent 目录。memory_policy,用于决定本次提交是否抽取 self memory、是否抽取 peer memory,以及可抽取哪些 memory types。peer_id时返回当前 User 可访问的全部 User/Peer 记忆;指定peer_id时只补充该 peer 的记忆空间。Related Issue
暂无关联 issue。本 PR 来自数据面认证、User/Peer 记忆归属和目录结构方案讨论。
Type of Change
Changes Made
数据主体收敛到 User:
agent_id作为内部数据主体的路径传播。agent_id兼容入口,并通过normalize_peer_id(peer_id, agent_id)统一转换为peer_id。viking://agent/...、agent directory、agent identity 等内部目录和身份语义,相关模板里的agent_space仅作为兼容变量映射到当前 User 空间。引入 Peer 记忆模型:
peer_id字段,支持单条和 batch add messages。peer_id不参与授权,也不会改变当前请求的user_id;它只决定是否在当前 User 下创建和访问peers/{peer_id}记忆空间。MemoryIsolationHandler只以当前 authenticated User 作为写入 owner,LLM 抽取出的user_id、agent_id、agent_ids不能改写归属。增加 Session memory policy:
openviking/session/memory_policy.py,统一解析 self/peer 抽取策略。create_session和commit_session支持设置或覆盖memory_policy。self.enabled控制当前 User 自身记忆抽取。peer.enabled控制是否按消息中的peer_id抽取 peer memory。types为空时表示使用默认可抽取类型;未知类型或 peer 不支持的类型会被拒绝。调整目录与资源能力:
add_skill写入viking://user/{user_id}/skills/...。调整检索行为:
find/search支持peer_id参数。peer_id时,默认检索当前 User 的自身记忆以及该 User 下全部 peer 记忆。peer_id时,检索当前 User 自身记忆和对应 peer 记忆。agent_id请求会被转换为peer_id,保证旧调用可以平滑进入新模型。SDK/CLI 适配:
peer_id、batch add messages、commitmemory_policy、search/findpeer_id。ov find/search --peer-id,并保持 profile output、TUI、配置项和主分支最新改动兼容。测试覆盖:
tests/server/test_peer_id_compat.py覆盖agent_id -> peer_id兼容、冲突校验、检索默认 peer 范围等行为。tests/session/test_memory_policy.py覆盖 memory policy 默认值、类型过滤、commit 覆盖和非法配置。Testing
本地验证命令:
cargo fmt -p ov_cli .venv/bin/python -m compileall -q openviking openviking_cli git diff --check .venv/bin/python -m pytest tests/server/test_peer_id_compat.py tests/session/test_session_messages.py tests/client/test_http_client_config.py tests/cli/test_user_identifier.py tests/session/memory/test_memory_isolation_handler.py tests/session/test_memory_policy.py -q cargo test -p ov_cli结果:
Checklist
Screenshots (if applicable)
不涉及 UI 截图。
Additional Notes
本 PR 当前以 Draft 形式创建,原因是改动覆盖数据面认证、目录结构、Session commit、检索、SDK/CLI 和大量测试,建议先按设计方案和核心调用链做 review,再决定是否 Ready for review。
需要重点 review 的点:
agent_id兼容策略是否只保留在请求入口,内部是否已经统一为peer_id。peer_id时检索全部当前 User peer memory 的行为是否符合产品预期。memory_policy的默认值和类型约束是否足够简单,是否避免引入不必要的授权模型复杂度。add_skill写入 User 私有 skill 空间是否符合当前阶段对 skills 私有化的判断。