Skip to content

feat(bot): Bot session compact by using openviking, ov client add function and param#2284

Merged
qin-ctx merged 9 commits into
mainfrom
feature_bot_session_compact
May 29, 2026
Merged

feat(bot): Bot session compact by using openviking, ov client add function and param#2284
qin-ctx merged 9 commits into
mainfrom
feature_bot_session_compact

Conversation

@yeshion23333
Copy link
Copy Markdown
Collaborator

@yeshion23333 yeshion23333 commented May 28, 2026

Description

Summary

本次改造将 VikingBot 的长对话压缩链路接入 OpenViking session context,把原来的本地 JSONL compact 流程替换为“本地短期原始日志 + OpenViking 长上下文压缩层”的双层结构。

核心变化:

  • 新增基于稳定 OpenViking session 的上下文压缩模式:

    • 一个 VikingBot session 对应一个稳定 OV session;
    • 每轮对话后增量同步本地新增消息;
    • 下一轮模型调用前从 get_session_context() 读取 OV 摘要和 live messages;
    • 本地只补未同步 tail,避免重复上下文。
  • 保持 OpenViking 写入和 commit 逻辑收敛在 openviking_hooks.py

    • 所有消息写入 admin session;
    • sender_id fanout 到 <session_id>_<sender_id>
    • 支持 token threshold 和 memory_window 双条件触发 commit。
  • 新增 session-context 相关配置到 AgentsConfig

    • session_context_enabled
    • session_context_token_budget
    • commit_token_threshold
    • commit_keep_recent_count
  • 扩展 OpenViking client/session wrapper:

    • 支持 batch_add_messages(...)
    • 支持 commit_session(..., keep_recent_count=...)
    • 对齐 server 侧已有 session commit 参数。
  • commit 成功后清理本地 JSONL session:

    • OV commit 作为新的 compact 行为;
    • commit 成功后清空本地消息;
    • 保留稳定 OV session id,后续继续从 OV context 恢复长上下文。
  • 增强同步状态一致性:

    • 抽出 session_state.py 管理 OV metadata / cursor / unsynced tail;
    • 修复 cursor 越界导致全量重传的问题;
    • 修复 sender fanout partial failure 仍被当作 success 的问题;
    • fanout 失败时不推进 sync cursor,也不会清空本地 JSONL。

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

@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 28, 2026

PR Reviewer Guide 🔍

(Review updated until commit 21fda3b)

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 4 🔵🔵🔵🔵⚪
🏅 Score: 85
🧪 PR contains tests
🔒 No security concerns identified
✅ No TODO sections
🔀 Multiple PR themes

Sub-PR theme: Add OpenViking session context integration with sync/commit logic

Relevant files:

  • bot/vikingbot/openviking_mount/session_state.py
  • bot/vikingbot/openviking_mount/ov_server.py
  • bot/vikingbot/hooks/builtins/openviking_hooks.py
  • bot/tests/test_openviking_api_key_type.py

Sub-PR theme: Extend OpenViking client with keep_recent_count parameter for commit

Relevant files:

  • openviking/client/session.py
  • openviking/async_client.py
  • openviking/sync_client.py
  • tests/session/test_session_commit.py

Sub-PR theme: Integrate OpenViking session context into agent loop

Relevant files:

  • bot/vikingbot/agent/loop.py
  • bot/tests/test_agent_loop_outcome.py

⚡ Recommended focus areas for review

Access to private method

Accessing private method _load_namespace_policy() on client is not a good practice as it couples the tool to internal implementation details of the client.

await client._load_namespace_policy()

@github-actions
Copy link
Copy Markdown

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
Possible issue
Preserve message parts and allow messages with either content or parts

The current _normalize_session_messages skips messages with parts (e.g., tool calls)
and doesn't propagate parts to the OpenViking session. This leads to missing tool
execution traces in the compressed context. Fix by preserving parts and allowing
messages with either content or parts.

bot/vikingbot/openviking_mount/ov_server.py [479-513]

 def _normalize_session_messages(
     self,
     messages: list[dict[str, Any]],
     default_user_role_id: Optional[str] = None,
 ) -> list[dict[str, Any]]:
     normalized: list[dict[str, Any]] = []
     assistant_role_id = self._assistant_role_id()
 
     for message in messages:
         role = str(message.get("role") or "").strip().lower()
         if role not in {"user", "assistant"}:
             continue
 
         content = message.get("content")
-        if not isinstance(content, str) or not content.strip():
+        parts = message.get("parts")
+        has_content = isinstance(content, str) and content.strip()
+        has_parts = isinstance(parts, list) and parts
+
+        if not has_content and not has_parts:
             continue
 
         payload = {
             "role": role,
-            "content": content,
             "created_at": message.get("created_at") or message.get("timestamp"),
         }
