Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.
Python agent-contextagent-memoryagent-pluginsagentic-ragcontext-databasedsh-pluginknowledge-basememorymemory-managementself-evolvingvector-database
fix(security): clean up code scanning and runtime findings (#1596)
Qin Haojie38c324bc9797
diff --git a/benchmark/RAG/scripts/download_dataset.py b/benchmark/RAG/scripts/download_dataset.pyindex 3a32ff639..5f00d7f7b 100644--- a/benchmark/RAG/scripts/download_dataset.py+++ b/benchmark/RAG/scripts/download_dataset.py@@ -6,12 +6,10 @@ Supports URL downloads from GitHub, S3, etc.import argparseimport hashlib-import json-import osimport shutilimport sysfrom pathlib import Path-from typing import Dict, List, Optional+from typing import Dict, Optionalfrom urllib.parse import urlparseimport requests@@ -26,9 +24,7 @@ DATASET_SOURCES = {"source_type": "url","url": "https://raw.githubusercontent.com/snap-research/locomo/main/data/locomo10.json","checksum": "",- "files": [- "locomo10.json"- ]+ "files": ["locomo10.json"],},#"SyllabusQA": {@@ -39,23 +35,19 @@ DATASET_SOURCES = {"data/dataset_split/train.csv","data/dataset_split/val.csv","data/dataset_split/test.csv",- "syllabi/syllabi_redacted/word/"+ "syllabi/syllabi_redacted/word/",],- "extract_subdir": "SyllabusQA-main"+ "extract_subdir": "SyllabusQA-main",},#"Qasper": {"source_type": "url","urls": ["https://qasper-dataset.s3.us-west-2.amazonaws.com/qasper-train-dev-v0.3.tgz",- "https://qasper-dataset.s3.us-west-2.amazonaws.com/qasper-test-and-evaluator-v0.3.tgz"+ "https://qasper-dataset.s3.us-west-2.amazonaws.com/qasper-test-and-evaluator-v0.3.tgz",],"checksum": "",- "files": [- "qasper-train-v0.3.json",- "qasper-dev-v0.3.json",- "qasper-test-v0.3.json"- ]+ "files": ["qasper-train-v0.3.json", "qasper-dev-v0.3.json", "qasper-test-v0.3.json"],},#"FinanceBench": {@@ -65,12 +57,13 @@ DATASET_SOURCES = {"files": ["data/financebench_open_source.jsonl","data/financebench_document_information.jsonl",- "pdfs/"+ "pdfs/",],- "extract_subdir": "financebench-main"+ "extract_subdir": "financebench-main",},}+def calculate_checksum(file_path: Path, algorithm: str = "sha256") -> str:"""Calculate checksum of a file."""hash_obj = hashlib.new(algorithm)@@ -83,20 +76,23 @@ def calculate_checksum(file_path: Path, algorithm: str = "sha256") -> str:def download_file(url: str, dest_path: Path, chunk_size: int = 8192) -> bool:"""Download a file with progress bar."""dest_path.parent.mkdir(parents=True, exist_ok=True)-+try:response = requests.get(url, stream=True, timeout=30)response.raise_for_status()-+total_size = int(response.headers.get("content-length", 0))-- with open(dest_path, "wb") as f, tqdm(- desc=f"Downloading {dest_path.name}",- total=total_size,- unit="B",- unit_scale=True,- unit_divisor=1024,- ) as pbar:++ with (+ open(dest_path, "wb") as f,+ tqdm(+ desc=f"Downloading {dest_path.name}",+ total=total_size,+ unit="B",+ unit_scale=True,+ unit_divisor=1024,+ ) as pbar,+ ):for chunk in response.iter_content(chunk_size=chunk_size):if chunk:f.write(chunk)@@ -109,26 +105,37 @@ def download_file(url: str, dest_path: Path, chunk_size: int = 8192) -> bool:return False-def extract_archive(archive_path: Path, extract_to: Path, extract_subdir: Optional[str] = None) -> bool:+def extract_archive(+ archive_path: Path, extract_to: Path, extract_subdir: Optional[str] = None+) -> bool:"""Extract archive file (zip, tar.gz, etc.)."""- import zipfileimport tarfile-+ import zipfile+try:temp_extract_dir = extract_to / ".temp_extract"temp_extract_dir.mkdir(parents=True, exist_ok=True)-+if archive_path.suffix == ".zip":with zipfile.ZipFile(archive_path, "r") as zip_ref:zip_ref.extractall(temp_extract_dir)elif archive_path.suffix in [".tar", ".gz", ".tgz"]:with tarfile.open(archive_path, "r:*") as tar_ref:- tar_ref.extractall(temp_extract_dir)+ safe_members = []+ extract_root = temp_extract_dir.resolve()+ for member in tar_ref.getmembers():+ if member.issym() or member.islnk():+ raise ValueError(f"Unsafe link entry in tar archive: {member.name}")+ member_path = (extract_root / member.name).resolve()+ if not member_path.is_relative_to(extract_root):+ raise ValueError(f"Unsafe tar member path: {member.name}")+ safe_members.append(member)+ tar_ref.extractall(temp_extract_dir, members=safe_members)else:print(f"Unsupported archive format: {archive_path.suffix}")shutil.rmtree(temp_extract_dir)return False-+if extract_subdir:source_dir = temp_extract_dir / extract_subdirif source_dir.exists() and source_dir.is_dir():@@ -151,7 +158,7 @@ def extract_archive(archive_path: Path, extract_to: Path, extract_subdir: Optionelse:dest_item.unlink()shutil.move(str(item), str(dest_item))-+shutil.rmtree(temp_extract_dir)return Trueexcept Exception as e:@@ -166,20 +173,20 @@ def verify_dataset(dataset_name: str, dataset_dir: Path) -> bool:if dataset_name not in DATASET_SOURCES:print(f"Unknown dataset: {dataset_name}")return False-+source = DATASET_SOURCES[dataset_name]missing_files = []-+for file_path in source["files"]:full_path = dataset_dir / file_path# Check if path exists (either file or directory)if not full_path.exists():missing_files.append(file_path)-+if missing_files:print(f"Missing files for {dataset_name}: {missing_files}")return False-+print(f"✓ {dataset_name} verified successfully")return True@@ -191,41 +198,37 @@ def is_archive_file(file_path: Path) -> bool:def download_from_url(- source: Dict,- output_dir: Path,- dataset_name: str,- force: bool = False,- verify: bool = True+ source: Dict, output_dir: Path, dataset_name: str, force: bool = False, verify: bool = True) -> bool:"""Download dataset from URL. Supports both archives and single files.Supports single url or multiple urls via urls field."""dataset_dir = output_dir / dataset_name-+if dataset_dir.exists() and not force:print(f"{dataset_name} already exists at {dataset_dir}, skipping download")if verify:return verify_dataset(dataset_name, dataset_dir)return True-+print(f"Downloading {dataset_name}...")-+# Support single url or multiple urlsurls = source.get("urls", [source.get("url")]) if source.get("urls") else [source.get("url")]-+success = Truefor url in urls:if not url:continue-+parsed_url = urlparse(url)file_name = Path(parsed_url.path).namedownloaded_path = output_dir / file_name-+if not download_file(url, downloaded_path):success = Falsecontinue-+if "checksum" in source and source["checksum"]:algo, expected_checksum = source["checksum"].split(":", 1)actual_checksum = calculate_checksum(downloaded_path, algo)@@ -237,7 +240,7 @@ def download_from_url(success = Falsecontinueprint(f"✓ Checksum verified for {dataset_name}")-+if is_archive_file(downloaded_path):extract_subdir = source.get("extract_subdir")if not extract_archive(downloaded_path, dataset_dir, extract_subdir):@@ -250,47 +253,44 @@ def download_from_url(dest_path = dataset_dir / file_nameshutil.move(str(downloaded_path), str(dest_path))print(f"✓ Saved single file to {dest_path}")-+if verify and not verify_dataset(dataset_name, dataset_dir):return False-+if success:print(f"✓ {dataset_name} downloaded successfully to {dataset_dir}")return successdef download_dataset(- dataset_name: str,- output_dir: Path,- force: bool = False,- verify: bool = True+ dataset_name: str, output_dir: Path, force: bool = False, verify: bool = True) -> bool:"""Download a single dataset."""if dataset_name not in DATASET_SOURCES:print(f"Unknown dataset: {dataset_name}")return False-+source = DATASET_SOURCES[dataset_name]dataset_dir = output_dir / dataset_name-+if dataset_dir.exists() and not force:print(f"{dataset_name} already exists at {dataset_dir}, skipping download")if verify:return verify_dataset(dataset_name, dataset_dir)return True-+success = download_from_url(source, output_dir, dataset_name, force, verify)-+if success and verify:return verify_dataset(dataset_name, dataset_dir)-+return successdef main():# Check if any datasets are configured- configured_datasets = [k for k in DATASET_SOURCES.keys() if not k.startswith('#')]-+ configured_datasets = [k for k in DATASET_SOURCES.keys() if not k.startswith("#")]+if not configured_datasets:print("=" * 80)print("No datasets configured!")@@ -302,60 +302,45 @@ def main():print("See README_DATASET_CONFIG.md for detailed instructions.")print("=" * 80)return 1-- parser = argparse.ArgumentParser(- description="Download datasets for RAG benchmark"- )++ parser = argparse.ArgumentParser(description="Download datasets for RAG benchmark")parser.add_argument(- "--dataset", "-d",+ "--dataset",+ "-d",type=str,choices=configured_datasets + ["all"],default="all",- help="Dataset to download (default: all)"+ help="Dataset to download (default: all)",)parser.add_argument(- "--output-dir", "-o",+ "--output-dir",+ "-o",type=Path,default=Path(__file__).parent.parent / "raw_data",- help="Output directory (default: raw_data/)"- )- parser.add_argument(- "--force", "-f",- action="store_true",- help="Force re-download even if dataset exists"+ help="Output directory (default: raw_data/)",)parser.add_argument(- "--no-verify",- action="store_true",- help="Skip dataset verification"+ "--force", "-f", action="store_true", help="Force re-download even if dataset exists")-+ parser.add_argument("--no-verify", action="store_true", help="Skip dataset verification")+args = parser.parse_args()-+output_dir = args.output_dir.resolve()output_dir.mkdir(parents=True, exist_ok=True)-- datasets = (- configured_datasets- if args.dataset == "all"- else [args.dataset]- )-++ datasets = configured_datasets if args.dataset == "all" else [args.dataset]+print(f"Downloading datasets to: {output_dir}")print(f"Datasets: {', '.join(datasets)}")print()-+success_count = 0for dataset in datasets:- if download_dataset(- dataset,- output_dir,- args.force,- not args.no_verify- ):+ if download_dataset(dataset, output_dir, args.force, not args.no_verify):success_count += 1print()-+print(f"Download complete: {success_count}/{len(datasets)} successful")return 0 if success_count == len(datasets) else 1diff --git a/bot/demo/werewolf/werewolf_server.py b/bot/demo/werewolf/werewolf_server.pyindex d225a5dad..a1b2221c8 100644--- a/bot/demo/werewolf/werewolf_server.py+++ b/bot/demo/werewolf/werewolf_server.py@@ -1,19 +1,21 @@"""Werewolf game server with message routing and Web UI."""import asyncio+import htmlimport jsonimport reimport time+from dataclasses import dataclass, fieldfrom datetime import datetimefrom pathlib import Pathfrom typing import Any, Dict, List, Optional+from urllib.parse import quoteimport httpximport typerfrom fastapi import FastAPI-from fastapi.responses import HTMLResponse, FileResponse, JSONResponse+from fastapi.responses import FileResponse, HTMLResponse, JSONResponsefrom loguru import logger-from dataclasses import dataclass, fieldapp = typer.Typer()@@ -26,15 +28,17 @@ app = typer.Typer()@dataclassclass ChatMessage:"""A chat message in the history."""+channel_id: strcontent: stris_user: bool- timestamp: float = field(default_factory=lambda: __import__('time').time())+ timestamp: float = field(default_factory=lambda: __import__("time").time())@dataclassclass PendingReply:"""Track pending player replies."""+channel_id: strmessage_id: strtimestamp: float@@ -43,6 +47,7 @@ class PendingReply:@dataclassclass GameState:"""Shared game state."""+game_id: strvikingbot_url: strconfig_path: Path@@ -106,7 +111,9 @@ def get_storage_path(config: Dict[str, Any]) -> Path:storage_config = config.get("storage", {})bot_config = config.get("bot", {})sandbox_config = bot_config.get("sandbox", {})- storage_workspace = sandbox_config.get("storage_workspace") or storage_config.get("workspace", "~/.openviking/data")+ storage_workspace = sandbox_config.get("storage_workspace") or storage_config.get(+ "workspace", "~/.openviking/data"+ )return Path(storage_workspace).expanduser()@@ -195,7 +202,14 @@ def extract_content_without_mentions(content: str) -> str:def is_waiting_like_reply(content: str) -> bool:"""Check whether god reply is an initialization/paused-state reply."""text = str(content or "")- waiting_markers = ["初始化完成", "初始化已完成", "等待开始", "等待指令", "等待继续",'等待"开始"指令']+ waiting_markers = [+ "初始化完成",+ "初始化已完成",+ "等待开始",+ "等待指令",+ "等待继续",+ '等待"开始"指令',+ ]return any(marker in text for marker in waiting_markers)@@ -210,12 +224,7 @@ def parse_game_record(game_record_path: Path) -> Dict[str, Any]:- game_time: "白天", "黑夜", or None- players: list of dicts with "id", "role", "status""""- result = {- "game_status": None,- "game_result": None,- "game_time": None,- "players": []- }+ result = {"game_status": None, "game_result": None, "game_time": None, "players": []}if not game_record_path.exists():logger.warning(f"GAME_RECORD.md not found at {game_record_path}")@@ -249,12 +258,10 @@ def parse_game_record(game_record_path: Path) -> Dict[str, Any]:player_id = cells[1]role = cells[2]status = cells[3].strip("[]")- if player_id and not all(c == '-' for c in player_id):- result["players"].append({- "id": player_id,- "role": role,- "status": status- })+ if player_id and not all(c == "-" for c in player_id):+ result["players"].append(+ {"id": player_id, "role": role, "status": status}+ )except Exception as e:logger.warning(f"Error parsing GAME_RECORD.md: {e}")@@ -290,7 +297,16 @@ def normalize_leaderboard_game_result(game_result: Any) -> str:def is_wolf_role_for_leaderboard(role: Any) -> bool:role_str = str(role or "").strip()- return role_str in {"Werewolf", "WhiteWolfKing", "WolfCub", "WolfBeauty", "狼人", "白狼王", "狼美人", "狼崽"}+ return role_str in {+ "Werewolf",+ "WhiteWolfKing",+ "WolfCub",+ "WolfBeauty",+ "狼人",+ "白狼王",+ "狼美人",+ "狼崽",+ }def is_dead_status_for_leaderboard(status: Any) -> bool:@@ -298,21 +314,27 @@ def is_dead_status_for_leaderboard(status: Any) -> bool:return any(token in status_str for token in ("死亡", "出局", "淘汰"))-def build_leaderboard_game_from_record(parsed_record: Dict[str, Any], *, session_id: str, game_mode: str) -> Dict[str, Any]:+def build_leaderboard_game_from_record(+ parsed_record: Dict[str, Any], *, session_id: str, game_mode: str+) -> Dict[str, Any]:winner = normalize_leaderboard_game_result(parsed_record.get("game_result"))players = []for player in parsed_record.get("players", []):role = player.get("role", "")dead = is_dead_status_for_leaderboard(player.get("status", ""))- won = (winner == "狼人胜利" and is_wolf_role_for_leaderboard(role)) or (winner == "好人胜利" and not is_wolf_role_for_leaderboard(role))+ won = (winner == "狼人胜利" and is_wolf_role_for_leaderboard(role)) or (+ winner == "好人胜利" and not is_wolf_role_for_leaderboard(role)+ )score = (2 if won else 0) + (1 if not dead else 0)- players.append({- "id": player.get("id", ""),- "role": role,- "won": won,- "dead": dead,- "score": score,- })+ players.append(+ {+ "id": player.get("id", ""),+ "role": role,+ "won": won,+ "dead": dead,+ "score": score,+ }+ )return {"session_id": session_id,@@ -350,7 +372,11 @@ def save_game_to_leaderboard_from_record(game_mode: str,) -> Dict[str, Any]:if str(game_mode or "") == "human_player":- return {"success": True, "skipped": True, "reason": "human_player games are excluded from leaderboard"}+ return {+ "success": True,+ "skipped": True,+ "reason": "human_player games are excluded from leaderboard",+ }if not session_id:return {"success": False, "error": "missing_session_id"}@@ -371,15 +397,24 @@ def save_game_to_leaderboard_from_record(return {"success": False, "error": "missing_players"}leaderboard = load_leaderboard(storage_path)- if any(str(game.get("session_id") or "") == session_id for game in leaderboard.get("games", [])):- return {"success": True, "skipped": True, "reason": "already_saved", "leaderboard": leaderboard}+ if any(+ str(game.get("session_id") or "") == session_id for game in leaderboard.get("games", [])+ ):+ return {+ "success": True,+ "skipped": True,+ "reason": "already_saved",+ "leaderboard": leaderboard,+ }- leaderboard["games"].append({- "session_id": session_id,- "timestamp": time.time(),- "winner": game_data.get("winner", ""),- "players": game_data.get("players", []),- })+ leaderboard["games"].append(+ {+ "session_id": session_id,+ "timestamp": time.time(),+ "winner": game_data.get("winner", ""),+ "players": game_data.get("players", []),+ }+ )for player in game_data.get("players", []):player_id = player.get("id", "")@@ -436,7 +471,9 @@ def save_conversation_to_file(storage_path: Path, session_id: str, messages: Lislogger.info(f"Conversation saved to {file_path}")-def get_player_game_md_path(storage_path: Path, channel_id: str, human_player_channel: str = "human") -> Path:+def get_player_game_md_path(+ storage_path: Path, channel_id: str, human_player_channel: str = "human"+) -> Path:"""Get GAME.md path for a player channel."""bot_workspace = storage_path / "bot" / "workspace"if channel_id == human_player_channel:@@ -444,7 +481,9 @@ def get_player_game_md_path(storage_path: Path, channel_id: str, human_player_chreturn bot_workspace / f"bot_api__{channel_id}" / "GAME.md"-def get_players_snapshot(storage_path: Path, channels: List[str], human_player_channel: str = "human") -> List[Dict[str, Any]]:+def get_players_snapshot(+ storage_path: Path, channels: List[str], human_player_channel: str = "human"+) -> List[Dict[str, Any]]:"""Read current players info from GAME.md files."""players = []player_idx = 1@@ -462,11 +501,13 @@ def get_players_snapshot(storage_path: Path, channels: List[str], human_player_cif role_match:role = role_match.group(1).strip()- players.append({- "id": ch,- "seat": player_idx,- "role": role,- })+ players.append(+ {+ "id": ch,+ "seat": player_idx,+ "role": role,+ }+ )player_idx += 1return players@@ -492,7 +533,9 @@ def save_runtime_state(storage_path: Path, *, game_mode: str) -> Path:"saved_at": time.time(),"game_mode": game_mode,}- runtime_state_path.write_text(json.dumps(payload, ensure_ascii=False, indent=2), encoding="utf-8")+ runtime_state_path.write_text(+ json.dumps(payload, ensure_ascii=False, indent=2), encoding="utf-8"+ )return runtime_state_path@@ -509,7 +552,9 @@ def load_runtime_state(storage_path: Path) -> Dict[str, Any]:return {}-def build_replay_base_info(game_record_text: str, parsed_record: Dict[str, Any], players: List[Dict[str, Any]]) -> Dict[str, Any]:+def build_replay_base_info(+ game_record_text: str, parsed_record: Dict[str, Any], players: List[Dict[str, Any]]+) -> Dict[str, Any]:"""Build minimal replay base info from archived game record and players."""dead = []record_player_by_id = {player.get("id"): player for player in parsed_record.get("players", [])}@@ -536,7 +581,9 @@ def build_replay_base_info(game_record_text: str, parsed_record: Dict[str, Any],}-def archive_replay_state(storage_path: Path, channels: List[str], session_id: str, force: bool = False) -> Optional[Path]:+def archive_replay_state(+ storage_path: Path, channels: List[str], session_id: str, force: bool = False+) -> Optional[Path]:"""Archive replay state for a session so replay does not depend on live files."""if not session_id:return None@@ -561,7 +608,9 @@ def archive_replay_state(storage_path: Path, channels: List[str], session_id: st"players": players,"base_info": build_replay_base_info(game_record, parsed_record, players),}- replay_state_path.write_text(json.dumps(payload, ensure_ascii=False, indent=2), encoding="utf-8")+ replay_state_path.write_text(+ json.dumps(payload, ensure_ascii=False, indent=2), encoding="utf-8"+ )logger.info(f"Replay state archived to {replay_state_path}")return replay_state_path@@ -572,7 +621,9 @@ def build_restart_message(state: GameState) -> str:player_idx = 1for ch in state.channels:if ch != "god" and player_idx <= 8:- game_md_path = get_player_game_md_path(state.storage_path, ch, state.human_player_channel)+ game_md_path = get_player_game_md_path(+ state.storage_path, ch, state.human_player_channel+ )player_list.append(f"{player_idx}号: {ch},GAME.md地址:{game_md_path}")player_idx += 1@@ -583,7 +634,7 @@ def build_restart_message(state: GameState) -> str:游戏配置:- 玩家数: {len(player_list)}- 玩家列表:-{chr(10).join(f' - {p}' for p in player_list)}+{chr(10).join(f" - {p}" for p in player_list)}- GAME_RECORD.md 位置:{bot_workspace}/bot_api__god/GAME_RECORD.md- 对话记录位置:{bot_workspace}/werewolf/CONVERSATION_{state.session_id}.md@@ -596,11 +647,15 @@ def ensure_human_workspace(storage_path: Path, human_channel: str = "human") ->human_workspace.mkdir(parents=True, exist_ok=True)human_game_md = human_workspace / "GAME.md"if not human_game_md.exists():- human_game_md.write_text("# 真实玩家游戏文件\n\n请编辑此文件来设置你的角色和状态。\n", encoding="utf-8")+ human_game_md.write_text(+ "# 真实玩家游戏文件\n\n请编辑此文件来设置你的角色和状态。\n", encoding="utf-8"+ )return human_game_md-def build_channels_for_game_mode(config: Dict[str, Any], game_mode: str, human_channel: str = "human") -> List[str]:+def build_channels_for_game_mode(+ config: Dict[str, Any], game_mode: str, human_channel: str = "human"+) -> List[str]:"""Build channel list for the requested game mode."""channels = get_bot_channels(config)if game_mode != "human_player":@@ -619,7 +674,9 @@ def apply_game_mode_to_state(state: GameState, game_mode: str) -> None:state.game_mode = game_modestate.has_human_player = game_mode == "human_player"state.human_player_channel = "human"- state.channels = build_channels_for_game_mode(state.config, game_mode, state.human_player_channel)+ state.channels = build_channels_for_game_mode(+ state.config, game_mode, state.human_player_channel+ )state.waiting_for_human = Falsestate.human_player_message = Nonestate.human_messages.clear()@@ -633,7 +690,11 @@ def apply_game_mode_to_state(state: GameState, game_mode: str) -> None:def cancel_auto_start_task(state: GameState):"""Cancel pending auto-start task if any."""current_task = asyncio.current_task()- if state.auto_start_task and not state.auto_start_task.done() and state.auto_start_task is not current_task:+ if (+ state.auto_start_task+ and not state.auto_start_task.done()+ and state.auto_start_task is not current_task+ ):state.auto_start_task.cancel()if state.auto_start_task is not current_task:state.auto_start_task = None@@ -646,7 +707,11 @@ def get_completed_games_in_auto_run(state: GameState) -> int:def get_remaining_auto_run_games(state: GameState) -> Optional[int]:"""Return remaining games in fixed mode, None for non-fixed modes."""- if not state.auto_run_enabled or state.auto_run_mode != "fixed" or not state.auto_run_target_games:+ if (+ not state.auto_run_enabled+ or state.auto_run_mode != "fixed"+ or not state.auto_run_target_games+ ):return Nonereturn max(0, state.auto_run_target_games - get_completed_games_in_auto_run(state))@@ -686,7 +751,9 @@ async def stop_router_task(state: GameState):state.router_task = None-async def begin_game_loop(state: GameState, initial_message: str, *, reset_game_flags: bool = False) -> None:+async def begin_game_loop(+ state: GameState, initial_message: str, *, reset_game_flags: bool = False+) -> None:"""Start the router loop with a god message."""if "god" not in state.channels:raise ValueError("god channel not found")@@ -742,7 +809,11 @@ async def continue_current_game(state: GameState) -> None:def schedule_next_game(state: GameState, delay_seconds: float = 1.5):"""Schedule automatic restart + start for the next game."""- if not should_continue_auto_run(state) or state.auto_restart_scheduled or state.starting_next_game:+ if (+ not should_continue_auto_run(state)+ or state.auto_restart_scheduled+ or state.starting_next_game+ ):returnstate.auto_restart_scheduled = True@@ -794,9 +865,7 @@ def load_latest_conversation(storage_path: Path) -> tuple[List[ChatMessage], Opt# Find all conversation filesconversation_files = sorted(- bot_workspace.glob("CONVERSATION_*.md"),- key=lambda p: p.stat().st_mtime,- reverse=True+ bot_workspace.glob("CONVERSATION_*.md"), key=lambda p: p.stat().st_mtime, reverse=True)if not conversation_files:@@ -813,13 +882,12 @@ def load_latest_conversation(storage_path: Path) -> tuple[List[ChatMessage], Opt# Parse the markdown formatcurrent_speaker = None- current_timestamp = Nonecurrent_content = []for line in content.split("\n"):line = line.rstrip()# Match section header: ## [HH:MM:SS] speaker- match = re.match(r'^## \[([0-9:]+)\] (.+)$', line)+ match = re.match(r"^## \[([0-9:]+)\] (.+)$", line)if match:# Save previous messageif current_speaker and current_content:@@ -830,15 +898,16 @@ def load_latest_conversation(storage_path: Path) -> tuple[List[ChatMessage], Optexcept ValueError:ts = time.time()- messages.append(ChatMessage(- channel_id=current_speaker,- content="\n".join(current_content).strip(),- is_user=(current_speaker == "admin"),- timestamp=ts- ))+ messages.append(+ ChatMessage(+ channel_id=current_speaker,+ content="\n".join(current_content).strip(),+ is_user=(current_speaker == "admin"),+ timestamp=ts,+ )+ )# Start new message- time_str = match.group(1)current_speaker = match.group(2)current_content = []elif line and not line.startswith("# "):@@ -853,14 +922,18 @@ def load_latest_conversation(storage_path: Path) -> tuple[List[ChatMessage], Optexcept ValueError:ts = time.time()- messages.append(ChatMessage(- channel_id=current_speaker,- content="\n".join(current_content).strip(),- is_user=(current_speaker == "admin"),- timestamp=ts- ))+ messages.append(+ ChatMessage(+ channel_id=current_speaker,+ content="\n".join(current_content).strip(),+ is_user=(current_speaker == "admin"),+ timestamp=ts,+ )+ )- logger.info(f"Loaded {len(messages)} messages from latest conversation, session_id={session_id}")+ logger.info(+ f"Loaded {len(messages)} messages from latest conversation, session_id={session_id}"+ )return messages, session_id@@ -900,6 +973,7 @@ async def broadcast_to_players(List of replies from mentioned players"""import time+tasks = []reply_channels = []all_player_channels = []@@ -925,7 +999,7 @@ async def broadcast_to_players(if state.has_human_player and state.human_player_channel in mentioned_players:human_mentioned = Truementioned_players = [ch for ch in mentioned_players if ch != state.human_player_channel]- logger.info(f"Human player mentioned, will wait for user input")+ logger.info("Human player mentioned, will wait for user input")for ch in state.channels:if ch == "god":@@ -954,7 +1028,9 @@ async def broadcast_to_players(if is_mentioned:reply_channels.append(ch)- logger.info(f"Broadcasting to {len(all_player_channels)} players, {len(reply_channels)} need reply: {reply_channels}")+ logger.info(+ f"Broadcasting to {len(all_player_channels)} players, {len(reply_channels)} need reply: {reply_channels}"+ )# Send all messages concurrently (don't record internal broadcasts)results = await asyncio.gather(*tasks, return_exceptions=True)@@ -973,30 +1049,31 @@ async def broadcast_to_players(# If this player was mentioned and replied successfully, record the replyif is_mentioned and not isinstance(result, Exception):response_content = result.get("message", "") or ""- state.messages.append(ChatMessage(- channel_id=ch,- content=response_content,- is_user=False,- timestamp=time.time(),- ))+ state.messages.append(+ ChatMessage(+ channel_id=ch,+ content=response_content,+ is_user=False,+ timestamp=time.time(),+ )+ )logger.info(f"Received reply from {ch}: {response_content[:100]}...")- replies.append({- "channel_id": ch,- "response": result- })+ replies.append({"channel_id": ch, "response": result})# Handle human playerif human_mentioned:- logger.info(f"Waiting for human player input...")+ logger.info("Waiting for human player input...")state.waiting_for_human = Truehuman_message = f"{sender_prefix}{message}"- state.human_messages.append(ChatMessage(- channel_id="god",- content=human_message,- is_user=False,- timestamp=time.time(),- ))+ state.human_messages.append(+ ChatMessage(+ channel_id="god",+ content=human_message,+ is_user=False,+ timestamp=time.time(),+ )+ )# Wait for human player to respondwhile state.waiting_for_human and state.running:@@ -1005,10 +1082,9 @@ async def broadcast_to_players(if state.human_player_message:reply_content = state.human_player_messagestate.human_player_message = None- replies.append({- "channel_id": state.human_player_channel,- "response": {"message": reply_content}- })+ replies.append(+ {"channel_id": state.human_player_channel, "response": {"message": reply_content}}+ )logger.info(f"Received human player reply: {reply_content[:100]}...")# Save after receiving player replies@@ -1088,7 +1164,7 @@ def build_message_for_god(player_replies: List[Dict[str, Any]], all_channels: Liparts = []for reply in player_replies:content = reply.get("response", {}).get("message", "")- channel_id = reply['channel_id']+ channel_id = reply["channel_id"]if channel_id == "god":parts.append(f"god:{content}")else:@@ -1139,18 +1215,22 @@ async def message_router_loop(# 0. Record admin message only for the first round (admin -> god)if loop_count == 1:msg_timestamp = time.time()- state.messages.append(ChatMessage(- channel_id="admin",- content=current_message,- is_user=True,- timestamp=msg_timestamp,- ))+ state.messages.append(+ ChatMessage(+ channel_id="admin",+ content=current_message,+ is_user=True,+ timestamp=msg_timestamp,+ )+ )# Save immediately so UI can show itif state.session_id:save_conversation_to_file(state.storage_path, state.session_id, state.messages)# 1. Send message to current channel (usually god)- logger.info(f"Sending to {current_channel} (from {current_sender_id}): {current_message[:100]}...")+ logger.info(+ f"Sending to {current_channel} (from {current_sender_id}): {current_message[:100]}..."+ )try:response = await send_to_channel(vikingbot_url=state.vikingbot_url,@@ -1170,12 +1250,14 @@ async def message_router_loop(logger.info(f"Received from {current_channel}: {response_content[:100]}...")# 3. Record agent's reply (always record agent responses)- state.messages.append(ChatMessage(- channel_id=current_channel,- content=response_content,- is_user=False,- timestamp=time.time(),- ))+ state.messages.append(+ ChatMessage(+ channel_id=current_channel,+ content=response_content,+ is_user=False,+ timestamp=time.time(),+ )+ )# 4. Parse mentions and pure content early (needed for game end broadcasting)mentions = parse_mentions(response_content)@@ -1184,7 +1266,9 @@ async def message_router_loop(pure_content = current_message# Check if game has ended by parsing GAME_RECORD.md- god_record_path = state.storage_path / "bot" / "workspace" / "bot_api__god" / "GAME_RECORD.md"+ god_record_path = (+ state.storage_path / "bot" / "workspace" / "bot_api__god" / "GAME_RECORD.md"+ )has_game_ended, game_result = is_game_ended_from_record(god_record_path)valid_mentions = [m for m in mentions if m in state.channels and m != "god"]if valid_mentions:@@ -1192,9 +1276,16 @@ async def message_router_loop(# Only treat the game as fully ended after god has produced the final conclusion# and no longer asks any player for follow-up actions.- if current_channel == "god" and has_game_ended and not state.remember_sent and not valid_mentions:+ if (+ current_channel == "god"+ and has_game_ended+ and not state.remember_sent+ and not valid_mentions+ ):state.game_ended = True- logger.info(f"Game ended detected from GAME_RECORD.md after final god reply, result: {game_result}")+ logger.info(+ f"Game ended detected from GAME_RECORD.md after final god reply, result: {game_result}"+ )if pure_content:await broadcast_message_to_players(@@ -1207,7 +1298,9 @@ async def message_router_loop(if state.session_id:save_conversation_to_file(state.storage_path, state.session_id, state.messages)- archive_replay_state(state.storage_path, state.channels, state.session_id, force=True)+ archive_replay_state(+ state.storage_path, state.channels, state.session_id, force=True+ )leaderboard_result = save_game_to_leaderboard_from_record(storage_path=state.storage_path,@@ -1216,11 +1309,15 @@ async def message_router_loop()if leaderboard_result.get("success"):if leaderboard_result.get("skipped"):- logger.info(f"Leaderboard save skipped: {leaderboard_result.get('reason', 'unknown')}")+ logger.info(+ f"Leaderboard save skipped: {leaderboard_result.get('reason', 'unknown')}"+ )else:logger.info("Leaderboard saved from authoritative GAME_RECORD.md")else:- logger.warning(f"Failed to save leaderboard from GAME_RECORD.md: {leaderboard_result.get('error', 'unknown')}")+ logger.warning(+ f"Failed to save leaderboard from GAME_RECORD.md: {leaderboard_result.get('error', 'unknown')}"+ )try:await send_to_channel(@@ -1249,7 +1346,9 @@ async def message_router_loop(state.remember_sent = Truestate.completed_games += 1- logger.info(f"/remember sent to all, game complete! completed_games={state.completed_games}")+ logger.info(+ f"/remember sent to all, game complete! completed_games={state.completed_games}"+ )except Exception as e:logger.exception(f"Error sending /remember: {e}")@@ -1271,9 +1370,16 @@ async def message_router_loop(save_conversation_to_file(state.storage_path, state.session_id, state.messages)waiting_like_reply = is_waiting_like_reply(response_content)- if not valid_mentions and current_channel == "god" and not has_game_ended and not waiting_like_reply:+ if (+ not valid_mentions+ and current_channel == "god"+ and not has_game_ended+ and not waiting_like_reply+ ):if state.god_no_mention_retry_count >= 2:- logger.warning("God reply still has no valid player mention after fallback retries; stopping router")+ logger.warning(+ "God reply still has no valid player mention after fallback retries; stopping router"+ )breakstate.god_no_mention_retry_count += 1@@ -1298,7 +1404,9 @@ async def message_router_loop(# Validate mentioned channels existif not valid_mentions:- logger.warning(f"No valid player mentions in: {mentions}, available: {state.channels}")+ logger.warning(+ f"No valid player mentions in: {mentions}, available: {state.channels}"+ )breaklogger.info(f"Mentioned players: {valid_mentions}")@@ -1318,7 +1426,7 @@ async def message_router_loop(# 7. First, broadcast player replies to all other players (need_reply=False)if player_replies:for reply in player_replies:- reply_channel = reply['channel_id']+ reply_channel = reply["channel_id"]reply_content = reply.get("response", {}).get("message", "")if reply_content and reply_channel != state.human_player_channel:await broadcast_message_to_players(@@ -1337,7 +1445,7 @@ async def message_router_loop(current_message = god_message# Use specific player_id as sender_id (use first player if multiple replies)if player_replies:- current_sender_id = player_replies[0]['channel_id']+ current_sender_id = player_replies[0]["channel_id"]else:current_sender_id = "players"@@ -1405,8 +1513,12 @@ def create_fastapi_app(state: GameState) -> FastAPI:@fastapi_app.get("/fonts/{filename}")async def serve_font(filename: str):"""Serve font files."""- font_path = Path(__file__).parent / filename- if font_path.exists():+ font_root = Path(__file__).parent.resolve()+ try:+ font_path = (font_root / filename).resolve()+ except OSError:+ return HTMLResponse(content="", status_code=404)+ if font_path.is_relative_to(font_root) and font_path.is_file():return FileResponse(font_path)return HTMLResponse(content="", status_code=404)@@ -1417,27 +1529,29 @@ def create_fastapi_app(state: GameState) -> FastAPI:# The router loop owns the end-of-game transition so god's final reply# can still be received, broadcast to players, and followed by /remember.- return JSONResponse(content={- "game_id": state.game_id,- "session_id": state.session_id,- "running": state.running,- "channels": state.channels,- "message_count": len(state.messages),- "smart_buttons": state.config.get("smart_buttons", False),- "game_ended": state.game_ended,- "force_restarted": state.force_restarted,- "game_mode": state.game_mode,- "has_human_player": state.has_human_player,- "human_player_channel": state.human_player_channel,- "waiting_for_human": state.waiting_for_human,- "auto_run_enabled": state.auto_run_enabled,- "auto_run_mode": state.auto_run_mode,- "auto_run_target_games": state.auto_run_target_games,- "auto_run_remaining_games": get_remaining_auto_run_games(state),- "auto_restart_scheduled": state.auto_restart_scheduled,- "starting_next_game": state.starting_next_game,- "completed_games": state.completed_games,- })+ return JSONResponse(+ content={+ "game_id": state.game_id,+ "session_id": state.session_id,+ "running": state.running,+ "channels": state.channels,+ "message_count": len(state.messages),+ "smart_buttons": state.config.get("smart_buttons", False),+ "game_ended": state.game_ended,+ "force_restarted": state.force_restarted,+ "game_mode": state.game_mode,+ "has_human_player": state.has_human_player,+ "human_player_channel": state.human_player_channel,+ "waiting_for_human": state.waiting_for_human,+ "auto_run_enabled": state.auto_run_enabled,+ "auto_run_mode": state.auto_run_mode,+ "auto_run_target_games": state.auto_run_target_games,+ "auto_run_remaining_games": get_remaining_auto_run_games(state),+ "auto_restart_scheduled": state.auto_restart_scheduled,+ "starting_next_game": state.starting_next_game,+ "completed_games": state.completed_games,+ }+ )@fastapi_app.get("/api/human/messages")async def get_human_messages():@@ -1445,23 +1559,27 @@ def create_fastapi_app(state: GameState) -> FastAPI:if not state.has_human_player:return JSONResponse(content={"messages": []})- return JSONResponse(content={- "messages": [- {- "channel_id": msg.channel_id,- "content": msg.content,- "is_user": msg.is_user,- "timestamp": msg.timestamp,- }- for msg in state.human_messages- ]- })+ return JSONResponse(+ content={+ "messages": [+ {+ "channel_id": msg.channel_id,+ "content": msg.content,+ "is_user": msg.is_user,+ "timestamp": msg.timestamp,+ }+ for msg in state.human_messages+ ]+ }+ )@fastapi_app.post("/api/human/send")async def send_human_message(payload: dict):"""Send a message from human player."""if not state.has_human_player:- return JSONResponse(content={"success": False, "error": "Human player mode not enabled"})+ return JSONResponse(+ content={"success": False, "error": "Human player mode not enabled"}+ )if not state.waiting_for_human:return JSONResponse(content={"success": False, "error": "Not waiting for human input"})@@ -1474,21 +1592,26 @@ def create_fastapi_app(state: GameState) -> FastAPI:return JSONResponse(content={"success": False, "error": "Invalid target"})import time- timestamp = time.time()- state.human_messages.append(ChatMessage(- channel_id=state.human_player_channel,- content=message,- is_user=True,- timestamp=timestamp,- ))- if target == "all":- state.messages.append(ChatMessage(+ timestamp = time.time()+ state.human_messages.append(+ ChatMessage(channel_id=state.human_player_channel,content=message,- is_user=False,+ is_user=True,timestamp=timestamp,- ))+ )+ )++ if target == "all":+ state.messages.append(+ ChatMessage(+ channel_id=state.human_player_channel,+ content=message,+ is_user=False,+ timestamp=timestamp,+ )+ )await broadcast_message_to_players(state,message,@@ -1512,7 +1635,9 @@ def create_fastapi_app(state: GameState) -> FastAPI:human_game_md = state.storage_path / "bot" / "workspace" / "human" / "GAME.md"if not human_game_md.exists():- return JSONResponse(content={"content": "# 真实玩家游戏文件\n\n请编辑此文件来设置你的角色和状态。\n"})+ return JSONResponse(+ content={"content": "# 真实玩家游戏文件\n\n请编辑此文件来设置你的角色和状态。\n"}+ )return JSONResponse(content={"content": human_game_md.read_text(encoding="utf-8")})@@ -1532,22 +1657,26 @@ def create_fastapi_app(state: GameState) -> FastAPI:@fastapi_app.get("/api/messages")async def get_messages():"""Get full message history."""- return JSONResponse(content={- "messages": [- {- "channel_id": msg.channel_id,- "content": msg.content,- "is_user": msg.is_user,- "timestamp": msg.timestamp- }- for msg in state.messages- ]- })+ return JSONResponse(+ content={+ "messages": [+ {+ "channel_id": msg.channel_id,+ "content": msg.content,+ "is_user": msg.is_user,+ "timestamp": msg.timestamp,+ }+ for msg in state.messages+ ]+ }+ )@fastapi_app.get("/api/players")async def get_players():"""Get player info, reading roles from GAME.md."""- players = get_players_snapshot(state.storage_path, state.channels, state.human_player_channel)+ players = get_players_snapshot(+ state.storage_path, state.channels, state.human_player_channel+ )return JSONResponse(content={"players": players})@fastapi_app.post("/api/start")@@ -1556,7 +1685,9 @@ def create_fastapi_app(state: GameState) -> FastAPI:if state.running or (state.router_task and not state.router_task.done()):return JSONResponse(content={"success": False, "error": "Game already running"})- requested_game_mode = str((payload or {}).get("game_mode") or state.game_mode or "all_agents").strip()+ requested_game_mode = str(+ (payload or {}).get("game_mode") or state.game_mode or "all_agents"+ ).strip()if requested_game_mode not in {"all_agents", "human_player"}:return JSONResponse(content={"success": False, "error": "Invalid game_mode"})@@ -1568,12 +1699,16 @@ def create_fastapi_app(state: GameState) -> FastAPI:logger.warning("Failed to start game")return _failed_operation_response("Failed to start game")- return JSONResponse(content={"success": True, "session_id": state.session_id, "game_mode": state.game_mode})+ return JSONResponse(+ content={"success": True, "session_id": state.session_id, "game_mode": state.game_mode}+ )@fastapi_app.post("/api/restart")async def restart_game(payload: Optional[Dict[str, Any]] = None):"""Restart the game."""- requested_game_mode = str((payload or {}).get("game_mode") or state.game_mode or "all_agents").strip()+ requested_game_mode = str(+ (payload or {}).get("game_mode") or state.game_mode or "all_agents"+ ).strip()if requested_game_mode not in {"all_agents", "human_player"}:return JSONResponse(content={"success": False, "error": "Invalid game_mode"})@@ -1585,7 +1720,9 @@ def create_fastapi_app(state: GameState) -> FastAPI:logger.warning("Failed to restart game")return _failed_operation_response("Failed to restart game")- return JSONResponse(content={"success": True, "session_id": session_id, "game_mode": state.game_mode})+ return JSONResponse(+ content={"success": True, "session_id": session_id, "game_mode": state.game_mode}+ )@fastapi_app.post("/api/continue")async def continue_game():@@ -1621,31 +1758,39 @@ def create_fastapi_app(state: GameState) -> FastAPI:enabled = bool(payload.get("enabled", False))if not enabled:disable_auto_run(state)- return JSONResponse(content={- "success": True,- "auto_run_enabled": state.auto_run_enabled,- "auto_run_mode": state.auto_run_mode,- "auto_run_target_games": state.auto_run_target_games,- "auto_run_remaining_games": get_remaining_auto_run_games(state),- "auto_restart_scheduled": state.auto_restart_scheduled,- "starting_next_game": state.starting_next_game,- "completed_games": state.completed_games,- })+ return JSONResponse(+ content={+ "success": True,+ "auto_run_enabled": state.auto_run_enabled,+ "auto_run_mode": state.auto_run_mode,+ "auto_run_target_games": state.auto_run_target_games,+ "auto_run_remaining_games": get_remaining_auto_run_games(state),+ "auto_restart_scheduled": state.auto_restart_scheduled,+ "starting_next_game": state.starting_next_game,+ "completed_games": state.completed_games,+ }+ )mode = str(payload.get("mode") or "fixed").strip().lower()target_games_raw = payload.get("target_games")if mode not in {"fixed", "infinite"}:- return JSONResponse(content={"success": False, "error": "auto-run mode must be 'fixed' or 'infinite'"})+ return JSONResponse(+ content={"success": False, "error": "auto-run mode must be 'fixed' or 'infinite'"}+ )target_games = Noneif mode == "fixed":try:target_games = int(target_games_raw)except (TypeError, ValueError):- return JSONResponse(content={"success": False, "error": "target_games must be an integer >= 1"})+ return JSONResponse(+ content={"success": False, "error": "target_games must be an integer >= 1"}+ )if target_games < 1:- return JSONResponse(content={"success": False, "error": "target_games must be an integer >= 1"})+ return JSONResponse(+ content={"success": False, "error": "target_games must be an integer >= 1"}+ )state.auto_run_enabled = Truestate.auto_run_mode = mode@@ -1658,16 +1803,18 @@ def create_fastapi_app(state: GameState) -> FastAPI:if not state.running and (not state.router_task or state.router_task.done()):schedule_next_game(state, delay_seconds=0.1)- return JSONResponse(content={- "success": True,- "auto_run_enabled": state.auto_run_enabled,- "auto_run_mode": state.auto_run_mode,- "auto_run_target_games": state.auto_run_target_games,- "auto_run_remaining_games": get_remaining_auto_run_games(state),- "auto_restart_scheduled": state.auto_restart_scheduled,- "starting_next_game": state.starting_next_game,- "completed_games": state.completed_games,- })+ return JSONResponse(+ content={+ "success": True,+ "auto_run_enabled": state.auto_run_enabled,+ "auto_run_mode": state.auto_run_mode,+ "auto_run_target_games": state.auto_run_target_games,+ "auto_run_remaining_games": get_remaining_auto_run_games(state),+ "auto_restart_scheduled": state.auto_restart_scheduled,+ "starting_next_game": state.starting_next_game,+ "completed_games": state.completed_games,+ }+ )@fastapi_app.get("/api/openviking/tree")async def get_openviking_tree():@@ -1682,7 +1829,7 @@ def create_fastapi_app(state: GameState) -> FastAPI:tree = {"agent": {"path": str(default_path / "agent"), "files": []},- "user": {"path": str(default_path / "user"), "files": []}+ "user": {"path": str(default_path / "user"), "files": []},}# Scan agent directory@@ -1703,8 +1850,14 @@ def create_fastapi_app(state: GameState) -> FastAPI:Path format: "agent/subpath/file.md" or "user/subpath/file.md""""- viking_path = get_viking_path(state.config)- file_path = viking_path / "default" / path+ default_root = (get_viking_path(state.config) / "default").resolve()+ try:+ file_path = (default_root / path).resolve()+ except OSError:+ return JSONResponse(content={"error": "File not found"}, status_code=404)++ if not file_path.is_relative_to(default_root):+ return JSONResponse(content={"error": "File not found"}, status_code=404)if not file_path.exists():return JSONResponse(content={"error": "File not found"}, status_code=404)@@ -1714,11 +1867,7 @@ def create_fastapi_app(state: GameState) -> FastAPI:try:content = file_path.read_text(encoding="utf-8")- return JSONResponse(content={- "path": path,- "content": content,- "name": file_path.name- })+ return JSONResponse(content={"path": path, "content": content, "name": file_path.name})except Exception:logger.exception("Failed to read OpenViking file")return _internal_error_response("Failed to read file")@@ -1732,20 +1881,20 @@ def create_fastapi_app(state: GameState) -> FastAPI:return JSONResponse(content={"files": []})conversation_files = sorted(- bot_workspace.glob("CONVERSATION_*.md"),- key=lambda p: p.stat().st_mtime,- reverse=True+ bot_workspace.glob("CONVERSATION_*.md"), key=lambda p: p.stat().st_mtime, reverse=True)files = []for f in conversation_files:session_id = f.stem.replace("CONVERSATION_", "")- files.append({- "session_id": session_id,- "filename": f.name,- "modified": f.stat().st_mtime,- "size": f.stat().st_size- })+ files.append(+ {+ "session_id": session_id,+ "filename": f.name,+ "modified": f.stat().st_mtime,+ "size": f.stat().st_size,+ }+ )return JSONResponse(content={"files": files, "current_session_id": state.session_id})@@ -1760,11 +1909,9 @@ def create_fastapi_app(state: GameState) -> FastAPI:try:content = file_path.read_text(encoding="utf-8")- return JSONResponse(content={- "session_id": session_id,- "content": content,- "filename": file_path.name- })+ return JSONResponse(+ content={"session_id": session_id, "content": content, "filename": file_path.name}+ )except Exception:logger.exception("Failed to read conversation file")return _internal_error_response("Failed to read conversation")@@ -1792,9 +1939,7 @@ def create_fastapi_app(state: GameState) -> FastAPI:return JSONResponse(content={"files": [], "current_session_id": state.session_id})session_files = sorted(- sessions_path.glob("bot_api__*.jsonl"),- key=lambda p: p.stat().st_mtime,- reverse=True+ sessions_path.glob("bot_api__*.jsonl"), key=lambda p: p.stat().st_mtime, reverse=True)files = []@@ -1808,13 +1953,15 @@ def create_fastapi_app(state: GameState) -> FastAPI:channel_id = "unknown"file_session_id = f.stem- files.append({- "channel_id": channel_id,- "session_id": file_session_id,- "filename": f.name,- "modified": f.stat().st_mtime,- "size": f.stat().st_size- })+ files.append(+ {+ "channel_id": channel_id,+ "session_id": file_session_id,+ "filename": f.name,+ "modified": f.stat().st_mtime,+ "size": f.stat().st_size,+ }+ )return JSONResponse(content={"files": files, "current_session_id": state.session_id})@@ -1836,7 +1983,7 @@ def create_fastapi_app(state: GameState) -> FastAPI:try:data = json.loads(line)if data.get("_type") == "metadata":- formatted_lines.append(f"# 会话元数据\n")+ formatted_lines.append("# 会话元数据\n")formatted_lines.append(f"- 创建时间: {data.get('created_at', '')}")formatted_lines.append(f"- 更新时间: {data.get('updated_at', '')}")formatted_lines.append("")@@ -1860,18 +2007,22 @@ def create_fastapi_app(state: GameState) -> FastAPI:token_usage = data.get("token_usage")if token_usage:formatted_lines.append("")- formatted_lines.append(f"*Token使用: prompt={token_usage.get('prompt_tokens', 0)}, completion={token_usage.get('completion_tokens', 0)}, total={token_usage.get('total_tokens', 0)}*")+ formatted_lines.append(+ f"*Token使用: prompt={token_usage.get('prompt_tokens', 0)}, completion={token_usage.get('completion_tokens', 0)}, total={token_usage.get('total_tokens', 0)}*"+ )formatted_lines.append("")except json.JSONDecodeError:formatted_lines.append(line)content = "\n".join(formatted_lines)- return JSONResponse(content={- "filename": filename,- "content": content,- "raw_content": file_path.read_text(encoding="utf-8")- })+ return JSONResponse(+ content={+ "filename": filename,+ "content": content,+ "raw_content": file_path.read_text(encoding="utf-8"),+ }+ )except Exception:logger.exception("Failed to read bot session file")return _internal_error_response("Failed to read session file")@@ -1891,11 +2042,9 @@ def create_fastapi_app(state: GameState) -> FastAPI:try:content = file_path.read_text(encoding="utf-8")- return JSONResponse(content={- "channel_id": channel_id,- "filename": filename,- "content": content- })+ return JSONResponse(+ content={"channel_id": channel_id, "filename": filename, "content": content}+ )except Exception:logger.exception("Failed to read game file")return _internal_error_response("Failed to read game file")@@ -1922,27 +2071,31 @@ def create_fastapi_app(state: GameState) -> FastAPI:try:for item in sorted(root_path.iterdir()):- if item.name.startswith('.') and item.is_file():+ if item.name.startswith(".") and item.is_file():# Skip hidden files but include .abstract.md and .overview.md- if item.name not in ['.abstract.md', '.overview.md']:+ if item.name not in [".abstract.md", ".overview.md"]:continueitem_rel_path = f"{relative_path}/{item.name}" if relative_path else item.nameif item.is_dir():- result.append({- "name": item.name,- "path": item_rel_path,- "type": "directory",- "children": scan_directory_tree(item, item_rel_path)- })+ result.append(+ {+ "name": item.name,+ "path": item_rel_path,+ "type": "directory",+ "children": scan_directory_tree(item, item_rel_path),+ }+ )else:- result.append({- "name": item.name,- "path": item_rel_path,- "type": "file",- "size": item.stat().st_size- })+ result.append(+ {+ "name": item.name,+ "path": item_rel_path,+ "type": "file",+ "size": item.stat().st_size,+ }+ )except Exception as e:logger.warning(f"Error scanning directory {root_path}: {e}")@@ -1958,7 +2111,17 @@ def create_fastapi_app(state: GameState) -> FastAPI:3. /data/* -> bot/workspace/*4. /data/* -> storage_path/*"""- bot_workspace = state.storage_path / "bot" / "workspace"+ storage_root = state.storage_path.resolve()+ bot_workspace = (storage_root / "bot" / "workspace").resolve()++ def resolve_under(base_dir: Path, relative_path: str) -> Optional[Path]:+ try:+ candidate = (base_dir / relative_path).resolve()+ except OSError:+ return None+ if not candidate.is_relative_to(base_dir):+ return None+ return candidate# Special case: root path - list bot/workspaceif path == "" or path == "/":@@ -1966,47 +2129,57 @@ def create_fastapi_app(state: GameState) -> FastAPI:# Special handling for werewolf/GAME_RECORD.md - map to god's GAME_RECORD.mdif path == "werewolf/GAME_RECORD.md":- god_record_path = bot_workspace / "bot_api__god" / "GAME_RECORD.md"- if god_record_path.exists():+ god_record_path = resolve_under(bot_workspace, "bot_api__god/GAME_RECORD.md")+ if god_record_path and god_record_path.is_file():return FileResponse(god_record_path)# First try: bot/workspace/{path}- workspace_path = bot_workspace / path- if workspace_path.exists():+ workspace_path = resolve_under(bot_workspace, path)+ if workspace_path and workspace_path.exists():if workspace_path.is_file():return FileResponse(workspace_path)elif workspace_path.is_dir():return await list_directory(workspace_path, path)# Second try: storage_path/{path}- file_path = state.storage_path / path- if file_path.exists() and file_path.is_file():+ file_path = resolve_under(storage_root, path)+ if file_path and file_path.exists() and file_path.is_file():return FileResponse(file_path)- dir_path = state.storage_path / path- if dir_path.exists() and dir_path.is_dir():+ dir_path = resolve_under(storage_root, path)+ if dir_path and dir_path.exists() and dir_path.is_dir():return await list_directory(dir_path, path)return HTMLResponse(content="", media_type="text/markdown", status_code=404)async def list_directory(dir_path: Path, url_path: str):"""Generate a simple HTML directory listing."""+ clean_segments = [segment for segment in url_path.split("/") if segment]+ parent_segments = clean_segments[:-1]+ parent_href = "/data/" + "/".join(quote(segment, safe="") for segment in parent_segments)+ if not parent_segments:+ parent_href = "/data/"entries = []for item in sorted(dir_path.iterdir()):- name = item.name+ href_segments = clean_segments + [item.name]+ href = "/data/" + "/".join(quote(segment, safe="") for segment in href_segments)+ display_name = item.nameif item.is_dir():- name += "/"- entries.append(f'<a href="{url_path.rstrip("/")}/{name}">{name}</a><br>')+ href += "/"+ display_name += "/"+ entries.append(+ f'<a href="{html.escape(href, quote=True)}">{html.escape(display_name)}</a><br>'+ )- html = f"""+ html_content = f"""<!DOCTYPE html><html><body>- <h1>/{url_path}</h1>- {'<a href="../">../</a><br>' if url_path else ''}- {''.join(entries)}+ <h1>/{html.escape(url_path)}</h1>+ {f'<a href="{html.escape(parent_href, quote=True)}">../</a><br>' if url_path else ""}+ {"".join(entries)}</body></html>"""- return HTMLResponse(content=html)+ return HTMLResponse(content=html_content)return fastapi_app@@ -2019,19 +2192,18 @@ def create_fastapi_app(state: GameState) -> FastAPI:def main(port: int = typer.Option(1995, "--port", "-p", help="UI port"),vikingbot_url: str = typer.Option(- "http://localhost:18790",- "--vikingbot-url",- help="Vikingbot API URL"+ "http://localhost:18790", "--vikingbot-url", help="Vikingbot API URL"),config_path: str = typer.Option(- "~/.openviking/ov-multi.conf",- "--config",- "-c",- help="Config file path"+ "~/.openviking/ov-multi.conf", "--config", "-c", help="Config file path"),game_id: str = typer.Option("default", "--game-id", help="Game ID"),- smart_buttons: bool = typer.Option(False, "--smart-buttons", "-s", help="Enable smart button visibility control"),- game_mode: str = typer.Option("all_agents", "--game-mode", "-m", help="Game mode: all_agents or human_player"),+ smart_buttons: bool = typer.Option(+ False, "--smart-buttons", "-s", help="Enable smart button visibility control"+ ),+ game_mode: str = typer.Option(+ "all_agents", "--game-mode", "-m", help="Game mode: all_agents or human_player"+ ),):"""Start the werewolf game server."""import uvicorn@@ -2101,7 +2273,9 @@ def main(logger.info(f"UI will be available at http://localhost:{port}")logger.info(f"Storage path: {storage_path}")logger.info(f"Bot workspace: {storage_path / 'bot' / 'workspace'}")- logger.info(f"Checking for GAME_RECORD.md: {storage_path / 'bot' / 'workspace' / 'bot_api__god' / 'GAME_RECORD.md'}")+ logger.info(+ f"Checking for GAME_RECORD.md: {storage_path / 'bot' / 'workspace' / 'bot_api__god' / 'GAME_RECORD.md'}"+ )uvicorn.run(fastapi_app,diff --git a/bot/tests/test_minimax_provider.py b/bot/tests/test_minimax_provider.pyindex 26c1ee10b..be61ec4b2 100644--- a/bot/tests/test_minimax_provider.py+++ b/bot/tests/test_minimax_provider.py@@ -2,9 +2,9 @@# SPDX-License-Identifier: AGPL-3.0"""Tests for MiniMax provider support (MiniMax-M2.7, MiniMax-M2.7-highspeed)."""-import pytest+from urllib.parse import urlparse-from vikingbot.providers.registry import find_by_model, find_by_name, PROVIDERS, ProviderSpec+from vikingbot.providers.registry import ProviderSpec, find_by_model, find_by_nameclass TestMiniMaxRegistry:@@ -50,7 +50,9 @@ class TestMiniMaxRegistry:def test_minimax_api_base_uses_international_domain(self):"""Default API base must point to the international endpoint."""spec = find_by_name("minimax")- assert spec.default_api_base.startswith("https://api.minimax.io"), (+ parsed = urlparse(spec.default_api_base)+ assert parsed.scheme == "https"+ assert parsed.hostname == "api.minimax.io", ("Default base URL must use international domain api.minimax.io, ""not the mainland China domain api.minimaxi.com")diff --git a/bot/tests/test_werewolf_server_security.py b/bot/tests/test_werewolf_server_security.pyindex 4c8948797..025f10b10 100644--- a/bot/tests/test_werewolf_server_security.py+++ b/bot/tests/test_werewolf_server_security.py@@ -49,3 +49,25 @@ def test_conversation_endpoint_hides_internal_exception_details(tmp_path):assert response.status_code == 500assert response.json() == {"error": "Failed to read conversation"}assert "secret stack detail" not in response.text+++def test_openviking_file_rejects_path_traversal(tmp_path):+ workspace = tmp_path / "level1" / "level2" / "workspace"+ (workspace / "viking" / "default").mkdir(parents=True, exist_ok=True)+ secret_file = workspace.parent / "secret.txt"+ secret_file.write_text("TOPSECRET", encoding="utf-8")++ state = GameState(+ game_id="test-game",+ vikingbot_url="http://127.0.0.1:18790",+ config_path=tmp_path / "ov.conf",+ config={"storage": {"workspace": str(workspace)}},+ storage_path=workspace,+ )+ client = TestClient(create_fastapi_app(state))++ response = client.get("/api/openviking/file", params={"path": "../../../secret.txt"})++ assert response.status_code == 404+ assert response.json() == {"error": "File not found"}+ assert "TOPSECRET" not in response.textdiff --git a/bot/vikingbot/agent/tools/filesystem.py b/bot/vikingbot/agent/tools/filesystem.pyindex 2683eb1de..492b5fabb 100644--- a/bot/vikingbot/agent/tools/filesystem.py+++ b/bot/vikingbot/agent/tools/filesystem.py@@ -3,10 +3,9 @@from typing import TYPE_CHECKING, Anyfrom vikingbot.agent.tools.base import Tool-from vikingbot.config.schema import SessionKey--from vikingbot.sandbox.manager import SandboxManager+if TYPE_CHECKING:+ from vikingbot.agent.tools.base import ToolContextclass ReadFileTool(Tool):@@ -107,7 +106,7 @@ class EditFileTool(Tool):content = await sandbox.read_file(path)if old_text not in content:- return f"Error: old_text not found in file. Make sure it matches exactly."+ return "Error: old_text not found in file. Make sure it matches exactly."count = content.count(old_text)if count > 1:diff --git a/bot/vikingbot/agent/tools/shell.py b/bot/vikingbot/agent/tools/shell.pyindex d94c9e70f..d2b513382 100644--- a/bot/vikingbot/agent/tools/shell.py+++ b/bot/vikingbot/agent/tools/shell.py@@ -1,18 +1,11 @@"""Shell execution tool."""-import asyncio-import os-import re-from pathlib import Pathfrom typing import TYPE_CHECKING, Any-from loguru import logger-from vikingbot.agent.tools.base import Tool-from vikingbot.config.schema import SessionKey--from vikingbot.sandbox.manager import SandboxManager+if TYPE_CHECKING:+ from vikingbot.agent.tools.base import ToolContextclass ExecTool(Tool):@@ -50,7 +43,6 @@ class ExecTool(Tool):working_dir: str | None = None,**kwargs: Any,) -> str:-# Always use sandbox manager (includes direct mode)try:sandbox = await tool_context.sandbox_manager.get_sandbox(tool_context.session_key)diff --git a/bot/vikingbot/agent/tools/web.py b/bot/vikingbot/agent/tools/web.pyindex 7582c39ed..3b242e109 100644--- a/bot/vikingbot/agent/tools/web.py+++ b/bot/vikingbot/agent/tools/web.py@@ -3,24 +3,52 @@import htmlimport jsonimport re-from typing import Any+from html.parser import HTMLParser+from typing import TYPE_CHECKING, Anyfrom urllib.parse import urlparseimport httpxfrom vikingbot.agent.tools.base import Tool+if TYPE_CHECKING:+ from vikingbot.agent.tools.base import ToolContext+# Shared constantsUSER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 14_7_2) AppleWebKit/537.36"MAX_REDIRECTS = 5 # Limit redirects to prevent DoS attacks+class _HTMLTextExtractor(HTMLParser):+ """Extract text content while ignoring script/style blocks."""++ def __init__(self):+ super().__init__(convert_charrefs=True)+ self._parts: list[str] = []+ self._ignored_tag_stack = 0++ def handle_starttag(self, tag: str, attrs: list[tuple[str, str | None]]):+ if tag.lower() in {"script", "style"}:+ self._ignored_tag_stack += 1++ def handle_endtag(self, tag: str):+ if tag.lower() in {"script", "style"} and self._ignored_tag_stack:+ self._ignored_tag_stack -= 1++ def handle_data(self, data: str):+ if not self._ignored_tag_stack:+ self._parts.append(data)++ def get_text(self) -> str:+ return "".join(self._parts)++def _strip_tags(text: str) -> str:"""Remove HTML tags and decode entities."""- text = re.sub(r"<script[\s\S]*?</script>", "", text, flags=re.I)- text = re.sub(r"<style[\s\S]*?</style>", "", text, flags=re.I)- text = re.sub(r"<[^>]+>", "", text)- return html.unescape(text).strip()+ parser = _HTMLTextExtractor()+ parser.feed(text)+ parser.close()+ return html.unescape(parser.get_text()).strip()def _normalize(text: str) -> str:diff --git a/bot/vikingbot/console/web_console.py b/bot/vikingbot/console/web_console.pyindex 4617dfaef..f4afc0044 100644--- a/bot/vikingbot/console/web_console.py+++ b/bot/vikingbot/console/web_console.py@@ -1,13 +1,12 @@import jsonimport sys-import osfrom pathlib import Pathfrom typing import Any, Dict, List, Optional, Tupleimport gradio as gr-from vikingbot.config.loader import load_config, save_config, get_config_path-from vikingbot.config.schema import Config, ChannelType, SandboxBackend, SandboxMode+from vikingbot.config.loader import get_config_path, load_config, save_config+from vikingbot.config.schema import Configdef resolve_schema_ref(@@ -215,7 +214,7 @@ def collect_values_from_components(elif isinstance(value, str):try:value = json.loads(value)- except:+ except Exception:value = []result[field_name] = value@@ -338,7 +337,7 @@ def create_sessions_tab():lines.append(f'<div style="color: black;"><b>{role}:</b> {content}</div>')- except:+ except Exception:lines.append(f'<div style="color: black;">{line}</div>')elif session_file_json.exists():with open(session_file_json, "r") as f:@@ -361,6 +360,7 @@ def create_workspace_tab():workspace_path = config.workspace_path# Create workspace directory if it doesn't existworkspace_path.mkdir(parents=True, exist_ok=True)+ workspace_root = workspace_path.resolve()workspace_path_str = str(workspace_path)with gr.Row():@@ -381,14 +381,22 @@ def create_workspace_tab():if not selected_file:return "", "Please select a file to view"- if Path(selected_file).is_file():+ try:+ candidate = Path(selected_file).resolve()+ except OSError:+ return "", "File not found"++ if not candidate.is_relative_to(workspace_root):+ return "", "Access denied"++ if candidate.is_file():try:- with open(selected_file, "r") as f:- return f.read(), f"Loaded {Path(selected_file).name}"- except:+ with open(candidate, "r") as f:+ return f.read(), f"Loaded {candidate.name}"+ except Exception:return "Cannot read file (binary or encoding error)", ""- elif Path(selected_file).is_dir():- return "", f"{Path(selected_file).name} is a directory"+ elif candidate.is_dir():+ return "", f"{candidate.name} is a directory"return "", "File not found"file_explorer.change(diff --git a/bot/vikingbot/hooks/base.py b/bot/vikingbot/hooks/base.pyindex 4d9e8c684..ce48192e2 100644--- a/bot/vikingbot/hooks/base.py+++ b/bot/vikingbot/hooks/base.py@@ -1,18 +1,11 @@from abc import ABC, abstractmethod-from enum import Enumfrom dataclasses import dataclass-from typing import Any, Dict, Optionalfrom datetime import datetime+from typing import Any, Dict, Optionalfrom vikingbot.config.schema import SessionKey-# class HookType(Enum):-# SYNC = "sync"-# ASYNC = "async"-# BLOCKING = "blocking"--@dataclassclass HookContext:event_type: strdiff --git a/bot/workspace/skills/opencode/opencode_utils.py b/bot/workspace/skills/opencode/opencode_utils.pyindex 44f811e74..66e6d162e 100644--- a/bot/workspace/skills/opencode/opencode_utils.py+++ b/bot/workspace/skills/opencode/opencode_utils.py@@ -5,15 +5,14 @@ import jsonimport osimport subprocessimport sys-import tracebackimport time+import tracebackfrom opencode_ai import Opencodedef execute_cmd(cmd):try:- # 同时捕获stdout和stderr,排查输出位置result = subprocess.run(cmd,shell=True,@@ -24,7 +23,6 @@ def execute_cmd(cmd):check=True, # 等价于check_output的行为,命令失败会抛异常)stdout = result.stdout.strip() # 去除空白符(换行/空格)- stderr = result.stderr.strip()return stdoutexcept subprocess.CalledProcessError as e:# 捕获命令执行失败的异常(返回码非0)@@ -57,14 +55,15 @@ def start_opencode():# 先fork一次,再启动进程,确保脱离所有父进程关联cmd = ["opencode", "serve"]# 创建新会话 + 重定向所有输出- proc = subprocess.Popen(- cmd,- preexec_fn=os.setsid, # 关键:创建新的会话ID- stdout=open("opencode.log", "a"),- stderr=subprocess.STDOUT,- stdin=subprocess.DEVNULL,- )- pid = proc.pid+ with open("opencode.log", "a", encoding="utf-8") as log_file:+ proc = subprocess.Popen(+ cmd,+ preexec_fn=os.setsid, # 关键:创建新的会话ID+ stdout=log_file,+ stderr=subprocess.STDOUT,+ stdin=subprocess.DEVNULL,+ )+ pid = proc.pidprint(f"opencode serve已启动,PID: {pid}")# 短暂等待确保进程启动成功@@ -80,7 +79,7 @@ def check_serve_status():"""测试opencode连接,失败则启动服务并重试"""try:client = Opencode(base_url="http://127.0.0.1:4096")- modes = client.app.modes()+ client.app.modes()except Exception as e:print(f"连接opencode失败,错误: {e}")# 启动服务diff --git a/examples/cloud/alice.py b/examples/cloud/alice.pyindex ba0866451..74bc97f65 100644--- a/examples/cloud/alice.py+++ b/examples/cloud/alice.py@@ -57,7 +57,7 @@ def main():print(f"Server: {url}")print("User: alice")- print(f"Key: {api_key[:16]}...")+ print("Key: [hidden]")client = ov.SyncHTTPClient(url=url, api_key=api_key, agent_id="alice-agent")client.initialize()diff --git a/examples/cloud/bob.py b/examples/cloud/bob.pyindex d73418d67..3f65a24d5 100644--- a/examples/cloud/bob.py+++ b/examples/cloud/bob.py@@ -56,7 +56,7 @@ def main():print(f"Server: {url}")print("User: bob")- print(f"Key: {api_key[:16]}...")+ print("Key: [hidden]")client = ov.SyncHTTPClient(url=url, api_key=api_key, agent_id="bob-agent")client.initialize()diff --git a/openviking/__init__.py b/openviking/__init__.pyindex e7691c5ff..7805ea0a8 100644--- a/openviking/__init__.py+++ b/openviking/__init__.py@@ -6,6 +6,8 @@ OpenViking - An Agent-native context databaseData in, Context out."""+from typing import TYPE_CHECKING+try:from ._version import version as __version__except ImportError:@@ -24,6 +26,16 @@ except ImportError as exc:"Reinstall openviking or run 'pip install -e .' from the project root.") from exc+if TYPE_CHECKING:+ from openviking.async_client import AsyncOpenViking+ from openviking.session import Session+ from openviking.sync_client import SyncOpenViking+ from openviking_cli.client.http import AsyncHTTPClient+ from openviking_cli.client.sync_http import SyncHTTPClient+ from openviking_cli.session.user_id import UserIdentifier++ OpenViking = SyncOpenViking+def __getattr__(name: str):if name == "AsyncOpenViking":diff --git a/openviking/core/__init__.py b/openviking/core/__init__.pyindex 1baea54b3..2d9398f68 100644--- a/openviking/core/__init__.py+++ b/openviking/core/__init__.py@@ -3,7 +3,17 @@"""Core context abstractions for OpenViking."""from importlib import import_module-from typing import Any+from typing import TYPE_CHECKING, Any++if TYPE_CHECKING:+ from openviking.core.building_tree import BuildingTree+ from openviking.core.context import Context, ContextType, ResourceContentType+ from openviking.core.directories import (+ PRESET_DIRECTORIES,+ DirectoryDefinition,+ DirectoryInitializer,+ )+ from openviking.core.skill_loader import SkillLoader_EXPORTS = {"BuildingTree": ("openviking.core.building_tree", "BuildingTree"),diff --git a/openviking/crypto/encryptor.py b/openviking/crypto/encryptor.pyindex 9af1b95e7..350193f8f 100644--- a/openviking/crypto/encryptor.py+++ b/openviking/crypto/encryptor.py@@ -9,10 +9,11 @@ Implements Envelope Encryption pattern:- Account Key is derived from Root Key"""+import importlibimport secretsimport structimport time-from typing import Tuple+from typing import TYPE_CHECKING, Any, Tuplefrom openviking.crypto.exceptions import (AuthenticationFailedError,@@ -20,14 +21,11 @@ from openviking.crypto.exceptions import (InvalidMagicError,KeyMismatchError,)-from openviking.crypto.providers import (- PROVIDER_LOCAL,- PROVIDER_VAULT,- PROVIDER_VOLCENGINE,- RootKeyProvider,-)from openviking_cli.utils.logger import get_logger+if TYPE_CHECKING:+ from openviking.crypto.providers import RootKeyProvider+logger = get_logger(__name__)# Magic number: OpenViking Encryption v1@@ -37,11 +35,29 @@ MAGIC_LENGTH = len(MAGIC)# Envelope format versionVERSION = 0x01+PROVIDER_LOCAL = 0x01+PROVIDER_VAULT = 0x02+PROVIDER_VOLCENGINE = 0x03+++def _record_encryption_metrics(+ *metrics: Tuple[str, dict[str, Any]],+ debug_message: str,+) -> None:+ """Emit encryption metrics without letting observability failures affect crypto flows."""+ try:+ encryption_module = importlib.import_module("openviking.metrics.datasources.encryption")+ datasource = encryption_module.EncryptionEventDataSource+ for metric_name, metric_kwargs in metrics:+ getattr(datasource, metric_name)(**metric_kwargs)+ except Exception:+ logger.debug(debug_message, exc_info=True)+class FileEncryptor:"""File encryptor."""- def __init__(self, provider: RootKeyProvider):+ def __init__(self, provider: "RootKeyProvider"):"""Initialize FileEncryptor.@@ -51,7 +67,7 @@ class FileEncryptor:self.provider = providerself._provider_type = self._detect_provider_type(provider)- def _detect_provider_type(self, provider: RootKeyProvider) -> int:+ def _detect_provider_type(self, provider: "RootKeyProvider") -> int:"""Detect Provider type."""from openviking.crypto.providers import (LocalFileProvider,@@ -82,15 +98,11 @@ class FileEncryptor:start = time.perf_counter()# Metrics must be best-effort: encryption must succeed/fail solely based on crypto logic,# not on observability availability. Therefore all metrics emissions are wrapped in try/except.- try:- from openviking.metrics.datasources.encryption import EncryptionEventDataSource-- EncryptionEventDataSource.record_payload_size(- operation="encrypt", size_bytes=len(plaintext)- )- EncryptionEventDataSource.record_bytes(operation="encrypt", size_bytes=len(plaintext))- except Exception:- pass+ _record_encryption_metrics(+ ("record_payload_size", {"operation": "encrypt", "size_bytes": len(plaintext)}),+ ("record_bytes", {"operation": "encrypt", "size_bytes": len(plaintext)}),+ debug_message="Failed to record encryption pre-encrypt metrics",+ )status = "ok"try:@@ -110,14 +122,17 @@ class FileEncryptor:raisefinally:elapsed = time.perf_counter() - start- try:- from openviking.metrics.datasources.encryption import EncryptionEventDataSource-- EncryptionEventDataSource.record_operation(- operation="encrypt", status=status, duration_seconds=elapsed- )- except Exception:- pass+ _record_encryption_metrics(+ (+ "record_operation",+ {+ "operation": "encrypt",+ "status": status,+ "duration_seconds": elapsed,+ },+ ),+ debug_message="Failed to record encryption operation metrics",+ )async def decrypt(self, account_id: str, ciphertext: bytes) -> bytes:"""@@ -162,40 +177,35 @@ class FileEncryptor:try:plaintext = await self._aes_gcm_decrypt(file_key, data_iv, encrypted_content)- try:- from openviking.metrics.datasources.encryption import EncryptionEventDataSource-- EncryptionEventDataSource.record_payload_size(- operation="decrypt", size_bytes=len(ciphertext)- )- EncryptionEventDataSource.record_bytes(- operation="decrypt", size_bytes=len(ciphertext)- )- except Exception:- pass+ _record_encryption_metrics(+ ("record_payload_size", {"operation": "decrypt", "size_bytes": len(ciphertext)}),+ ("record_bytes", {"operation": "decrypt", "size_bytes": len(ciphertext)}),+ debug_message="Failed to record encryption pre-decrypt metrics",+ )return plaintextexcept AuthenticationFailedError as e:status = "error"- try:- from openviking.metrics.datasources.encryption import EncryptionEventDataSource-- EncryptionEventDataSource.record_auth_failed()- except Exception:- pass+ _record_encryption_metrics(+ ("record_auth_failed", {}),+ debug_message="Failed to record encryption authentication failure metrics",+ )raise AuthenticationFailedError(f"Authentication failed: {e}")except Exception as e:status = "error"raise AuthenticationFailedError(f"Authentication failed: {e}")finally:elapsed = time.perf_counter() - start- try:- from openviking.metrics.datasources.encryption import EncryptionEventDataSource-- EncryptionEventDataSource.record_operation(- operation="decrypt", status=status, duration_seconds=elapsed- )- except Exception:- pass+ _record_encryption_metrics(+ (+ "record_operation",+ {+ "operation": "decrypt",+ "status": status,+ "duration_seconds": elapsed,+ },+ ),+ debug_message="Failed to record decryption operation metrics",+ )def _build_envelope(self,diff --git a/openviking/crypto/providers.py b/openviking/crypto/providers.pyindex beb39c147..bc3c8df01 100644--- a/openviking/crypto/providers.py+++ b/openviking/crypto/providers.py@@ -11,6 +11,7 @@ Provides multiple key management methods:import abcimport asyncio+import importlibimport osimport secretsimport time@@ -26,6 +27,22 @@ from openviking_cli.utils.logger import get_loggerlogger = get_logger(__name__)++def _record_encryption_metrics(+ *metrics: Tuple[str, Dict[str, Any]],+ debug_message: Optional[str] = None,+) -> None:+ """Emit encryption metrics without creating a hard import edge back into crypto."""+ try:+ encryption_module = importlib.import_module("openviking.metrics.datasources.encryption")+ datasource = encryption_module.EncryptionEventDataSource+ for metric_name, metric_kwargs in metrics:+ getattr(datasource, metric_name)(**metric_kwargs)+ except Exception:+ if debug_message:+ logger.debug(debug_message, exc_info=True)++# HKDF related constantsHKDF_SALT = b"openviking-kek-salt-v1"HKDF_INFO_PREFIX = b"openviking:kek:v1:"@@ -95,17 +112,13 @@ class RootKeyProvider(ABC):raisefinally:elapsed = time.perf_counter() - start- try:- from openviking.metrics.datasources.encryption import EncryptionEventDataSource-- EncryptionEventDataSource.record_key_derivation(- status=status, duration_seconds=elapsed- )- except Exception:- logger.debug(- "Failed to record encryption key derivation metrics",- exc_info=True,- )+ _record_encryption_metrics(+ (+ "record_key_derivation",+ {"status": status, "duration_seconds": elapsed},+ ),+ debug_message="Failed to record encryption key derivation metrics",+ )class BaseProvider(RootKeyProvider):@@ -181,20 +194,10 @@ class LocalFileProvider(BaseProvider):async def get_root_key(self) -> bytes:"""Get Root Key."""if self._root_key is not None:- try:- from openviking.metrics.datasources.encryption import EncryptionEventDataSource-- EncryptionEventDataSource.record_key_cache_hit(provider="local")- except Exception:- pass+ _record_encryption_metrics(("record_key_cache_hit", {"provider": "local"}))return self._root_key- try:- from openviking.metrics.datasources.encryption import EncryptionEventDataSource-- EncryptionEventDataSource.record_key_cache_miss(provider="local")- except Exception:- pass+ _record_encryption_metrics(("record_key_cache_miss", {"provider": "local"}))start = time.perf_counter()status = "ok"@@ -206,19 +209,18 @@ class LocalFileProvider(BaseProvider):raisefinally:elapsed = time.perf_counter() - start- try:- from openviking.metrics.datasources.encryption import EncryptionEventDataSource-- EncryptionEventDataSource.record_key_load(- status=status, provider="local", duration_seconds=elapsed- )- EncryptionEventDataSource.record_key_version_usage(key_version="local")- except Exception:- logger.debug(- "Failed to record encryption key metrics for provider=%s",- "local",- exc_info=True,- )+ _record_encryption_metrics(+ (+ "record_key_load",+ {+ "status": status,+ "provider": "local",+ "duration_seconds": elapsed,+ },+ ),+ ("record_key_version_usage", {"key_version": "local"}),+ debug_message="Failed to record encryption key metrics for provider=local",+ )async def _load_or_create_root_key(self) -> bytes:"""Load or create Root Key."""@@ -481,20 +483,10 @@ class VaultProvider(BaseProvider):Root key"""if self._root_key is not None:- try:- from openviking.metrics.datasources.encryption import EncryptionEventDataSource-- EncryptionEventDataSource.record_key_cache_hit(provider="vault")- except Exception:- pass+ _record_encryption_metrics(("record_key_cache_hit", {"provider": "vault"}))return self._root_key- try:- from openviking.metrics.datasources.encryption import EncryptionEventDataSource-- EncryptionEventDataSource.record_key_cache_miss(provider="vault")- except Exception:- pass+ _record_encryption_metrics(("record_key_cache_miss", {"provider": "vault"}))start = time.perf_counter()status = "ok"@@ -506,21 +498,18 @@ class VaultProvider(BaseProvider):raisefinally:elapsed = time.perf_counter() - start- try:- from openviking.metrics.datasources.encryption import EncryptionEventDataSource-- EncryptionEventDataSource.record_key_load(- status=status, provider="vault", duration_seconds=elapsed- )- EncryptionEventDataSource.record_key_version_usage(- key_version=str(self.root_key_name)- )- except Exception:- logger.debug(- "Failed to record encryption key metrics for provider=%s",- "vault",- exc_info=True,- )+ _record_encryption_metrics(+ (+ "record_key_load",+ {+ "status": status,+ "provider": "vault",+ "duration_seconds": elapsed,+ },+ ),+ ("record_key_version_usage", {"key_version": str(self.root_key_name)}),+ debug_message="Failed to record encryption key metrics for provider=vault",+ )async def derive_account_key(self, account_id: str) -> bytes:"""@@ -748,20 +737,10 @@ class VolcengineKMSProvider(BaseProvider):Root key"""if self._root_key is not None:- try:- from openviking.metrics.datasources.encryption import EncryptionEventDataSource-- EncryptionEventDataSource.record_key_cache_hit(provider="volcengine_kms")- except Exception:- pass+ _record_encryption_metrics(("record_key_cache_hit", {"provider": "volcengine_kms"}))return self._root_key- try:- from openviking.metrics.datasources.encryption import EncryptionEventDataSource-- EncryptionEventDataSource.record_key_cache_miss(provider="volcengine_kms")- except Exception:- pass+ _record_encryption_metrics(("record_key_cache_miss", {"provider": "volcengine_kms"}))start = time.perf_counter()status = "ok"@@ -773,19 +752,18 @@ class VolcengineKMSProvider(BaseProvider):raisefinally:elapsed = time.perf_counter() - start- try:- from openviking.metrics.datasources.encryption import EncryptionEventDataSource-- EncryptionEventDataSource.record_key_load(- status=status, provider="volcengine_kms", duration_seconds=elapsed- )- EncryptionEventDataSource.record_key_version_usage(key_version=str(self.key_id))- except Exception:- logger.debug(- "Failed to record encryption key metrics for provider=%s",- "volcengine_kms",- exc_info=True,- )+ _record_encryption_metrics(+ (+ "record_key_load",+ {+ "status": status,+ "provider": "volcengine_kms",+ "duration_seconds": elapsed,+ },+ ),+ ("record_key_version_usage", {"key_version": str(self.key_id)}),+ debug_message="Failed to record encryption key metrics for provider=volcengine_kms",+ )async def derive_account_key(self, account_id: str) -> bytes:"""diff --git a/openviking/eval/ragas/__init__.py b/openviking/eval/ragas/__init__.pyindex 38e88cc8f..fb08122d1 100644--- a/openviking/eval/ragas/__init__.py+++ b/openviking/eval/ragas/__init__.py@@ -122,7 +122,7 @@ def _create_ragas_llm_from_config() -> Optional[Any]:api_base = env_config["api_base"]model_name = env_config["model"] or "gpt-4o-mini"- logger.info(f"Using RAGAS LLM from environment: model={model_name}, base_url={api_base}")+ logger.info("Using RAGAS LLM from environment variables")openai_model = ChatOpenAI(model=model_name,diff --git a/openviking/models/embedder/openai_embedders.py b/openviking/models/embedder/openai_embedders.pyindex d2d7b46e5..937f2f04f 100644--- a/openviking/models/embedder/openai_embedders.py+++ b/openviking/models/embedder/openai_embedders.py@@ -417,6 +417,10 @@ class OpenAISparseEmbedder(SparseEmbedderBase):"""def __init__(self, *args, **kwargs):+ model_name = kwargs.get("model_name")+ if model_name is None and args:+ model_name = str(args[0])+ super().__init__(model_name=model_name or "openai-sparse-unsupported")raise NotImplementedError("OpenAI does not support sparse embeddings. ""Consider using VolcengineSparseEmbedder or other providers."@@ -433,6 +437,10 @@ class OpenAIHybridEmbedder(HybridEmbedderBase):"""def __init__(self, *args, **kwargs):+ model_name = kwargs.get("model_name")+ if model_name is None and args:+ model_name = str(args[0])+ super().__init__(model_name=model_name or "openai-hybrid-unsupported")raise NotImplementedError("OpenAI does not support hybrid embeddings. ""Consider using VolcengineHybridEmbedder or other providers."diff --git a/openviking/models/vlm/backends/litellm_vlm.py b/openviking/models/vlm/backends/litellm_vlm.pyindex cc30b8a83..f0f9d750c 100644--- a/openviking/models/vlm/backends/litellm_vlm.py+++ b/openviking/models/vlm/backends/litellm_vlm.py@@ -9,6 +9,7 @@ import osimport timefrom pathlib import Pathfrom typing import Any, Dict, List, Optional, Union+from urllib.parse import urlparseos.environ["LITELLM_LOCAL_MODEL_COST_MAP"] = "True"@@ -22,6 +23,17 @@ from ..base import ToolCall, VLMBase, VLMResponselogger = logging.getLogger(__name__)++def _is_google_generate_language_endpoint(api_base: str) -> bool:+ """Check whether the configured API base is Gemini's native endpoint."""+ parsed = urlparse(api_base)+ hostname = (parsed.hostname or "").lower().rstrip(".")+ if hostname != "generativelanguage.googleapis.com":+ return False+ normalized_path = parsed.path.rstrip("/")+ return normalized_path.startswith("/v1") or normalized_path.startswith("/v1beta")++PROVIDER_CONFIGS: Dict[str, Dict[str, Any]] = {"openrouter": {"keywords": ("openrouter",),@@ -214,9 +226,7 @@ class LiteLLMVLMProvider(VLMBase):if self.api_key:kwargs["api_key"] = self.api_keyif self.api_base:- is_google_endpoint = "generativelanguage.googleapis.com" in self.api_base and (- "/v1" in self.api_base or "/v1beta" in self.api_base- )+ is_google_endpoint = _is_google_generate_language_endpoint(self.api_base)if not is_google_endpoint:kwargs["api_base"] = self.api_baseif self._extra_headers:diff --git a/openviking/parse/__init__.py b/openviking/parse/__init__.pyindex e447710c1..380f65288 100644--- a/openviking/parse/__init__.py+++ b/openviking/parse/__init__.py@@ -2,6 +2,7 @@# SPDX-License-Identifier: AGPL-3.0"""Document parsers for various formats."""+from openviking.core.building_tree import BuildingTreefrom openviking.parse.base import NodeType, ParseResult, ResourceNode, create_parse_resultfrom openviking.parse.converter import DocumentConverterfrom openviking.parse.custom import CallbackParserWrapper, CustomParserProtocol, CustomParserWrapperdiff --git a/openviking/parse/parsers/epub.py b/openviking/parse/parsers/epub.pyindex 072a6380c..09fce2848 100644--- a/openviking/parse/parsers/epub.py+++ b/openviking/parse/parsers/epub.py@@ -10,6 +10,7 @@ Inspired by microsoft/markitdown approach.import htmlimport reimport zipfile+from html.parser import HTMLParserfrom pathlib import Pathfrom typing import List, Optional, Union@@ -21,6 +22,69 @@ from openviking_cli.utils.logger import get_loggerlogger = get_logger(__name__)+class _EPubMarkdownParser(HTMLParser):+ """Convert HTML fragments to simple markdown without regex tag stripping."""++ _HEADER_PREFIX = {"h1": "# ", "h2": "## ", "h3": "### ", "h4": "#### "}++ def __init__(self):+ super().__init__(convert_charrefs=True)+ self._parts: list[str] = []+ self._ignored_tag_stack = 0++ def handle_starttag(self, tag: str, attrs):+ normalized_tag = tag.lower()+ if normalized_tag in {"script", "style"}:+ self._ignored_tag_stack += 1+ return+ if self._ignored_tag_stack:+ return+ if normalized_tag in self._HEADER_PREFIX:+ self._ensure_block_break()+ self._parts.append(self._HEADER_PREFIX[normalized_tag])+ elif normalized_tag in {"strong", "b"}:+ self._parts.append("**")+ elif normalized_tag in {"em", "i"}:+ self._parts.append("*")+ elif normalized_tag == "li":+ self._parts.append("\n- ")+ elif normalized_tag == "br":+ self._parts.append("\n")+ elif normalized_tag in {"p", "div", "section", "article"}:+ self._ensure_block_break()++ def handle_endtag(self, tag: str):+ normalized_tag = tag.lower()+ if normalized_tag in {"script", "style"}:+ if self._ignored_tag_stack:+ self._ignored_tag_stack -= 1+ return+ if self._ignored_tag_stack:+ return+ if normalized_tag in self._HEADER_PREFIX or normalized_tag in {+ "p",+ "div",+ "section",+ "article",+ }:+ self._parts.append("\n\n")+ elif normalized_tag in {"strong", "b"}:+ self._parts.append("**")+ elif normalized_tag in {"em", "i"}:+ self._parts.append("*")++ def handle_data(self, data: str):+ if not self._ignored_tag_stack:+ self._parts.append(data)++ def get_markdown(self) -> str:+ return "".join(self._parts)++ def _ensure_block_break(self):+ if self._parts and not self._parts[-1].endswith("\n"):+ self._parts.append("\n\n")++class EPubParser(BaseParser):"""EPub e-book parser for OpenViking.@@ -138,36 +202,13 @@ class EPubParser(BaseParser):def _html_to_markdown(self, html_content: str) -> str:"""Simple HTML to markdown conversion."""- # Remove script and style tags- html_content = re.sub(r"<script[^>]*>.*?</script>", "", html_content, flags=re.DOTALL)- html_content = re.sub(r"<style[^>]*>.*?</style>", "", html_content, flags=re.DOTALL)-- # Convert headers- html_content = re.sub(r"<h1[^>]*>(.*?)</h1>", r"# \1", html_content, flags=re.DOTALL)- html_content = re.sub(r"<h2[^>]*>(.*?)</h2>", r"## \1", html_content, flags=re.DOTALL)- html_content = re.sub(r"<h3[^>]*>(.*?)</h3>", r"### \1", html_content, flags=re.DOTALL)- html_content = re.sub(r"<h4[^>]*>(.*?)</h4>", r"#### \1", html_content, flags=re.DOTALL)-- # Convert bold and italic- html_content = re.sub(r"<strong>(.*?)</strong>", r"**\1**", html_content, flags=re.DOTALL)- html_content = re.sub(r"<b>(.*?)</b>", r"**\1**", html_content, flags=re.DOTALL)- html_content = re.sub(r"<em>(.*?)</em>", r"*\1*", html_content, flags=re.DOTALL)- html_content = re.sub(r"<i>(.*?)</i>", r"*\1*", html_content, flags=re.DOTALL)-- # Convert paragraphs- html_content = re.sub(r"<p[^>]*>(.*?)</p>", r"\1\n\n", html_content, flags=re.DOTALL)-- # Convert line breaks- html_content = re.sub(r"<br\s*/?>", "\n", html_content)-- # Remove remaining HTML tags- html_content = re.sub(r"<[^>]+>", "", html_content)-- # Unescape HTML entities- html_content = html.unescape(html_content)+ parser = _EPubMarkdownParser()+ parser.feed(html_content)+ parser.close()+ markdown = html.unescape(parser.get_markdown())# Normalize whitespace- html_content = re.sub(r"\n\s*\n", "\n\n", html_content)- html_content = re.sub(r"[ \t]+", " ", html_content)+ markdown = re.sub(r"\n\s*\n", "\n\n", markdown)+ markdown = re.sub(r"[ \t]+", " ", markdown)- return html_content.strip()+ return markdown.strip()diff --git a/openviking/parse/parsers/feishu.py b/openviking/parse/parsers/feishu.pyindex f477509b3..ff756c03e 100644--- a/openviking/parse/parsers/feishu.py+++ b/openviking/parse/parsers/feishu.py@@ -33,6 +33,8 @@ from openviking_cli.utils.logger import get_loggerlogger = get_logger(__name__)+_ALLOWED_FEISHU_HOSTS = ("feishu.cn", "larksuite.com")+def _getattr_safe(obj, key: str, default=None):"""Get attribute from SDK object or dict, with safe fallback."""@@ -41,6 +43,16 @@ def _getattr_safe(obj, key: str, default=None):return getattr(obj, key, default)+def _is_allowed_feishu_url(source_path: str) -> bool:+ """Return True for Feishu/Lark URLs on allowed hosts."""+ parsed = urlparse(source_path)+ hostname = (parsed.hostname or "").lower().rstrip(".")+ return any(+ hostname == allowed_host or hostname.endswith(f".{allowed_host}")+ for allowed_host in _ALLOWED_FEISHU_HOSTS+ )++class FeishuParser(BaseParser):"""Parser for Feishu/Lark cloud documents.@@ -235,6 +247,9 @@ class FeishuParser(BaseParser):Returns:(doc_type, token) e.g. ("docx", "doxcnABC123")"""+ if not _is_allowed_feishu_url(url):+ raise ValueError(f"Feishu host not allowed: {url}")+parsed = urlparse(url)path_parts = [p for p in parsed.path.split("/") if p]if len(path_parts) < 2:@@ -298,7 +313,7 @@ class FeishuParser(BaseParser):**kwargs,) -> ParseResult:"""Not typically used for Feishu (URL-based parser)."""- if source_path and ("feishu.cn" in source_path or "larksuite.com" in source_path):+ if source_path and _is_allowed_feishu_url(source_path):return await self.parse(source_path, instruction=instruction, **kwargs)raise NotImplementedError("FeishuParser requires a Feishu URL. Use parse() instead.")diff --git a/openviking/server/__init__.py b/openviking/server/__init__.pyindex 91a428deb..a86bebba2 100644--- a/openviking/server/__init__.py+++ b/openviking/server/__init__.py@@ -2,6 +2,12 @@# SPDX-License-Identifier: AGPL-3.0"""OpenViking HTTP Server module."""+from typing import TYPE_CHECKING++if TYPE_CHECKING:+ from openviking.server.app import create_app+ from openviking.server.bootstrap import main as run_server+def __getattr__(name: str):if name == "create_app":diff --git a/openviking/service/session_service.py b/openviking/service/session_service.pyindex e5772714b..5d53914ec 100644--- a/openviking/service/session_service.py+++ b/openviking/service/session_service.py@@ -50,6 +50,35 @@ class SessionService:if not self._viking_fs:raise NotInitializedError("VikingFS")+ @staticmethod+ def _record_lifecycle_metric(action: str, status: str) -> None:+ """Best-effort session lifecycle metrics should never break the main flow."""+ try:+ from openviking.metrics.datasources.session import SessionLifecycleDataSource++ SessionLifecycleDataSource.record_lifecycle(action=action, status=status)+ except Exception:+ logger.debug(+ "Failed to record session lifecycle metric action=%s status=%s",+ action,+ status,+ exc_info=True,+ )++ @staticmethod+ def _record_archive_metric(status: str) -> None:+ """Best-effort archive metrics should never break the main flow."""+ try:+ from openviking.metrics.datasources.session import SessionLifecycleDataSource++ SessionLifecycleDataSource.record_archive(status=status)+ except Exception:+ logger.debug(+ "Failed to record session archive metric status=%s",+ status,+ exc_info=True,+ )+def session(self, ctx: RequestContext, session_id: Optional[str] = None) -> Session:"""Create a new session or load an existing one.@@ -80,12 +109,7 @@ class SessionService:Raises:AlreadyExistsError: If a session with the given ID already exists"""- try:- from openviking.metrics.datasources.session import SessionLifecycleDataSource-- SessionLifecycleDataSource.record_lifecycle(action="create", status="attempt")- except Exception:- pass+ self._record_lifecycle_metric("create", "attempt")try:if session_id:existing = self.session(ctx, session_id)@@ -93,20 +117,10 @@ class SessionService:raise AlreadyExistsError(f"Session '{session_id}' already exists")session = self.session(ctx, session_id)await session.ensure_exists()- try:- from openviking.metrics.datasources.session import SessionLifecycleDataSource-- SessionLifecycleDataSource.record_lifecycle(action="create", status="ok")- except Exception:- pass+ self._record_lifecycle_metric("create", "ok")return sessionexcept Exception:- try:- from openviking.metrics.datasources.session import SessionLifecycleDataSource-- SessionLifecycleDataSource.record_lifecycle(action="create", status="error")- except Exception:- pass+ self._record_lifecycle_metric("create", "error")raiseasync def get(@@ -127,20 +141,10 @@ class SessionService:raise NotFoundError(session_id, "session")await session.ensure_exists()await session.load()- try:- from openviking.metrics.datasources.session import SessionLifecycleDataSource-- SessionLifecycleDataSource.record_lifecycle(action="get", status="ok")- except Exception:- pass+ self._record_lifecycle_metric("get", "ok")return sessionexcept Exception:- try:- from openviking.metrics.datasources.session import SessionLifecycleDataSource-- SessionLifecycleDataSource.record_lifecycle(action="get", status="error")- except Exception:- pass+ self._record_lifecycle_metric("get", "error")raiseasync def sessions(self, ctx: RequestContext) -> List[Dict[str, Any]]:@@ -168,6 +172,7 @@ class SessionService:)return sessionsexcept Exception:+ logger.debug("Failed to list sessions", exc_info=True)return []async def delete(self, session_id: str, ctx: RequestContext) -> bool:@@ -190,21 +195,11 @@ class SessionService:try:await self._viking_fs.rm(session_uri, recursive=True, ctx=ctx)logger.info(f"Deleted session: {session_id}")- try:- from openviking.metrics.datasources.session import SessionLifecycleDataSource-- SessionLifecycleDataSource.record_lifecycle(action="delete", status="ok")- except Exception:- pass+ self._record_lifecycle_metric("delete", "ok")return Trueexcept Exception as e:logger.error(f"Failed to delete session {session_id}: {e}")- try:- from openviking.metrics.datasources.session import SessionLifecycleDataSource-- SessionLifecycleDataSource.record_lifecycle(action="delete", status="error")- except Exception:- pass+ self._record_lifecycle_metric("delete", "error")raise NotFoundError(session_id, "session")async def commit(self, session_id: str, ctx: RequestContext) -> Dict[str, Any]:@@ -236,17 +231,8 @@ class SessionService:self._ensure_initialized()session = await self.get(session_id, ctx)result = await session.commit_async()- try:- from openviking.metrics.datasources.session import SessionLifecycleDataSource-- SessionLifecycleDataSource.record_lifecycle(- action="commit", status="ok" if result.get("status") else "error"- )- SessionLifecycleDataSource.record_archive(- status="ok" if result.get("archived") else "skip"- )- except Exception:- pass+ self._record_lifecycle_metric("commit", "ok" if result.get("status") else "error")+ self._record_archive_metric("ok" if result.get("archived") else "skip")return resultasync def get_commit_task(self, task_id: str, ctx: RequestContext) -> Optional[Dict[str, Any]]:@@ -278,10 +264,5 @@ class SessionService:session_id=session_id,ctx=ctx,)- try:- from openviking.metrics.datasources.session import SessionLifecycleDataSource-- SessionLifecycleDataSource.record_lifecycle(action="extract", status="ok")- except Exception:- pass+ self._record_lifecycle_metric("extract", "ok")return memoriesdiff --git a/openviking/storage/__init__.py b/openviking/storage/__init__.pyindex 1d1f07140..dfae5ff86 100644--- a/openviking/storage/__init__.py+++ b/openviking/storage/__init__.py@@ -8,6 +8,8 @@ native C++ engine extension is loaded while ``storage/__init__`` isstill executing."""+from typing import TYPE_CHECKING+from openviking.storage.errors import (CollectionNotFoundError,ConnectionError,@@ -17,6 +19,13 @@ from openviking.storage.errors import (StorageException,)+if TYPE_CHECKING:+ from openviking.storage.observers import BaseObserver, QueueObserver+ from openviking.storage.queuefs import QueueManager, get_queue_manager, init_queue_manager+ from openviking.storage.viking_fs import VikingFS, get_viking_fs, init_viking_fs+ from openviking.storage.viking_vector_index_backend import VikingVectorIndexBackend+ from openviking.storage.vikingdb_manager import VikingDBManager, VikingDBManagerProxy+_LAZY_IMPORTS = {"BaseObserver": "openviking.storage.observers","QueueObserver": "openviking.storage.observers",diff --git a/openviking/storage/queuefs/queue_manager.py b/openviking/storage/queuefs/queue_manager.pyindex 76c14c3c2..a9d762804 100644--- a/openviking/storage/queuefs/queue_manager.py+++ b/openviking/storage/queuefs/queue_manager.py@@ -54,8 +54,7 @@ def init_queue_manager(def get_queue_manager() -> "QueueManager":"""Get QueueManager singleton."""if _instance is None:- # If not initialized, try to initialize with default configuration- return init_queue_manager()+ raise RuntimeError("QueueManager is not initialized. Call init_queue_manager() first.")return _instancediff --git a/openviking/storage/transaction/lock_handle.py b/openviking/storage/transaction/lock_handle.pyindex 1b00fc941..2fb405fff 100644--- a/openviking/storage/transaction/lock_handle.py+++ b/openviking/storage/transaction/lock_handle.py@@ -8,6 +8,10 @@ from dataclasses import dataclass, fieldfrom typing import Protocol, runtime_checkable+def _new_lock_id() -> str:+ return str(uuid.uuid4())++@runtime_checkableclass LockOwner(Protocol):"""Minimal interface that PathLock requires from its caller."""@@ -15,8 +19,11 @@ class LockOwner(Protocol):id: strlocks: list[str]- def add_lock(self, path: str) -> None: ...- def remove_lock(self, path: str) -> None: ...+ def add_lock(self, path: str) -> None:+ raise NotImplementedError++ def remove_lock(self, path: str) -> None:+ raise NotImplementedError@dataclass@@ -24,7 +31,7 @@ class LockHandle:"""Identifies a lock holder. PathLock uses ``id`` to generate fencing tokensand ``locks`` to track acquired lock files."""- id: str = field(default_factory=lambda: str(uuid.uuid4()))+ id: str = field(default_factory=_new_lock_id)locks: list[str] = field(default_factory=list)created_at: float = field(default_factory=time.time)last_active_at: float = field(init=False)diff --git a/openviking/storage/vectordb/collection/local_collection.py b/openviking/storage/vectordb/collection/local_collection.pyindex 278e21637..8be9ce1c6 100644--- a/openviking/storage/vectordb/collection/local_collection.py+++ b/openviking/storage/vectordb/collection/local_collection.py@@ -41,6 +41,11 @@ from openviking.storage.vectordb.utils.constants import (from openviking.storage.vectordb.utils.data_processor import DataProcessorfrom openviking.storage.vectordb.utils.dict_utils import ThreadSafeDictManagerfrom openviking.storage.vectordb.utils.id_generator import generate_auto_id+from openviking.storage.vectordb.utils.path_safety import (+ resolve_storage_path,+ safe_join,+ safe_join_name,+)from openviking.storage.vectordb.utils.str_to_uint64 import str_to_uint64from openviking.storage.vectordb.vectorize.base import BaseVectorizerfrom openviking.storage.vectordb.vectorize.vectorizer import VectorizerAdapter@@ -107,18 +112,19 @@ def get_or_create_local_collection()return Collection(collection)else:- os.makedirs(path, exist_ok=True)- meta_path = os.path.join(path, "collection_meta.json")+ collection_dir = str(resolve_storage_path(path))+ os.makedirs(collection_dir, exist_ok=True)+ meta_path = str(safe_join(collection_dir, "collection_meta.json"))meta = create_collection_meta(meta_path, meta_data)vectorizer = (VectorizerFactory.create(meta.vectorize)if meta.vectorize and vectorizer is Noneelse vectorizer)- storage_path = os.path.join(path, STORAGE_DIR_NAME)+ storage_path = str(safe_join(collection_dir, STORAGE_DIR_NAME))store_mgr = create_store_manager("local", storage_path)collection = PersistCollection(- path=path, meta=meta, store=store_mgr, vectorizer=vectorizer, config=config+ path=collection_dir, meta=meta, store=store_mgr, vectorizer=vectorizer, config=config)return Collection(collection)@@ -358,7 +364,9 @@ class LocalCollection(ICollection):return SearchResult()cands = cands_list[0]sparse_vector = (- dict(zip(cands.sparse_raw_terms, cands.sparse_values)) if cands.sparse_raw_terms else {}+ dict(zip(cands.sparse_raw_terms, cands.sparse_values, strict=False))+ if cands.sparse_raw_terms+ else {})return self.search_by_vector(@@ -607,7 +615,9 @@ class LocalCollection(ICollection):if not self.vectorizer_adapter:raw_data[vk] = list(cand_data.vector)if svk and cand_data.sparse_raw_terms and cand_data.sparse_values:- raw_data[svk] = dict(zip(cand_data.sparse_raw_terms, cand_data.sparse_values))+ raw_data[svk] = dict(+ zip(cand_data.sparse_raw_terms, cand_data.sparse_values, strict=False)+ )raw_data = validation.fix_fields_data(raw_data, self.meta.fields_dict)raw_data_list.append(raw_data)@@ -927,28 +937,41 @@ class PersistCollection(LocalCollection):vectorizer: Optional[BaseVectorizer] = None,config: Optional[Dict[str, Any]] = None,):- self.collection_dir = path+ self.collection_dir = str(resolve_storage_path(path))os.makedirs(self.collection_dir, exist_ok=True)- self.index_dir = os.path.join(self.collection_dir, "index")+ self.index_dir = str(safe_join(self.collection_dir, "index"))os.makedirs(self.index_dir, exist_ok=True)super().__init__(meta, store, vectorizer, config)self._recover()LocalCollection._register_scheduler_job(self) # TTL expiration data cleanupdef _recover(self):- index_names = [- folder- for folder in os.listdir(self.index_dir)- if os.path.isdir(os.path.join(self.index_dir, folder))- ]- for index_name in index_names:- meta_path = os.path.join(self.index_dir, index_name, "index_meta.json")- if not os.path.exists(meta_path):+ for folder in os.listdir(self.index_dir):+ try:+ index_dir = safe_join(self.index_dir, folder)+ except ValueError:+ logger.warning(f"Skipping invalid index directory under {self.index_dir}: {folder}")+ continue++ if not index_dir.is_dir():+ continue++ try:+ validation.validate_name_str(folder)+ except ValueError:+ logger.warning(+ f"Skipping index directory with invalid name under {self.index_dir}: {folder}"+ )+ continue++ index_name = folder+ meta_path = safe_join(index_dir, "index_meta.json")+ if not meta_path.exists():logger.warning(f"Index metadata file not found at {meta_path}, skipping recovery for index {index_name}")continue- meta = create_index_meta(self.meta, meta_path)+ meta = create_index_meta(self.meta, str(meta_path))# When recovering an existing index, pass initial_timestamp=0.# This ensures the index's base version starts at 0, allowing it to ingest# all data from the delta log (CandidateData) regardless of when that data was created.@@ -1021,9 +1044,9 @@ class PersistCollection(LocalCollection):cands_list: List[CandidateData],force_rebuild: bool = False,):- new_index_dir = os.path.join(self.index_dir, index_name)+ new_index_dir = str(safe_join_name(self.index_dir, index_name))os.makedirs(new_index_dir, exist_ok=True)- meta_path = os.path.join(new_index_dir, "index_meta.json")+ meta_path = str(safe_join(new_index_dir, "index_meta.json"))meta = create_index_meta(self.meta, meta_path, meta_data)index = PersistentIndex(name=index_name,diff --git a/openviking/storage/vectordb/index/local_index.py b/openviking/storage/vectordb/index/local_index.pyindex 9f0a4f389..a4493db23 100644--- a/openviking/storage/vectordb/index/local_index.py+++ b/openviking/storage/vectordb/index/local_index.py@@ -218,7 +218,6 @@ class LocalIndex(IIndex):)self.meta = metaself.field_type_converter = DataProcessor(self.meta.collection_meta.fields_dict)- passdef update(self,@@ -311,7 +310,7 @@ class LocalIndex(IIndex):return agg_datadef close(self):- pass+ return Nonedef drop(self):if self.engine_proxy:@@ -428,13 +427,7 @@ class VolatileIndex(LocalIndex):index_config_dict["UpdateTimeStamp"] = version_intindex_config_json = json.dumps(index_config_dict)- # Get the vector normalization flag from meta- normalize_vector_flag = meta.inner_meta.get("VectorIndex", {}).get("NormalizeVector", False)-- # Directly initialize engine_proxy without calling parent __init__- self.engine_proxy = IndexEngineProxy(index_config_json, normalize_vector_flag)- self.meta = meta- self.field_type_converter = DataProcessor(self.meta.collection_meta.fields_dict)+ super().__init__(index_config_json, meta)self.engine_proxy.add_data(self._convert_candidate_list_for_index(cands_list))def need_rebuild(self) -> bool:diff --git a/openviking/storage/vectordb/project/local_project.py b/openviking/storage/vectordb/project/local_project.pyindex d2adde60c..e4e0d1a08 100644--- a/openviking/storage/vectordb/project/local_project.py+++ b/openviking/storage/vectordb/project/local_project.py@@ -7,6 +7,12 @@ from typing import Any, Dict, Optionalfrom openviking.storage.vectordb.collection.collection import Collectionfrom openviking.storage.vectordb.collection.local_collection import get_or_create_local_collectionfrom openviking.storage.vectordb.utils.dict_utils import ThreadSafeDictManager+from openviking.storage.vectordb.utils.path_safety import (+ resolve_storage_path,+ safe_join,+ safe_join_name,+)+from openviking.storage.vectordb.utils.validation import validate_name_strfrom openviking_cli.utils.logger import default_logger as logger@@ -25,8 +31,9 @@ def get_or_create_local_project(path: str = ""):return projectelse:# Persistent project - persisted to disk- os.makedirs(path, exist_ok=True)- project = LocalProject(path=path)+ project_path = str(resolve_storage_path(path))+ os.makedirs(project_path, exist_ok=True)+ project = LocalProject(path=project_path)return project@@ -46,7 +53,7 @@ class LocalProject:- If empty: creates volatile project, collections not persisted- If not empty: creates persistent project, auto-loads all existing collections in that directory"""- self.path = path+ self.path = str(resolve_storage_path(path)) if path else ""self.collections = ThreadSafeDictManager[Collection]()# If persistent project, load existing collections@@ -70,15 +77,19 @@ class LocalProject:returnfor entry in entries:- entry_path = os.path.join(self.path, entry)+ try:+ entry_path = safe_join(self.path, entry)+ meta_path = safe_join(entry_path, "collection_meta.json")+ except ValueError:+ logger.warning(f"Skipping invalid collection directory under {self.path}: {entry}")+ continue# Only process directories- if not os.path.isdir(entry_path):+ if not entry_path.is_dir():continue# Check if it's a collection directory (should contain collection_meta.json)- meta_path = os.path.join(entry_path, "collection_meta.json")- if not os.path.exists(meta_path):+ if not meta_path.exists():logger.warning(f"Directory {entry} does not contain collection_meta.json, skipping")continue@@ -88,10 +99,11 @@ class LocalProject:meta_data = json.load(f)collection_name = meta_data.get("CollectionName", entry)+ validate_name_str(collection_name)# Load collectionlogger.info(f"Loading collection: {collection_name} from {entry_path}")- collection = get_or_create_local_collection(path=entry_path)+ collection = get_or_create_local_collection(path=str(entry_path))self.collections.set(collection_name, collection)logger.info(f"Successfully loaded collection: {collection_name}")@@ -168,10 +180,13 @@ class LocalProject:# Decide whether to create volatile or persistent collection based on project pathif self.path:# Persistent collection- collection_path = os.path.join(self.path, collection_name)+ validate_name_str(collection_name)+ collection_path = safe_join_name(self.path, collection_name)os.makedirs(collection_path, exist_ok=True)logger.info(f"Creating persistent collection: {collection_name} at {collection_path}")- collection = get_or_create_local_collection(meta_data=meta_data, path=collection_path)+ collection = get_or_create_local_collection(+ meta_data=meta_data, path=str(collection_path)+ )else:# Volatile collectionlogger.info(f"Creating volatile collection: {collection_name}")diff --git a/openviking/storage/vectordb/project/project_group.py b/openviking/storage/vectordb/project/project_group.pyindex 88a0cdc6c..1576185bc 100644--- a/openviking/storage/vectordb/project/project_group.py+++ b/openviking/storage/vectordb/project/project_group.py@@ -8,6 +8,12 @@ from openviking.storage.vectordb.project.local_project import (get_or_create_local_project,)from openviking.storage.vectordb.utils.dict_utils import ThreadSafeDictManager+from openviking.storage.vectordb.utils.path_safety import (+ resolve_storage_path,+ safe_join,+ safe_join_name,+)+from openviking.storage.vectordb.utils.validation import validate_name_strfrom openviking_cli.utils.logger import default_logger as logger@@ -29,8 +35,9 @@ def get_or_create_project_group(path: str = ""):return groupelse:# Persistent project group - persisted to disk- os.makedirs(path, exist_ok=True)- group = ProjectGroup(path=path)+ group_path = str(resolve_storage_path(path))+ os.makedirs(group_path, exist_ok=True)+ group = ProjectGroup(path=group_path)return group@@ -52,7 +59,7 @@ class ProjectGroup:- If empty: Create volatile project group- If not empty: Create persistent project group, auto-load all existing projects in directory"""- self.path = path+ self.path = str(resolve_storage_path(path)) if path else ""self.projects = ThreadSafeDictManager[LocalProject]()# If persistent project group, load existing projects@@ -70,9 +77,9 @@ class ProjectGroup:if not os.path.exists(self.path):logger.info(f"ProjectGroup path does not exist: {self.path}")# Create default project- default_path = os.path.join(self.path, "default")+ default_path = safe_join(self.path, "default")os.makedirs(default_path, exist_ok=True)- self.projects.set("default", get_or_create_local_project(path=default_path))+ self.projects.set("default", get_or_create_local_project(path=str(default_path)))return# Scan all subdirectories@@ -84,10 +91,14 @@ class ProjectGroup:loaded_count = 0for entry in entries:- entry_path = os.path.join(self.path, entry)+ try:+ entry_path = safe_join(self.path, entry)+ except ValueError:+ logger.warning(f"Skipping invalid project directory under {self.path}: {entry}")+ continue# Only process directories- if not os.path.isdir(entry_path):+ if not entry_path.is_dir():continue# Use directory name as project name@@ -96,7 +107,7 @@ class ProjectGroup:try:# Load projectlogger.info(f"Loading project: {project_name} from {entry_path}")- project = get_or_create_local_project(path=entry_path)+ project = get_or_create_local_project(path=str(entry_path))self.projects.set(project_name, project)loaded_count += 1logger.info(f"Successfully loaded project: {project_name}")@@ -109,9 +120,9 @@ class ProjectGroup:# If no projects loaded, create default projectif loaded_count == 0:logger.info("No projects found, creating default project")- default_path = os.path.join(self.path, "default")+ default_path = safe_join(self.path, "default")os.makedirs(default_path, exist_ok=True)- self.projects.set("default", get_or_create_local_project(path=default_path))+ self.projects.set("default", get_or_create_local_project(path=str(default_path)))def close(self):"""Close project group, release all project resources"""@@ -183,10 +194,11 @@ class ProjectGroup:# Decide whether to create volatile or persistent project based on project group pathif self.path:# Persistent project- project_path = os.path.join(self.path, project_name)+ validate_name_str(project_name)+ project_path = safe_join_name(self.path, project_name)os.makedirs(project_path, exist_ok=True)logger.info(f"Creating persistent project: {project_name} at {project_path}")- project = get_or_create_local_project(path=project_path)+ project = get_or_create_local_project(path=str(project_path))else:# Volatile projectlogger.info(f"Creating volatile project: {project_name}")diff --git a/openviking/storage/vectordb/utils/validation.py b/openviking/storage/vectordb/utils/validation.pyindex 15b7f696f..887d05958 100644--- a/openviking/storage/vectordb/utils/validation.py+++ b/openviking/storage/vectordb/utils/validation.py@@ -134,14 +134,14 @@ class CollectionField(BaseModel):return v@model_validator(mode="after")- def validate_field_logic(cls, m):- if m.FieldType == FieldTypeEnum.VECTOR:- if m.Dim is None:+ def validate_field_logic(self):+ if self.FieldType == FieldTypeEnum.VECTOR:+ if self.Dim is None:raise ValueError("vector field must contain dim")- if m.IsPrimaryKey:- if m.FieldType not in (FieldTypeEnum.INT64, FieldTypeEnum.STRING):- raise ValueError(f"primary key must be int64 or string, got '{m.FieldType}'")- return m+ if self.IsPrimaryKey:+ if self.FieldType not in (FieldTypeEnum.INT64, FieldTypeEnum.STRING):+ raise ValueError(f"primary key must be int64 or string, got '{self.FieldType}'")+ return selfclass CollectionMetaConfig(BaseModel):diff --git a/openviking/telemetry/tracer.py b/openviking/telemetry/tracer.pyindex a189fa50d..8afdcdc8a 100644--- a/openviking/telemetry/tracer.py+++ b/openviking/telemetry/tracer.py@@ -40,6 +40,10 @@ _propagator: Any = None_trace_id_filter_added: bool = False+def _log_trace_internal_failure(message: str) -> None:+ logger.debug(message, exc_info=True)++class TraceIdLoggingFilter(logging.Filter):"""日志过滤器:注入 TraceID"""@@ -64,7 +68,7 @@ def _setup_logging():)_trace_id_filter_added = Trueexcept Exception:- pass+ _log_trace_internal_failure("[TRACER] failed to configure loguru trace_id patcher")# Also setup standard logging filtertry:@@ -73,7 +77,7 @@ def _setup_logging():if not any(isinstance(f, TraceIdLoggingFilter) for f in handler.filters):handler.addFilter(TraceIdLoggingFilter())except Exception:- pass+ _log_trace_internal_failure("[TRACER] failed to attach standard logging trace_id filter")def init_tracer_from_config() -> Any:@@ -223,7 +227,7 @@ def get_trace_id() -> str:trace_id = "{:032x}".format(current_span.context.trace_id)return trace_idexcept Exception:- pass+ _log_trace_internal_failure("[TRACER] failed to resolve current trace id")return ""@@ -435,7 +439,7 @@ class tracer:trace_id = "{:032x}".format(current_span.context.trace_id)return trace_idexcept Exception:- pass+ _log_trace_internal_failure("[TRACER] failed to resolve decorator trace id")return ""@staticmethod@@ -457,7 +461,7 @@ class tracer:return # span 已结束,不设置 attributecurrent_span.set_attribute(key, str(value))except Exception:- pass+ _log_trace_internal_failure(f"[TRACER] failed to set span attribute key={key}")@staticmethoddef info(line: str, console: bool = False) -> None:@@ -472,9 +476,9 @@ class tracer:if hasattr(current_span, "end_time") and current_span.end_time:return # span 已结束,不添加 eventcurrent_span.add_event(line)- except Exception as e:-+ except Exception:import traceback+traceback.print_stack()@staticmethod@@ -506,7 +510,7 @@ class tracer:current_span.set_status(Status(StatusCode.ERROR))current_span.add_event(line)except Exception:- pass+ _log_trace_internal_failure("[TRACER] failed to record span error")class _DummySpanContext:diff --git a/openviking/utils/agfs_utils.py b/openviking/utils/agfs_utils.pyindex a31fe7650..2b70be8c0 100644--- a/openviking/utils/agfs_utils.py+++ b/openviking/utils/agfs_utils.py@@ -139,7 +139,7 @@ def mount_agfs_backend(agfs: Any, agfs_config: Any) -> None:if plugin_name == "localfs" and "local_dir" in plugin_config.get("config", {}):local_dir = plugin_config["config"]["local_dir"]os.makedirs(local_dir, exist_ok=True)- logger.debug(f"[RAGFSUtils] Ensured local directory exists: {local_dir}")+ logger.debug("[RAGFSUtils] Ensured localfs storage directory exists")# Ensure queuefs db_path parent directory exists before mountingif plugin_name == "queuefs" and "db_path" in plugin_config.get("config", {}):db_path = plugin_config["config"]["db_path"]@@ -151,6 +151,6 @@ def mount_agfs_backend(agfs: Any, agfs_config: Any) -> None:passtry:agfs.mount(plugin_name, mount_path, plugin_config.get("config", {}))- logger.debug(f"[RAGFSUtils] Successfully mounted {plugin_name} at {mount_path}")- except Exception as e:- logger.error(f"[RAGFSUtils] Failed to mount {plugin_name} at {mount_path}: {e}")+ logger.debug(f"[RAGFSUtils] Successfully mounted {plugin_name}")+ except Exception:+ logger.error(f"[RAGFSUtils] Failed to mount {plugin_name}")diff --git a/openviking_cli/client/base.py b/openviking_cli/client/base.pyindex e06fb826e..6fbcaf169 100644--- a/openviking_cli/client/base.py+++ b/openviking_cli/client/base.py@@ -45,6 +45,7 @@ class BaseClient(ABC):telemetry: TelemetryRequest = False,) -> Dict[str, Any]:"""Add resource to OpenViking."""+ ...@abstractmethodasync def add_skill(diff --git a/openviking_cli/utils/config/encryption_config.py b/openviking_cli/utils/config/encryption_config.pyindex c4f3e0c3e..2fb3f60d0 100644--- a/openviking_cli/utils/config/encryption_config.py+++ b/openviking_cli/utils/config/encryption_config.py@@ -84,17 +84,17 @@ class EncryptionConfig(BaseModel):)local: LocalEncryptionProviderConfig = Field(- default_factory=lambda: LocalEncryptionProviderConfig(),+ default_factory=LocalEncryptionProviderConfig,description="Local provider configuration",)vault: VaultEncryptionProviderConfig = Field(- default_factory=lambda: VaultEncryptionProviderConfig(),+ default_factory=VaultEncryptionProviderConfig,description="Vault provider configuration",)volcengine_kms: VolcengineKMSEncryptionProviderConfig = Field(- default_factory=lambda: VolcengineKMSEncryptionProviderConfig(),+ default_factory=VolcengineKMSEncryptionProviderConfig,description="Volcengine KMS provider configuration",)diff --git a/openviking_cli/utils/config/open_viking_config.py b/openviking_cli/utils/config/open_viking_config.pyindex 035c33e3c..8b6a92c89 100644--- a/openviking_cli/utils/config/open_viking_config.py+++ b/openviking_cli/utils/config/open_viking_config.py@@ -53,68 +53,58 @@ class OpenVikingConfig(BaseModel):default_agent: Optional[str] = Field(default="default", description="Default agent identifier")storage: StorageConfig = Field(- default_factory=lambda: StorageConfig(), description="Storage configuration"+ default_factory=StorageConfig, description="Storage configuration")embedding: EmbeddingConfig = Field(- default_factory=lambda: EmbeddingConfig(), description="Embedding configuration"+ default_factory=EmbeddingConfig, description="Embedding configuration")- vlm: VLMConfig = Field(default_factory=lambda: VLMConfig(), description="VLM configuration")+ vlm: VLMConfig = Field(default_factory=VLMConfig, description="VLM configuration")- rerank: RerankConfig = Field(- default_factory=lambda: RerankConfig(), description="Rerank configuration"- )+ rerank: RerankConfig = Field(default_factory=RerankConfig, description="Rerank configuration")# Encryption configurationencryption: EncryptionConfig = Field(- default_factory=lambda: EncryptionConfig(), description="Encryption configuration"+ default_factory=EncryptionConfig, description="Encryption configuration")# Parser configurations- pdf: PDFConfig = Field(- default_factory=lambda: PDFConfig(), description="PDF parsing configuration"- )+ pdf: PDFConfig = Field(default_factory=PDFConfig, description="PDF parsing configuration")- code: CodeConfig = Field(- default_factory=lambda: CodeConfig(), description="Code parsing configuration"- )+ code: CodeConfig = Field(default_factory=CodeConfig, description="Code parsing configuration")image: ImageConfig = Field(- default_factory=lambda: ImageConfig(), description="Image parsing configuration"+ default_factory=ImageConfig, description="Image parsing configuration")audio: AudioConfig = Field(- default_factory=lambda: AudioConfig(), description="Audio parsing configuration"+ default_factory=AudioConfig, description="Audio parsing configuration")video: VideoConfig = Field(- default_factory=lambda: VideoConfig(), description="Video parsing configuration"+ default_factory=VideoConfig, description="Video parsing configuration")markdown: MarkdownConfig = Field(- default_factory=lambda: MarkdownConfig(), description="Markdown parsing configuration"+ default_factory=MarkdownConfig, description="Markdown parsing configuration")- html: HTMLConfig = Field(- default_factory=lambda: HTMLConfig(), description="HTML parsing configuration"- )+ html: HTMLConfig = Field(default_factory=HTMLConfig, description="HTML parsing configuration")- text: TextConfig = Field(- default_factory=lambda: TextConfig(), description="Text parsing configuration"- )+ text: TextConfig = Field(default_factory=TextConfig, description="Text parsing configuration")directory: DirectoryConfig = Field(- default_factory=lambda: DirectoryConfig(), description="Directory parsing configuration"+ default_factory=DirectoryConfig, description="Directory parsing configuration")feishu: FeishuConfig = Field(- default_factory=lambda: FeishuConfig(),+ default_factory=FeishuConfig,description="Feishu/Lark document parsing configuration",)semantic: SemanticConfig = Field(- default_factory=lambda: SemanticConfig(),+ default_factory=SemanticConfig,description="Semantic processing configuration (overview/abstract limits)",)@@ -149,17 +139,15 @@ class OpenVikingConfig(BaseModel):),)- log: LogConfig = Field(default_factory=lambda: LogConfig(), description="Logging configuration")+ log: LogConfig = Field(default_factory=LogConfig, description="Logging configuration")- memory: MemoryConfig = Field(- default_factory=lambda: MemoryConfig(), description="Memory configuration"- )+ memory: MemoryConfig = Field(default_factory=MemoryConfig, description="Memory configuration")telemetry: "TelemetryConfig" = Field(- default_factory=lambda: TelemetryConfig(), description="Telemetry configuration"+ default_factory=TelemetryConfig, description="Telemetry configuration")prompts: PromptsConfig = Field(- default_factory=lambda: PromptsConfig(),+ default_factory=PromptsConfig,description="Prompt template configuration",)diff --git a/openviking_cli/utils/config/storage_config.py b/openviking_cli/utils/config/storage_config.pyindex bcdb7eb5a..59aa2430c 100644--- a/openviking_cli/utils/config/storage_config.py+++ b/openviking_cli/utils/config/storage_config.py@@ -24,15 +24,15 @@ class StorageConfig(BaseModel):workspace: str = Field(default="./data", description="Local data storage path (primary)")- agfs: AGFSConfig = Field(default_factory=lambda: AGFSConfig(), description="AGFS configuration")+ agfs: AGFSConfig = Field(default_factory=AGFSConfig, description="AGFS configuration")transaction: TransactionConfig = Field(- default_factory=lambda: TransactionConfig(),+ default_factory=TransactionConfig,description="Transaction mechanism configuration",)vectordb: VectorDBBackendConfig = Field(- default_factory=lambda: VectorDBBackendConfig(),+ default_factory=VectorDBBackendConfig,description="VectorDB backend configuration",)diff --git a/openviking_cli/utils/config/vectordb_config.py b/openviking_cli/utils/config/vectordb_config.pyindex 9f9f1739b..bf8c04b2b 100644--- a/openviking_cli/utils/config/vectordb_config.py+++ b/openviking_cli/utils/config/vectordb_config.py@@ -99,13 +99,13 @@ class VectorDBBackendConfig(BaseModel):)volcengine: Optional[VolcengineConfig] = Field(- default_factory=lambda: VolcengineConfig(),+ default_factory=VolcengineConfig,description="Volcengine VikingDB configuration for 'volcengine' type",)# VikingDB private deployment modevikingdb: Optional[VikingDBConfig] = Field(- default_factory=lambda: VikingDBConfig(),+ default_factory=VikingDBConfig,description="VikingDB private deployment configuration for 'vikingdb' type",)diff --git a/tests/api_test/tools/config/generate_config.py b/tests/api_test/tools/config/generate_config.pyindex 13678a00d..5f864ca24 100644--- a/tests/api_test/tools/config/generate_config.py+++ b/tests/api_test/tools/config/generate_config.py@@ -18,4 +18,11 @@ with open(config_path, "w") as f:json.dump(config, f, indent=2)print(f"Generated config at {config_path}")-print(json.dumps(config, indent=2))+redacted_config = {+ **config,+ "server": {+ **config["server"],+ "root_api_key": "<redacted>",+ },+}+print(json.dumps(redacted_config, indent=2))diff --git a/tests/api_test/tools/tests/test_admin_api.py b/tests/api_test/tools/tests/test_admin_api.pyindex 19e31e9e1..b35a7cf9d 100644--- a/tests/api_test/tools/tests/test_admin_api.py+++ b/tests/api_test/tools/tests/test_admin_api.py@@ -3,11 +3,19 @@ import jsonfrom api.client import OpenVikingAPIClient+def _redact_headers(headers):+ redacted = dict(headers)+ for key in list(redacted):+ if key.lower() in {"x-api-key", "authorization"}:+ redacted[key] = "<redacted>"+ return redacted++def main():client = OpenVikingAPIClient()- print("Current API Key:", client.api_key)- print("Current Headers:", client.session.headers)+ print("Current API Key configured:", bool(client.api_key))+ print("Current Headers:", _redact_headers(client.session.headers))print("\n" + "=" * 80 + "\n")print("Testing admin_list_accounts...")diff --git a/tests/api_test/tools/tests/test_config_value.py b/tests/api_test/tools/tests/test_config_value.pyindex 344b82bd1..0a2683696 100644--- a/tests/api_test/tools/tests/test_config_value.py+++ b/tests/api_test/tools/tests/test_config_value.py@@ -8,7 +8,7 @@ try:print("Config loaded:")print(f" host: {config.host}")print(f" port: {config.port}")- print(f" root_api_key: {config.root_api_key}")+ print(f" root_api_key configured: {bool(config.root_api_key)}")print(f" type(root_api_key): {type(config.root_api_key)}")if config.root_api_key:diff --git a/tests/api_test/tools/tests/test_create_app.py b/tests/api_test/tools/tests/test_create_app.pyindex 2d27030b0..42574b85d 100644--- a/tests/api_test/tools/tests/test_create_app.py+++ b/tests/api_test/tools/tests/test_create_app.py@@ -9,7 +9,7 @@ try:print("load_server_config result:")print(f" host: {config.host}")print(f" port: {config.port}")- print(f" root_api_key: {config.root_api_key}")+ print(f" root_api_key configured: {bool(config.root_api_key)}")print(f" type(root_api_key): {type(config.root_api_key)}")print("\nCreating app...")diff --git a/tests/api_test/tools/tests/test_full_startup.py b/tests/api_test/tools/tests/test_full_startup.pyindex 40f8364f7..4d78e1f2a 100644--- a/tests/api_test/tools/tests/test_full_startup.py+++ b/tests/api_test/tools/tests/test_full_startup.py@@ -13,7 +13,7 @@ async def test_lifespan():print("Config loaded:")print(f" host: {config.host}")print(f" port: {config.port}")- print(f" root_api_key: {config.root_api_key}")+ print(f" root_api_key configured: {bool(config.root_api_key)}")print("\nCreating app...")app = create_app(config)diff --git a/tests/api_test/tools/tests/test_headers.py b/tests/api_test/tools/tests/test_headers.pyindex 1a656320c..7049f311f 100644--- a/tests/api_test/tools/tests/test_headers.py+++ b/tests/api_test/tools/tests/test_headers.py@@ -7,13 +7,22 @@ sys.path.insert(0, ".")from api.client import OpenVikingAPIClient++def _redact_headers(headers):+ redacted = dict(headers)+ for key in list(redacted):+ if key.lower() in {"x-api-key", "authorization"}:+ redacted[key] = "<redacted>"+ return redacted++client = OpenVikingAPIClient()print("Client configuration:")-print(f" API Key: {client.api_key}")+print(f" API Key configured: {bool(client.api_key)}")print(f" Account: {client.account}")print(f" User: {client.user}")-print(f" Session headers: {dict(client.session.headers)}")+print(f" Session headers: {_redact_headers(client.session.headers)}")print("\n" + "=" * 80)print("Testing admin list accounts with debug...")@@ -22,7 +31,7 @@ url = f"{client.server_url}/api/v1/admin/accounts"headers = dict(client.session.headers)print(f"URL: {url}")-print(f"Headers: {json.dumps(headers, indent=2)}")+print(f"Headers: {json.dumps(_redact_headers(headers), indent=2)}")try:response = requests.get(url, headers=headers)diff --git a/tests/api_test/tools/tests/test_lifespan.py b/tests/api_test/tools/tests/test_lifespan.pyindex a707a6c31..ec187f9fd 100644--- a/tests/api_test/tools/tests/test_lifespan.py+++ b/tests/api_test/tools/tests/test_lifespan.py@@ -13,7 +13,7 @@ async def test_lifespan():print("load_server_config result:")print(f" host: {config.host}")print(f" port: {config.port}")- print(f" root_api_key: {config.root_api_key}")+ print(f" root_api_key configured: {bool(config.root_api_key)}")print("\nCreating app...")app = create_app(config)diff --git a/tests/api_test/tools/tests/test_load_config.py b/tests/api_test/tools/tests/test_load_config.pyindex 789fc8dc0..42a544908 100644--- a/tests/api_test/tools/tests/test_load_config.py+++ b/tests/api_test/tools/tests/test_load_config.py@@ -1,4 +1,5 @@import os+from copy import deepcopyfrom openviking.server.config import load_server_config@@ -10,7 +11,7 @@ try:print("Config loaded successfully!")print(f" host: {config.host}")print(f" port: {config.port}")- print(f" root_api_key: {config.root_api_key}")+ print(f" root_api_key configured: {bool(config.root_api_key)}")print(f" workers: {config.workers}")except Exception as e:print(f"Error loading config: {e}")@@ -29,8 +30,14 @@ try:import jsondata = json.load(f)- print(f"File content: {json.dumps(data, indent=2)}")- print(f"\nserver section: {json.dumps(data.get('server', {}), indent=2)}")- print(f"root_api_key from file: {data.get('server', {}).get('root_api_key')}")+ redacted_data = deepcopy(data)+ if "server" in redacted_data and isinstance(redacted_data["server"], dict):+ if "root_api_key" in redacted_data["server"]:+ redacted_data["server"]["root_api_key"] = "<redacted>"+ print(f"File content: {json.dumps(redacted_data, indent=2)}")+ print(f"\nserver section: {json.dumps(redacted_data.get('server', {}), indent=2)}")+ print(+ f"root_api_key configured in file: {bool(data.get('server', {}).get('root_api_key'))}"+ )except Exception as e:print(f"Error reading file: {e}")diff --git a/tests/api_test/tools/tests/test_simple_startup.py b/tests/api_test/tools/tests/test_simple_startup.pyindex 9408596cf..05cb1932a 100644--- a/tests/api_test/tools/tests/test_simple_startup.py+++ b/tests/api_test/tools/tests/test_simple_startup.py@@ -9,7 +9,7 @@ try:print("Config loaded:")print(f" host: {config.host}")print(f" port: {config.port}")- print(f" root_api_key: {config.root_api_key}")+ print(f" root_api_key configured: {bool(config.root_api_key)}")print("\nCreating app...")app = create_app(config)@@ -27,7 +27,7 @@ try:manager = APIKeyManager(config.root_api_key, None)print("APIKeyManager created successfully!")- print(f" manager.root_api_key: {manager.root_api_key}")+ print(f" manager.root_api_key configured: {bool(manager.root_api_key)}")print("\nTesting manager.get_accounts()...")accounts = manager.get_accounts()diff --git a/tests/parse/test_feishu_parser.py b/tests/parse/test_feishu_parser.pyindex 499b68df5..a94d40a32 100644--- a/tests/parse/test_feishu_parser.py+++ b/tests/parse/test_feishu_parser.py@@ -107,6 +107,10 @@ class TestParseFeishuUrl:with pytest.raises(ValueError, match="Cannot parse"):FeishuParser._parse_feishu_url("https://example.feishu.cn/")+ def test_invalid_host_raises(self):+ with pytest.raises(ValueError, match="host not allowed"):+ FeishuParser._parse_feishu_url("https://evil.example/docx/doxcnABC123")+class TestIsFeishuUrl:def test_feishu_docx(self):@@ -561,15 +565,14 @@ class TestParseAsyncIntegration:mock_md_result.parse_time = 0.1mock_md_result.meta = {}- with patch("openviking.parse.parsers.feishu.MarkdownParser") as MockMD:- mock_md_instance = MagicMock()+ mock_md_instance = MagicMock()- async def _mock_parse_content(*a, **kw):- return mock_md_result+ async def _mock_parse_content(*a, **kw):+ return mock_md_result- mock_md_instance.parse_content = _mock_parse_content- MockMD.return_value = mock_md_instance+ mock_md_instance.parse_content = _mock_parse_content+ with patch.object(parser, "_get_markdown_parser", return_value=mock_md_instance):result = asyncio.get_event_loop().run_until_complete(parser.parse("https://example.feishu.cn/docx/test123"))@@ -606,15 +609,14 @@ class TestParseAsyncIntegration:mock_md_result.parse_time = 0.1mock_md_result.meta = {}- with patch("openviking.parse.parsers.feishu.MarkdownParser") as MockMD:- mock_md_instance = MagicMock()+ mock_md_instance = MagicMock()- async def _mock_parse_content(*a, **kw):- return mock_md_result+ async def _mock_parse_content(*a, **kw):+ return mock_md_result- mock_md_instance.parse_content = _mock_parse_content- MockMD.return_value = mock_md_instance+ mock_md_instance.parse_content = _mock_parse_content+ with patch.object(parser, "_get_markdown_parser", return_value=mock_md_instance):result = asyncio.get_event_loop().run_until_complete(parser.parse("https://example.feishu.cn/wiki/wiki_token"))@@ -634,3 +636,17 @@ class TestParseAsyncIntegration:assert result.warningsassert "Unsupported" in result.warnings[0]++ def test_parse_rejects_invalid_host(self):+ """Main parse() entry point must reject non-Feishu hosts."""+ parser = FeishuParser()++ result = asyncio.get_event_loop().run_until_complete(+ parser.parse("https://evil.example/docx/doxcn123")+ )++ assert result.source_format == "feishu"+ assert result.warnings+ assert "host not allowed" in result.warnings[0]+ assert "feishu_doc_type" not in result.meta+ assert "feishu_token" not in result.metadiff --git a/tests/service/test_session_service_metrics.py b/tests/service/test_session_service_metrics.pynew file mode 100644index 000000000..e038fc3d4--- /dev/null+++ b/tests/service/test_session_service_metrics.py@@ -0,0 +1,85 @@+# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.+# SPDX-License-Identifier: AGPL-3.0++from unittest.mock import AsyncMock, Mock++import pytest++import openviking.service.session_service as session_service_module+from openviking.metrics.datasources.session import SessionLifecycleDataSource+from openviking.server.identity import RequestContext, Role+from openviking.service.session_service import SessionService+from openviking_cli.session.user_id import UserIdentifier+++def _make_ctx() -> RequestContext:+ return RequestContext(+ user=UserIdentifier("acme", "alice", "agent1"),+ role=Role.ADMIN,+ )+++async def test_create_keeps_working_when_lifecycle_metrics_fail(monkeypatch: pytest.MonkeyPatch):+ service = SessionService()+ ctx = _make_ctx()+ session = Mock()+ session.exists = AsyncMock(return_value=False)+ session.ensure_exists = AsyncMock()+ debug = Mock()++ def _boom(*_args, **_kwargs):+ raise RuntimeError("metrics failed")++ monkeypatch.setattr(service, "session", Mock(return_value=session))+ monkeypatch.setattr(SessionLifecycleDataSource, "record_lifecycle", staticmethod(_boom))+ monkeypatch.setattr(session_service_module.logger, "debug", debug)++ result = await service.create(ctx, "sess-1")++ assert result is session+ session.exists.assert_awaited_once()+ session.ensure_exists.assert_awaited_once()+ assert debug.call_count == 2+++async def test_commit_async_keeps_working_when_session_metrics_fail(+ monkeypatch: pytest.MonkeyPatch,+):+ service = SessionService(viking_fs=Mock())+ ctx = _make_ctx()+ session = Mock()+ session.commit_async = AsyncMock(return_value={"status": "queued", "archived": False})+ debug = Mock()++ def _boom(*_args, **_kwargs):+ raise RuntimeError("metrics failed")++ monkeypatch.setattr(service, "get", AsyncMock(return_value=session))+ monkeypatch.setattr(SessionLifecycleDataSource, "record_lifecycle", staticmethod(_boom))+ monkeypatch.setattr(SessionLifecycleDataSource, "record_archive", staticmethod(_boom))+ monkeypatch.setattr(session_service_module.logger, "debug", debug)++ result = await service.commit_async("sess-1", ctx)++ assert result == {"status": "queued", "archived": False}+ session.commit_async.assert_awaited_once()+ assert debug.call_count == 2+++async def test_sessions_returns_empty_and_logs_when_storage_listing_fails(+ monkeypatch: pytest.MonkeyPatch,+):+ service = SessionService(viking_fs=Mock())+ ctx = _make_ctx()+ debug = Mock()++ service._viking_fs.ls = AsyncMock(side_effect=RuntimeError("ls failed"))+ monkeypatch.setattr(session_service_module.logger, "debug", debug)++ result = await service.sessions(ctx)++ assert result == []+ debug.assert_called_once()