Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

fix: resolve OpenClaw session lock conflicts & fix session commit assertion (#1441)

kaisonglid18d9d771f83
diff --git a/tests/api_test/scenarios/sessions/test_session_commit.py b/tests/api_test/scenarios/sessions/test_session_commit.py
index f9428ca71..f0fb8daaf 100644
--- a/tests/api_test/scenarios/sessions/test_session_commit.py
+++ b/tests/api_test/scenarios/sessions/test_session_commit.py
@@ -1,145 +1,150 @@
-import pytest
-import json
import uuid
class TestSessionCommit:
"""TC-S01 对话持久化与 Commit"""
-
+
def test_session_persistence_and_commit(self, api_client):
"""对话持久化与 Commit:创建会话 -> 添加消息 -> 提交 -> 验证"""
random_id = str(uuid.uuid4())[:8]
test_messages = [
f"First test message for session commit {random_id}",
f"Second test message for session commit {random_id}",
- f"Third test message for session commit {random_id}"
+ f"Third test message for session commit {random_id}",
]
-
+
# 1. 创建会话
response = api_client.create_session()
assert response.status_code == 200
data = response.json()
- assert data.get('status') == 'ok'
- assert 'result' in data
-
- create_result = data['result']
- assert 'session_id' in create_result
- session_id = create_result['session_id']
+ assert data.get("status") == "ok"
+ assert "result" in data
+
+ create_result = data["result"]
+ assert "session_id" in create_result
+ session_id = create_result["session_id"]
assert session_id is not None
assert len(session_id) > 0
-
+
# 2. 添加多条消息
added_message_count = 0
- for i, test_message in enumerate(test_messages):
+ for _i, test_message in enumerate(test_messages):
response = api_client.add_message(session_id, "user", test_message)
assert response.status_code == 200
msg_data = response.json()
- assert msg_data.get('status') == 'ok'
+ assert msg_data.get("status") == "ok"
added_message_count += 1
-
+
# 3. 验证添加消息后 message_count 正确
response = api_client.get_session(session_id)
assert response.status_code == 200
get_data = response.json()
- assert get_data.get('status') == 'ok'
- get_result = get_data['result']
-
- assert get_result['message_count'] == added_message_count, \
+ assert get_data.get("status") == "ok"
+ get_result = get_data["result"]
+
+ assert get_result["message_count"] == added_message_count, (
f"Message count should be {added_message_count} after adding messages, got {get_result['message_count']}"
-
+ )
+
# 4. 标记会话已使用
response = api_client.session_used(session_id)
assert response.status_code == 200
-
+
# 5. 提交会话
response = api_client.session_commit(session_id)
assert response.status_code == 200
commit_data = response.json()
- assert commit_data.get('status') == 'ok'
-
+ assert commit_data.get("status") == "ok"
+
# 验证 commit 返回结果
- commit_result = commit_data['result']
- assert 'archived' in commit_result, "Commit result should contain 'archived' field"
- assert commit_result['archived'] == True, "Messages should be archived after commit"
-
+ commit_result = commit_data["result"]
+ assert "archived" in commit_result, "Commit result should contain 'archived' field"
+ assert commit_result["archived"], "Messages should be archived after commit"
+
# 6. 等待异步任务完成
- task_id = commit_result.get('task_id')
+ task_id = commit_result.get("task_id")
if task_id:
task_result = api_client.wait_for_task(task_id, timeout=60.0)
- task_status = task_result.get('status')
- assert task_status == 'completed', \
+ task_status = task_result.get("status")
+ assert task_status == "completed", (
f"Task should complete successfully, got status: {task_status}"
-
+ )
+
# 7. 获取会话信息验证持久化
response = api_client.get_session(session_id)
assert response.status_code == 200
get_data = response.json()
- assert get_data.get('status') == 'ok'
- assert 'result' in get_data
-
- get_result = get_data['result']
-
+ assert get_data.get("status") == "ok"
+ assert "result" in get_data
+
+ get_result = get_data["result"]
+
# 8. 验证会话基本信息
- assert 'session_id' in get_result
- assert get_result['session_id'] == session_id, "Session ID should match"
-
- assert 'user' in get_result
- assert 'account_id' in get_result['user']
- assert 'user_id' in get_result['user']
-
+ assert "session_id" in get_result
+ assert get_result["session_id"] == session_id, "Session ID should match"
+
+ assert "user" in get_result
+ assert "account_id" in get_result["user"]
+ assert "user_id" in get_result["user"]
+
# 9. 验证 commit_count(任务完成后应该 >= 1)
- if 'commit_count' in get_result:
- assert get_result['commit_count'] >= 1, \
+ if "commit_count" in get_result:
+ assert get_result["commit_count"] >= 1, (
f"Commit count should be at least 1 after task completed, got {get_result['commit_count']}."
-
+ )
+
# 10. 验证 last_commit_at(任务完成后应该有值)
- if 'last_commit_at' in get_result:
- assert get_result['last_commit_at'] != '', \
- f"last_commit_at should have value after commit, got empty string."
-
+ if "last_commit_at" in get_result:
+ assert get_result["last_commit_at"] != "", (
+ "last_commit_at should have value after commit, got empty string."
+ )
+
# 11. 使用 get_session_context 验证消息是否正确归档
response = api_client.get_session_context(session_id)
assert response.status_code == 200
context_data = response.json()
- assert context_data.get('status') == 'ok'
-
- context_result = context_data['result']
-
- # 消息归档后,messages 可能为空,消息内容在 latest_archive_overview 或 pre_archive_abstracts 中
- # 验证归档是否成功
- assert 'latest_archive_overview' in context_result, "Session context should contain 'latest_archive_overview'"
- assert 'pre_archive_abstracts' in context_result, "Session context should contain 'pre_archive_abstracts'"
-
+ assert context_data.get("status") == "ok"
+
+ context_result = context_data["result"]
+
+ # 消息归档后,messages 可能为空,消息内容在 latest_archive_overview 中
+ # 注意:pre_archive_abstracts 已不再返回(仅保留 latest_archive_overview)
+ assert "latest_archive_overview" in context_result, (
+ "Session context should contain 'latest_archive_overview'"
+ )
+
# 验证归档摘要不为空(说明消息已被处理)
- archive_overview = context_result['latest_archive_overview']
- archive_abstracts = context_result['pre_archive_abstracts']
-
- # 至少有一个归档
- assert len(archive_abstracts) >= 1 or archive_overview != '', \
- "At least one archive should exist after commit with messages"
-
+ archive_overview = context_result["latest_archive_overview"]
+
+ # 归档概览不为空即说明归档成功
+ assert archive_overview != "", (
+ "Archive overview should not be empty after commit with messages"
+ )
+
# 验证归档内容包含测试消息的关键词
archive_content = archive_overview.lower()
- found_in_archive = 'test message' in archive_content or 'session commit' in archive_content
- assert found_in_archive, \
+ found_in_archive = "test message" in archive_content or "session commit" in archive_content
+ assert found_in_archive, (
f"Archive overview should contain test message content. Got: {archive_overview[:200]}"
-
+ )
+
# 12. 验证会话状态(如果存在)
- if 'status' in get_result:
- assert get_result['status'] in ['active', 'committed', 'used'], \
+ if "status" in get_result:
+ assert get_result["status"] in ["active", "committed", "used"], (
f"Session status should be active/committed/used, got {get_result['status']}"
-
+ )
+
# 14. 验证memories_extracted(如果存在)
- if 'memories_extracted' in get_result:
- memories = get_result['memories_extracted']
+ if "memories_extracted" in get_result:
+ memories = get_result["memories_extracted"]
assert isinstance(memories, dict), "memories_extracted should be a dict"
-
+
# 15. 业务逻辑验证:验证会话可以被再次使用
response = api_client.add_message(session_id, "user", "Additional test message")
assert response.status_code == 200, "Session should still be usable after commit"
-
+
# 16. 业务逻辑验证:验证会话可以被再次提交
response = api_client.session_commit(session_id)
assert response.status_code == 200, "Session should be commitable multiple times"
-
+
print("✓ Session persistence and commit test passed")
diff --git a/tests/oc2ov_test/conftest.py b/tests/oc2ov_test/conftest.py
index 85aa43761..97e4aeacb 100644
--- a/tests/oc2ov_test/conftest.py
+++ b/tests/oc2ov_test/conftest.py
@@ -154,6 +154,12 @@ def pytest_html_results_table_row(report, cells):
"test_summary_generation_group_c": "长程总结生成-组C:OpenViking记忆优化项目,整合背景+讨论+闲聊生成完整总结",
"test_auto_session_basic": "自动Session ID测试:使用自动生成的session_id进行基本记忆写入和读取,验证Session ID自动管理功能",
"test_custom_session_prefix": "自定义Session ID测试:使用自定义前缀的session_id进行记忆写入和读取,验证自定义Session功能",
+ "test_memory_v2_preferences": "Memory V2偏好设置记忆:对话写入编程语言/编辑器/咖啡偏好→commit→验证preferences目录下新增记忆文件",
+ "test_memory_v2_entities": "Memory V2实体信息记忆:对话写入姓名/年龄/职业/公司/住址→commit→验证entities目录下新增记忆文件",
+ "test_memory_v2_events": "Memory V2事件记录记忆:对话写入技术分享会事件→commit→验证events目录下新增记忆文件",
+ "test_memory_v2_profile": "Memory V2用户画像记忆:对话写入技术负责人画像→commit→验证profile.md文件新增或更新",
+ "test_memory_v2_skills": "Memory V2技能记忆(Agent scope):对话写入Docker/K8s/CI-CD技能→commit→验证agent memories/skills目录下新增记忆文件",
+ "test_memory_v2_tools": "Memory V2工具记忆(Agent scope):对话写入Git/Jenkins/Prometheus工具使用→commit→验证agent memories/tools目录下新增记忆文件",
}
for test_name, desc in test_descriptions.items():
diff --git a/tests/oc2ov_test/tests/base_cli_test.py b/tests/oc2ov_test/tests/base_cli_test.py
index 28b7e16e5..ddfbf9bf9 100644
--- a/tests/oc2ov_test/tests/base_cli_test.py
+++ b/tests/oc2ov_test/tests/base_cli_test.py
@@ -9,21 +9,23 @@ import unittest
from config.settings import TEST_CONFIG
from utils.assertions import AssertionHelper
-from utils.openclaw_cli_client import OpenClawCLIClient
+from utils.openclaw_cli_client import OpenClawCLIClient, _wait_for_session_lock_release
from utils.test_utils import (
+ RetryManager,
SessionIdManager,
SmartWaiter,
- RetryManager,
- TestDataManager,
TestData,
+ TestDataManager,
get_default_data_manager,
)
+MIN_SYNC_WAIT_SECONDS = 5
+
class BaseOpenClawCLITest(unittest.TestCase):
"""
OpenClaw CLI 测试基类(增强版)
-
+
新增功能:
- Session ID 自动管理:每个测试类使用唯一的 session_id
- 智能等待策略:替代固定等待,支持轮询检查
@@ -39,9 +41,7 @@ class BaseOpenClawCLITest(unittest.TestCase):
"""
测试类初始化
"""
- cls._class_session_id = SessionIdManager.generate_test_class_session_id(
- cls.__name__
- )
+ cls._class_session_id = SessionIdManager.generate_test_class_session_id(cls.__name__)
cls.client = OpenClawCLIClient(session_id=cls._class_session_id)
cls.logger = logging.getLogger(cls.__name__)
cls.wait_time = TEST_CONFIG["wait_time"]
@@ -96,13 +96,19 @@ class BaseOpenClawCLITest(unittest.TestCase):
def wait_for_sync(self, seconds: int = None):
"""
- 等待记忆同步(固定等待)
+ 等待记忆同步(锁释放 + 最小等待)
Args:
- seconds: 等待秒数,默认使用配置的 wait_time
+ seconds: 等待秒数,默认使用配置的 wait_time,最小 MIN_SYNC_WAIT_SECONDS
"""
- wait_seconds = seconds or self.wait_time
- self.logger.info(f"等待 {wait_seconds} 秒,确认记忆同步...")
+ wait_seconds = max(seconds or self.wait_time, MIN_SYNC_WAIT_SECONDS)
+ self.logger.info(f"等待记忆同步 (锁释放 + {wait_seconds}秒)...")
+
+ lock_ok = _wait_for_session_lock_release(self.current_session_id)
+ if not lock_ok:
+ self.logger.warning("Session lock 未释放,额外等待 5 秒...")
+ time.sleep(5)
+
time.sleep(wait_seconds)
def smart_wait_for_sync(
@@ -110,16 +116,16 @@ class BaseOpenClawCLITest(unittest.TestCase):
check_message: str = None,
keywords: list = None,
timeout: float = None,
- poll_interval: float = 2.0,
+ poll_interval: float = 3.0,
) -> bool:
"""
- 智能等待记忆同步(轮询检查)
+ 智能等待记忆同步(锁释放 + 轮询检查)
Args:
check_message: 用于检查的消息(如不提供则使用固定等待)
keywords: 期望响应中包含的关键词
timeout: 超时时间(秒)
- poll_interval: 轮询间隔(秒)
+ poll_interval: 轮询间隔(秒),最小 3.0
Returns:
bool: 是否成功同步
@@ -128,9 +134,16 @@ class BaseOpenClawCLITest(unittest.TestCase):
self.wait_for_sync()
return True
+ poll_interval = max(poll_interval, 3.0)
timeout = timeout or self.wait_time * 3
+ lock_ok = _wait_for_session_lock_release(self.current_session_id)
+ if not lock_ok:
+ self.logger.warning("Session lock 未释放,额外等待 5 秒...")
+ time.sleep(5)
+
def check_response() -> bool:
+ _wait_for_session_lock_release(self.current_session_id)
response = self.client.send_message(check_message, session_id=self.current_session_id)
return self.assertion.assert_keywords_in_response(
response, keywords, require_all=True, case_sensitive=False
diff --git a/tests/oc2ov_test/tests/p0/test_memory_v2_full_suite.py b/tests/oc2ov_test/tests/p0/test_memory_v2_full_suite.py
index 3fdc198eb..f43a14280 100644
--- a/tests/oc2ov_test/tests/p0/test_memory_v2_full_suite.py
+++ b/tests/oc2ov_test/tests/p0/test_memory_v2_full_suite.py
@@ -20,6 +20,7 @@ from pathlib import Path
from typing import Any, Dict, List, Optional, Set, Tuple
import requests
+from utils.openclaw_cli_client import _wait_for_session_lock_release
from utils.test_utils import SessionIdManager
SERVER_URL = os.environ.get("SERVER_URL", "http://127.0.0.1:1933")
@@ -231,6 +232,8 @@ class MemoryV2TestSuite:
def run_openclaw_command(self, message: str, session_id: str) -> Dict[str, Any]:
"""执行 openclaw agent 命令"""
+ _wait_for_session_lock_release(session_id)
+
cmd = [
"openclaw",
"agent",
@@ -240,7 +243,9 @@ class MemoryV2TestSuite:
message,
"--json",
]
- result = subprocess.run(cmd, capture_output=True, text=True, timeout=120)
+ result = subprocess.run(cmd, capture_output=True, text=True, timeout=180)
+
+ _wait_for_session_lock_release(session_id)
if result.returncode != 0:
raise Exception(f"OpenClaw command failed: {result.stderr}")
diff --git a/tests/oc2ov_test/upgrade_openviking.sh b/tests/oc2ov_test/upgrade_openviking.sh
index 8a93d117d..bd475fee1 100644
--- a/tests/oc2ov_test/upgrade_openviking.sh
+++ b/tests/oc2ov_test/upgrade_openviking.sh
@@ -292,19 +292,53 @@ fi
sleep 3
-# Verify gateway is stopped
-if ps aux | grep -v grep | grep -q "[o]penclaw"; then
- log "⚠️ OpenClaw process still running, killing forcefully..."
+# Verify gateway is stopped - kill ALL openclaw processes to prevent multi-instance conflicts
+REMAINING_PIDS=$(ps aux | grep -v grep | grep "[o]penclaw" | awk '{print $2}' || true)
+if [ -n "$REMAINING_PIDS" ]; then
+ REMAINING_COUNT=$(echo "$REMAINING_PIDS" | wc -l)
+ log "⚠️ $REMAINING_COUNT OpenClaw process(es) still running, killing forcefully..."
+ log "PIDs: $REMAINING_PIDS"
pkill -9 -f "openclaw" || true
sleep 2
+
+ STILL_RUNNING=$(ps aux | grep -v grep | grep "[o]penclaw" | awk '{print $2}' || true)
+ if [ -n "$STILL_RUNNING" ]; then
+ log "⚠️ WARNING: Some processes could not be killed: $STILL_RUNNING"
+ else
+ log "✅ All OpenClaw processes terminated"
+ fi
fi
log "✅ OpenClaw gateway stopped"
-# Step 2: Clear OpenClaw cache to force reload Python packages
-log "Step 2: Clearing OpenClaw cache..."
+# Step 2: Clean up OpenClaw stale state (lock files, session locks, cache)
+log "Step 2: Pre-start cleanup for OpenClaw..."
+
+LOCK_COUNT=0
+SESSION_LOCK_COUNT=0
+
+if [ -d ~/.openclaw ]; then
+ LOCK_COUNT=$(find ~/.openclaw -name "*.lock" -type f 2>/dev/null | wc -l)
+ if [ "$LOCK_COUNT" -gt 0 ]; then
+ log "Found $LOCK_COUNT stale lock file(s), removing..."
+ find ~/.openclaw -name "*.lock" -type f -delete 2>/dev/null || true
+ log "✅ Stale lock files removed"
+ else
+ log "No stale lock files found"
+ fi
+
+ SESSION_LOCK_COUNT=$(find ~/.openclaw/agents -name "*.jsonl.lock" -type f 2>/dev/null | wc -l)
+ if [ "$SESSION_LOCK_COUNT" -gt 0 ]; then
+ log "Found $SESSION_LOCK_COUNT stale session lock(s), removing..."
+ find ~/.openclaw/agents -name "*.jsonl.lock" -type f -delete 2>/dev/null || true
+ log "✅ Stale session locks removed"
+ fi
+else
+ log "~/.openclaw directory not found, skipping lock cleanup"
+fi
+
rm -rf ~/.openclaw/cache/* 2>/dev/null || true
rm -rf ~/.openclaw/tmp/* 2>/dev/null || true
-log "✅ Cache cleared"
+log "✅ Pre-start cleanup completed (locks: $LOCK_COUNT, session locks: $SESSION_LOCK_COUNT, cache cleared)"
# Step 3: Verify OpenViking installation path before starting
log "Step 3: Verifying OpenViking installation path..."
diff --git a/tests/oc2ov_test/utils/openclaw_cli_client.py b/tests/oc2ov_test/utils/openclaw_cli_client.py
index d23818f80..d7ec6f3d5 100644
--- a/tests/oc2ov_test/utils/openclaw_cli_client.py
+++ b/tests/oc2ov_test/utils/openclaw_cli_client.py
@@ -2,13 +2,45 @@
OpenClaw CLI 客户端封装 - 使用 openclaw agent 命令
"""
+import glob
import json
import logging
+import os
import subprocess
+import time
from typing import Any, Dict, Optional
logger = logging.getLogger(__name__)
+OPENCLAW_HOME = os.path.expanduser("~/.openclaw")
+SESSION_LOCK_TIMEOUT = 15
+SESSION_LOCK_POLL_INTERVAL = 0.5
+
+
+def _find_session_lock(session_id: str) -> Optional[str]:
+ for pattern in [
+ os.path.join(OPENCLAW_HOME, "agents", "*", "sessions", f"{session_id}.jsonl.lock"),
+ os.path.join(OPENCLAW_HOME, "agents", "main", "sessions", f"{session_id}.jsonl.lock"),
+ ]:
+ matches = glob.glob(pattern)
+ if matches:
+ return matches[0]
+ return None
+
+
+def _wait_for_session_lock_release(session_id: str, timeout: float = SESSION_LOCK_TIMEOUT) -> bool:
+ lock_path = _find_session_lock(session_id)
+ if lock_path is None:
+ return True
+ start = time.time()
+ while time.time() - start < timeout:
+ if not os.path.exists(lock_path):
+ logger.info(f"Session lock released: {lock_path}")
+ return True
+ time.sleep(SESSION_LOCK_POLL_INTERVAL)
+ logger.warning(f"Session lock still held after {timeout}s: {lock_path}")
+ return False
+
class OpenClawCLIClient:
"""
@@ -53,12 +85,18 @@ class OpenClawCLIClient:
logger.info(f"输入消息: {message}")
logger.info(f"完整命令: {' '.join(cmd)}")
+ lock_released = _wait_for_session_lock_release(target_session_id)
+ if not lock_released:
+ logger.warning("Session lock not released, request may fail with lock timeout")
+
logger.info(f"⏳ 等待响应 (超时: {self.timeout}秒)...")
result = subprocess.run(cmd, capture_output=True, text=True, timeout=self.timeout)
logger.info(f"✅ 命令执行完成 - 返回码: {result.returncode}")
+ _wait_for_session_lock_release(target_session_id)
+
if result.returncode != 0:
error_msg = f"命令执行失败: {result.stderr}"
logger.error("=" * 80)
diff --git a/tests/oc2ov_test/utils/test_utils.py b/tests/oc2ov_test/utils/test_utils.py
index 2c1bacb1f..16802bd25 100644
--- a/tests/oc2ov_test/utils/test_utils.py
+++ b/tests/oc2ov_test/utils/test_utils.py
@@ -20,6 +20,14 @@ T = TypeVar("T")
# 使用 UUID 格式,确保 OpenClaw 直接使用,不做 SHA256 转换
FIXED_SESSION_ID = "00000000-0000-0000-0000-000000000001"
+# CI 环境下基于名称生成确定性 UUID 的命名空间
+_CI_SESSION_NAMESPACE = uuid.UUID("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
+
+
+def _ci_deterministic_uuid(name: str) -> str:
+ """基于名称生成确定性 UUID,CI 环境下同一名称始终返回同一 UUID。"""
+ return str(uuid.uuid5(_CI_SESSION_NAMESPACE, name))
+
def _is_ci_environment() -> bool:
"""检测是否在 CI 环境中运行"""
@@ -68,10 +76,11 @@ class SessionIdManager:
Returns:
str: UUID 格式的 session_id
"""
- # 在 CI 环境中使用固定的 session ID
+ # 在 CI 环境中使用基于前缀的确定性 session ID,避免不同测试互相干扰
if _is_ci_environment():
- logger.info(f"CI 环境检测到,使用固定 Session ID: {FIXED_SESSION_ID}")
- return FIXED_SESSION_ID
+ session_id = _ci_deterministic_uuid(f"session:{prefix}")
+ logger.info(f"CI 环境检测到,使用确定性 Session ID: {session_id} (prefix: {prefix})")
+ return session_id
# 生成 UUID 格式的 session ID
session_id = str(uuid.uuid4())
@@ -89,10 +98,13 @@ class SessionIdManager:
Returns:
str: 唯一的 session_id
"""
- # 在 CI 环境中使用固定的 session ID
+ # 在 CI 环境中使用基于类名的确定性 session ID,避免不同测试类互相干扰
if _is_ci_environment():
- logger.info(f"CI 环境检测到,使用固定 Session ID: {FIXED_SESSION_ID}")
- return FIXED_SESSION_ID
+ session_id = _ci_deterministic_uuid(f"class:{test_class_name}")
+ logger.info(
+ f"CI 环境检测到,使用确定性 Session ID: {session_id} (class: {test_class_name})"
+ )
+ return session_id
return f"test_{test_class_name}_{uuid.uuid4().hex[:8]}"
@@ -108,10 +120,14 @@ class SessionIdManager:
Returns:
str: 唯一的 session_id
"""
- # 在 CI 环境中使用固定的 session ID
+ # 在 CI 环境中使用基于类名+方法名的确定性 session ID
if _is_ci_environment():
- logger.info(f"CI 环境检测到,使用固定 Session ID: {FIXED_SESSION_ID}")
- return FIXED_SESSION_ID
+ session_id = _ci_deterministic_uuid(f"method:{test_class_name}:{test_method_name}")
+ logger.info(
+ f"CI 环境检测到,使用确定性 Session ID: {session_id} "
+ f"(class: {test_class_name}, method: {test_method_name})"
+ )
+ return session_id
return f"test_{test_class_name}_{test_method_name}_{uuid.uuid4().hex[:8]}"