+
+        if has_content:
+            payload["content"] = content
+        if has_parts:
+            payload["parts"] = parts
 
         role_id = message.get("role_id")
         if not role_id and role == "user":
             role_id = message.get("sender_id") or default_user_role_id
         elif not role_id and role == "assistant":
             role_id = assistant_role_id
 
         if role_id:
             payload["role_id"] = role_id
 
         normalized.append(payload)
 
     return normalized
Suggestion importance[1-10]: 7

__

Why: Fixes missing preservation of message parts (e.g., tool calls, context parts) in normalized messages, aligning with the PR's new tests for batch_add_messages that use parts. Improves completeness of session context.

Medium
Check if user initialization succeeds before proceeding

The _session_client_for_user function doesn't check if _initialize_user succeeds
before proceeding. If user initialization fails, it should fall back to the default
client instead of continuing with a potentially invalid state.

bot/vikingbot/openviking_mount/ov_server.py [445-470]

 async def _session_client_for_user(self, user_id: Optional[str] = None):
     if not user_id or self.mode == "local" or self._is_user_key_mode():
         return self._session_client(user_id)
     if user_id == self.admin_user_id and self.admin_user_client:
         return self.admin_user_client
 
     user_exists = await self._check_user_exists(user_id)
     if not user_exists:
-        await self._initialize_user(user_id)
+        initialized = await self._initialize_user(user_id)
+        if not initialized:
+            logger.warning(f"Failed to initialize user {user_id}, falling back to default client")
+            return self._session_client(user_id)
 
     api_key = await self._get_or_create_user_apikey(user_id)
     if not api_key:
         return self._session_client(user_id)
 
     client = self._user_clients.get(user_id)
     if client is None:
         client = ov.AsyncHTTPClient(
             url=self.openviking_config.server_url,
             api_key=api_key,
             agent_id=self.agent_id,
             account=self.account_id,
             user=user_id,
         )
         await client.initialize()
         self._user_clients[user_id] = client
     return client
Suggestion importance[1-10]: 5

__

Why: Adds error handling for user initialization failure, falling back to the default client. Improves robustness but isn't a critical bug fix.

Low

@yeshion23333 yeshion23333 marked this pull request as ready for review May 29, 2026 03:07
@yeshion23333 yeshion23333 requested review from chenjw and qin-ctx May 29, 2026 03:09
@github-actions
Copy link
Copy Markdown

Persistent review updated to latest commit 21fda3b

@github-actions
Copy link
Copy Markdown

PR Code Suggestions ✨

No code suggestions found for the PR.

…ompact

# Conflicts:
#	bot/tests/test_openviking_api_key_type.py
#	bot/vikingbot/agent/tools/ov_file.py
#	bot/vikingbot/openviking_mount/ov_server.py
Copy link
Copy Markdown
Collaborator

@qin-ctx qin-ctx left a comment

Choose a reason for hiding this comment

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

本次 review 发现 2 个 blocking 问题,已 inline 标注:batch add 没有处理服务端 100 条上限,以及 session-context 主链路会把完整 tool result 写入长上下文。另有一个非阻塞行为风险已 inline 标注。\n\n额外的非阻塞建议:这个 PR 目前包含两个 merge commit 和多个 fix bug / 默认值 commit,PR body 的 Type/Testing checklist 也还没有补完整;建议 merge 前 squash 并补齐测试说明。

Comment thread bot/vikingbot/openviking_mount/ov_server.py Outdated
Comment thread bot/vikingbot/openviking_mount/ov_server.py Outdated
Comment thread bot/vikingbot/agent/loop.py
Copy link
Copy Markdown
Collaborator

@qin-ctx qin-ctx left a comment

Choose a reason for hiding this comment

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

这轮复查确认之前 3 个问题都已经修复:batch add 已按 100 条分片、session-context 不再写入完整 tool result、pre-turn commit 已移到 outcome evaluation 之后。

但还有一个需要阻塞合并的问题:commit 后清空本地 messages 时,last_sender_synced_local_indexes 可能因为 session metadata 的递归 merge 被旧值合回来,导致后续 per-user fanout 漏同步同一 sender 的新消息。这个会影响 user-scoped memory/context 的完整性,建议修复后再合并。

额外建议:session_context_token_budget 目前只约束 OV context,最终 prompt 仍可能因为 local unsynced tail 过长而超窗;PR body 的 Type/Testing checklist 和 commit history 也建议合并前整理。

Comment thread bot/vikingbot/openviking_mount/session_state.py Outdated
@qin-ctx qin-ctx merged commit de443fd into main May 29, 2026
5 checks passed
@qin-ctx qin-ctx deleted the feature_bot_session_compact branch May 29, 2026 12:47
@github-project-automation github-project-automation Bot moved this from Backlog to Done in OpenViking project May 29, 2026
r266-tech added a commit to r266-tech/OpenViking that referenced this pull request May 29, 2026
r266-tech added a commit to r266-tech/OpenViking that referenced this pull request May 29, 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