Skip to content

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

test(bot): focus regression coverage and consolidate test suite (#5230)

DuTaoe1a0d3e0de03
diff --git a/bot/deploy/Dockerfile b/bot/deploy/Dockerfile
index e07ee30c1..d4a02339a 100644
--- a/bot/deploy/Dockerfile
+++ b/bot/deploy/Dockerfile
@@ -2,7 +2,7 @@ FROM python:3.13-slim-bookworm
# Install base tools, Node.js 20, and uv
RUN apt-get update && \
- apt-get install -y --no-install-recommends curl ca-certificates gnupg git ripgrep bubblewrap socat libfuse2 build-essential && \
+ apt-get install -y --no-install-recommends curl ca-certificates gnupg git bubblewrap socat libfuse2 build-essential && \
# Add NodeSource repository for Node.js 20 (new setup script, old node_20.x URL is deprecated)
curl -fsSL https://deb.nodesource.com/setup_20.x | bash - && \
apt-get install -y --no-install-recommends nodejs && \
@@ -37,9 +37,6 @@ ENV UV_HTTP_TIMEOUT=300
RUN uv pip install --system --no-cache "openviking>=0.3.9" && \
uv pip install --system --no-cache .
-# Install global npm packages for sandbox
-RUN npm install -g @anthropic-ai/sandbox-runtime
-
# ============================================
# Layer 2: Copy source code and build
# ============================================
diff --git a/bot/deploy/docker/Dockerfile b/bot/deploy/docker/Dockerfile
index 78f97c100..1d2fd81cd 100644
--- a/bot/deploy/docker/Dockerfile
+++ b/bot/deploy/docker/Dockerfile
@@ -5,7 +5,7 @@ FROM python:3.13-slim-bookworm
# Install system dependencies + Node.js 20 via NodeSource (npm/npx 自带,无需多阶段复制)
RUN apt-get update && \
- apt-get install -y --no-install-recommends curl ca-certificates git ripgrep bubblewrap socat libfuse2 build-essential gnupg && \
+ apt-get install -y --no-install-recommends curl ca-certificates git bubblewrap socat libfuse2 build-essential gnupg && \
curl -fsSL https://deb.nodesource.com/setup_20.x | bash - && \
apt-get install -y nodejs && \
curl -LsSf https://astral.sh/uv/install.sh | sh && \
@@ -27,9 +27,6 @@ RUN mkdir -p vikingbot
# Pre-install openviking from pip (linux/amd64 has pre-built wheel)
RUN uv pip install --system --no-cache "openviking>=0.3.9"
-# Install npm sandbox runtime
-RUN npm install -g @anthropic-ai/sandbox-runtime
-
# ============================================
# Layer 2: Install bridge npm deps (cached)
# ============================================
diff --git a/bot/pytest.ini b/bot/pytest.ini
new file mode 100644
index 000000000..2fb1cafc3
--- /dev/null
+++ b/bot/pytest.ini
@@ -0,0 +1,4 @@
+[pytest]
+testpaths = tests
+python_files = test_*.py
+asyncio_mode = auto
diff --git a/bot/scripts/test_all.sh b/bot/scripts/test_all.sh
index 7c17cef16..30e431dbb 100755
--- a/bot/scripts/test_all.sh
+++ b/bot/scripts/test_all.sh
@@ -1,50 +1,15 @@
#!/bin/bash
+set -euo pipefail
-# test_all.sh - Run all tests without triggering uv sync
-# Usage: scripts/test_all.sh
-
-set -e
-
-# Get the directory of this script
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
-PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
+REPO_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
+VIKINGBOT_TEST_PYTHON="${VIKINGBOT_TEST_PYTHON:-$REPO_ROOT/.venv/bin/python}"
-echo "=========================================="
-echo "Running OpenViking Tests"
-echo "=========================================="
-echo ""
-
-# Check if virtual environment exists
-VENV_PYTHON="$PROJECT_ROOT/.venv/bin/python"
-if [ ! -x "$VENV_PYTHON" ]; then
- echo "Error: Virtual environment not found at $VENV_PYTHON"
- echo "Please create a virtual environment first."
+if [ ! -x "$VIKINGBOT_TEST_PYTHON" ]; then
+ echo "Python not found: $VIKINGBOT_TEST_PYTHON" >&2
+ echo "Install the repository's bot/dev dependencies or set VIKINGBOT_TEST_PYTHON." >&2
exit 1
fi
-echo "Using Python from virtual environment: $VENV_PYTHON"
-echo ""
-
-# Change to project root
-cd "$PROJECT_ROOT"
-
-# Run pytest directly
-echo "Running tests..."
-echo "-----------------------------------------"
-"$VENV_PYTHON" -m pytest tests/ -v
-TEST_EXIT_CODE=$?
-echo "-----------------------------------------"
-echo ""
-
-# Show summary
-if [ $TEST_EXIT_CODE -eq 0 ]; then
- echo "=========================================="
- echo "✅ All tests passed!"
- echo "=========================================="
-else
- echo "=========================================="
- echo "❌ Some tests failed (exit code: $TEST_EXIT_CODE)"
- echo "=========================================="
-fi
-
-exit $TEST_EXIT_CODE
+cd "$REPO_ROOT"
+exec "$VIKINGBOT_TEST_PYTHON" -m pytest -c bot/pytest.ini bot/tests "$@"
diff --git a/bot/tests/README.md b/bot/tests/README.md
new file mode 100644
index 000000000..257a2172f
--- /dev/null
+++ b/bot/tests/README.md
@@ -0,0 +1,31 @@
+# VikingBot tests
+
+All Bot tests live in this directory, including provider and OpenViking session
+tests previously under `vikingbot/tests/unit`.
+
+From the repository root, with the bot and dev dependencies installed:
+
+```sh
+bash bot/scripts/test_all.sh -q
+```
+
+The runner uses the repository `.venv` without syncing dependencies. Set
+`VIKINGBOT_TEST_PYTHON` to use another Python environment. Pytest arguments are
+forwarded, for example `-k streaming` or `--cov=vikingbot`.
+
+Keep tests of observable behavior on active paths: agent/tool execution,
+streaming and provider failover, authentication and session isolation, memory
+recall/commit, sandbox boundaries, channel delivery, and Compile task lifecycle.
+Use real package imports and pytest-managed temporary directories. Mock external
+services at their boundaries; do not replace package modules to bypass imports.
+Parameterize cases that exercise the same path and assertions, and share setup
+for persisted sessions instead of copying JSONL fixtures across tests.
+Assert observable results rather than internal call avoidance, object identity,
+or unrelated behavior staying unchanged after a feature change. Keep negative
+checks that protect authorization, data integrity, and duplicate delivery.
+
+Enable the feature being tested before asserting that it rejects or skips work;
+otherwise an early return can make the test pass for the wrong reason. Check
+persisted state with a fresh reader. Budget and concurrency tests should also
+prove that useful work completed, and recovery tests should reach a successful
+retry. Keep each test focused on one behavior, with explicit expected results.
diff --git a/bot/tests/conftest.py b/bot/tests/conftest.py
index 71f8fbb74..f63e5fec2 100644
--- a/bot/tests/conftest.py
+++ b/bot/tests/conftest.py
@@ -1,31 +1,14 @@
# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.
# SPDX-License-Identifier: AGPL-3.0
-"""Global test fixtures"""
+"""Shared fixtures for the VikingBot suite."""
-import asyncio
-import shutil
from pathlib import Path
-from typing import Generator
import pytest
-# Test data root directory
-PROJECT_ROOT = Path(__file__).parent.parent
-TEST_TMP_DIR = PROJECT_ROOT / "test_data" / "tmp"
-
[email protected](scope="session")
-def event_loop():
- """Create session-level event loop"""
- loop = asyncio.new_event_loop()
- yield loop
- loop.close()
-
-
[email protected](scope="function")
-def temp_dir() -> Generator[Path, None, None]:
- """Create temp directory, auto-cleanup before and after test"""
- shutil.rmtree(TEST_TMP_DIR, ignore_errors=True)
- TEST_TMP_DIR.mkdir(parents=True, exist_ok=True)
- yield TEST_TMP_DIR
+def temp_dir(tmp_path: Path) -> Path:
+ """Give each test its own pytest-managed directory."""
+ return tmp_path
diff --git a/bot/tests/experience_data_mini.json b/bot/tests/experience_data_mini.json
deleted file mode 100644
index cf31d01fe..000000000
--- a/bot/tests/experience_data_mini.json
+++ /dev/null
@@ -1,10 +0,0 @@
-[
- {
- "unique_id": "rust-lang/rust169585762",
- "repo": "rust-lang/rust",
- "issue_id": "169585762",
- "bug_description": "Inconsistent error message formatting for duplicate import conflicts in the compiler's resolve phase.",
- "content_preview": "<issue_start><issue_comment>Title: Updated error message E0252\nusername_0: Fixes #35306 as part of #35233.\n\nr? @username_2\n<issue_comment>username_1: Thanks for the pull request, and welcome! The Rust",
- "fix_experience": "{\n \"Signals\": [\"inconsistent\", \"error message\", \"formatting\", \"duplicate\", \"import\"],\n \"Root Cause\": \"The compiler's resolve phase lacked specific span labels for duplicate import conflicts, leading to unclear error diagnostics.\",\n \"Fix Strategy\": \"Enhance error diagnostics by adding specific span labels to duplicate import errors to clarify the conflicting import statements.\",\n \"Patch Digest\": \"Modified E0252 error handling to create structured errors with span labels, added 'already imported' label to conflicting import spans, and updated test cases for new error message format.\",\n \"Verification\": \"Verify the fix by running updated test cases to ensure the new error message format with additional notes and span labels is correctly displayed for duplicate import conflicts.\"\n}"
- }
-]
\ No newline at end of file
diff --git a/bot/tests/test_agent_loop_outcome.py b/bot/tests/test_agent_loop_outcome.py
index 7b8f17806..8643cb673 100644
--- a/bot/tests/test_agent_loop_outcome.py
+++ b/bot/tests/test_agent_loop_outcome.py
@@ -1,12 +1,9 @@
import copy
-import sys
from datetime import datetime
from pathlib import Path
+from unittest.mock import AsyncMock
import pytest
-
-sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
-
from vikingbot.agent import loop as loop_module
from vikingbot.agent.context import ContextBuilder
from vikingbot.agent.loop import AgentLoop
@@ -14,7 +11,7 @@ from vikingbot.agent.tools.base import MultimodalToolResult
from vikingbot.agent.tools.registry import ToolExecutionResult
from vikingbot.bus.events import InboundMessage, OutboundEventType
from vikingbot.bus.queue import MessageBus
-from vikingbot.config.schema import AgentsConfig, Config, SessionKey
+from vikingbot.config.schema import Config, SessionKey
from vikingbot.openviking_mount.session_state import make_openviking_storage_session_id
from vikingbot.providers.base import LLMProvider, LLMResponse, ToolCallRequest
from vikingbot.session.manager import SessionManager
@@ -60,55 +57,89 @@ class _FakeLangfuseClient:
class _FakeOVClient:
- def __init__(self, *, context_payload=None, pending_tokens=None):
- self.context_payload = context_payload or {}
- self.pending_tokens = list(pending_tokens or [])
+ def __init__(self, *, context_payload):
+ self.context_payload = context_payload
self.context_calls = []
- self.append_calls = []
- self.commit_calls = []
- self.session_calls = []
async def get_session_context(self, session_id, token_budget):
self.context_calls.append((session_id, token_budget))
return self.context_payload
- async def append_messages(
- self,
- session_id,
- messages,
- default_user_peer_id=None,
- session_user_id=None,
- ):
- self.append_calls.append(
- (session_id, list(messages), default_user_peer_id, session_user_id)
- )
- return {"session_id": session_id, "added": len(messages), "message_count": len(messages)}
-
- async def get_session(self, session_id, user_id=None):
- self.session_calls.append((session_id, user_id))
- next_pending_tokens = self.pending_tokens.pop(0) if self.pending_tokens else 0
- return {"session_id": session_id, "pending_tokens": next_pending_tokens}
-
- async def commit_session(self, session_id, keep_recent_count=0, user_id=None):
- self.commit_calls.append((session_id, keep_recent_count, user_id))
- return {"session_id": session_id, "status": "accepted"}
+def make_loop(temp_dir, monkeypatch):
+ """Construct a real loop with external integrations stubbed for these tests."""
+ monkeypatch.setattr(AgentLoop, "_register_builtin_hooks", lambda self: None)
+ monkeypatch.setattr("vikingbot.agent.loop.SubagentManager", _FakeSubagentManager)
-def test_agents_config_temperature_schema_caps_at_two():
- schema = AgentsConfig.model_json_schema()
- temperature = schema["properties"]["temperature"]
+ def create(*, config=None, provider=None, bus=None, register_tools=False, **kwargs):
+ with monkeypatch.context() as setup:
+ if not register_tools:
+ setup.setattr(AgentLoop, "_register_default_tools", lambda self: None)
+ return AgentLoop(
+ bus=bus if bus is not None else MessageBus(),
+ provider=provider if provider is not None else _FakeProvider(),
+ workspace=temp_dir / "workspace",
+ config=config if config is not None else Config(storage_workspace=str(temp_dir)),
+ **kwargs,
+ )
- assert temperature["default"] == 0.7
- assert temperature["minimum"] == 0.0
- assert temperature["maximum"] == 2.0
+ return create
-def test_agents_config_enables_subagents_by_default():
- assert AgentsConfig().subagent_enabled is True
+def _image_call(call_id, label=None):
+ return ToolCallRequest(
+ id=call_id, name="read_image", arguments={"label": label} if label else {}, tokens=1
+ )
-def test_agents_config_keeps_ten_recent_openviking_messages_by_default():
- assert AgentsConfig().commit_keep_recent_count == 10
+class _MediaProvider(_FakeProvider):
+ def __init__(self, rounds, *, supports_media=True):
+ super().__init__()
+ self.calls = []
+ self.responses = iter(
+ [LLMResponse(content=None, tool_calls=calls) for calls in rounds]
+ + [LLMResponse(content="done")]
+ )
+ self.supports_media = supports_media
+
+ async def chat(self, messages, **kwargs):
+ self.calls.append(copy.deepcopy(messages))
+ return next(self.responses)
+
+ def supports_tool_result_media(self, model=None):
+ return self.supports_media
+
+
+class _ImageRegistry:
+ def __init__(self, content=None):
+ self.content = content
+
+ def get_definitions(self, **kwargs):
+ return [
+ {
+ "type": "function",
+ "function": {
+ "name": "read_image",
+ "description": "Read an image",
+ "parameters": {"type": "object", "properties": {}},
+ },
+ }
+ ]
+
+ async def execute_detailed(self, name, params, **kwargs):
+ label = params.get("label", "Image resource.")
+ content = (
+ self.content
+ if self.content is not None
+ else [
+ {"type": "text", "text": label},
+ {"type": "image_url", "image_url": {"url": "data:image/png;base64,ZGF0YQ=="}},
+ ]
+ )
+ return ToolExecutionResult(
+ result=MultimodalToolResult(text=label, content=content), effective_params=params
+ )
def test_context_keeps_multimodal_tool_result_on_tool_message(temp_dir: Path):
@@ -139,13 +170,7 @@ def test_context_keeps_multimodal_tool_result_on_tool_message(temp_dir: Path):
@pytest.mark.asyncio
@pytest.mark.parametrize("supports_media", [False, True])
-async def test_agent_loop_gates_multimodal_result_by_provider(
- temp_dir: Path, monkeypatch, supports_media: bool
-):
- monkeypatch.setattr(AgentLoop, "_register_builtin_hooks", lambda self: None)
- monkeypatch.setattr(AgentLoop, "_register_default_tools", lambda self: None)
- monkeypatch.setattr("vikingbot.agent.loop.SubagentManager", _FakeSubagentManager)
-
+async def test_agent_loop_gates_multimodal_result_by_provider(make_loop, supports_media: bool):
content = [
{"type": "text", "text": "Source: viking://resources/image.png"},
{
@@ -154,66 +179,14 @@ async def test_agent_loop_gates_multimodal_result_by_provider(
},
]
- class Provider(LLMProvider):
- def __init__(self):
- super().__init__()
- self.calls = []
-
- async def chat(self, messages, tools=None, **kwargs):
- self.calls.append(messages)
- if len(self.calls) == 1:
- return LLMResponse(
- content=None,
- tool_calls=[
- ToolCallRequest(
- id="call-1",
- name="read_image",
- arguments={},
- tokens=1,
- )
- ],
- )
- return LLMResponse(content="done")
-
- def get_default_model(self) -> str:
- return "fake-model"
-
- def supports_tool_result_media(self, model=None) -> bool:
- return supports_media
-
- class Registry:
- def get_definitions(self, **kwargs):
- return [
- {
- "type": "function",
- "function": {
- "name": "read_image",
- "description": "Read an image",
- "parameters": {"type": "object", "properties": {}},
- },
- }
- ]
-
- async def execute_detailed(self, name, params, **kwargs):
- return ToolExecutionResult(
- result=MultimodalToolResult(text="Image resource.", content=content),
- effective_params=params,
- )
-
- provider = Provider()
- loop = AgentLoop(
- bus=MessageBus(),
- provider=provider,
- workspace=temp_dir / "workspace",
- config=Config(storage_workspace=str(temp_dir)),
- max_iterations=2,
- )
+ provider = _MediaProvider([[_image_call("call-1")]], supports_media=supports_media)
+ loop = make_loop(provider=provider, max_iterations=2)
final, _reasoning, tools_used, _usage, _iteration = await loop._run_agent_loop(
messages=[{"role": "user", "content": "read it"}],
session_key=SessionKey(type="cli", channel_id="default", chat_id="multimodal"),
publish_events=False,
- tool_registry=Registry(),
+ tool_registry=_ImageRegistry(content),
)
assert final == "done"
@@ -227,88 +200,17 @@ async def test_agent_loop_gates_multimodal_result_by_provider(
@pytest.mark.asyncio
-async def test_agent_loop_limits_media_across_parallel_tool_results(temp_dir: Path, monkeypatch):
- monkeypatch.setattr(AgentLoop, "_register_builtin_hooks", lambda self: None)
- monkeypatch.setattr(AgentLoop, "_register_default_tools", lambda self: None)
- monkeypatch.setattr("vikingbot.agent.loop.SubagentManager", _FakeSubagentManager)
+async def test_agent_loop_limits_media_across_parallel_tool_results(make_loop, monkeypatch):
monkeypatch.setattr(loop_module, "MAX_INLINE_TOOL_RESULT_MEDIA_BYTES", 5)
- class Provider(LLMProvider):
- def __init__(self):
- super().__init__()
- self.calls = []
-
- async def chat(self, messages, tools=None, **kwargs):
- self.calls.append(messages)
- if len(self.calls) == 1:
- return LLMResponse(
- content=None,
- tool_calls=[
- ToolCallRequest(
- id="call-1",
- name="read_image",
- arguments={"label": "first"},
- tokens=1,
- ),
- ToolCallRequest(
- id="call-2",
- name="read_image",
- arguments={"label": "second"},
- tokens=1,
- ),
- ],
- )
- return LLMResponse(content="done")
-
- def get_default_model(self) -> str:
- return "fake-model"
-
- def supports_tool_result_media(self, model=None) -> bool:
- return True
-
- class Registry:
- def get_definitions(self, **kwargs):
- return [
- {
- "type": "function",
- "function": {
- "name": "read_image",
- "description": "Read an image",
- "parameters": {"type": "object", "properties": {}},
- },
- }
- ]
-
- async def execute_detailed(self, name, params, **kwargs):
- label = params["label"]
- return ToolExecutionResult(
- result=MultimodalToolResult(
- text=f"{label} image",
- content=[
- {"type": "text", "text": label},
- {
- "type": "image_url",
- "image_url": {"url": "data:image/png;base64,ZGF0YQ=="},
- },
- ],
- ),
- effective_params=params,
- )
-
- provider = Provider()
- loop = AgentLoop(
- bus=MessageBus(),
- provider=provider,
- workspace=temp_dir / "workspace",
- config=Config(storage_workspace=str(temp_dir)),
- max_iterations=2,
- )
+ provider = _MediaProvider([[_image_call("call-1", "first"), _image_call("call-2", "second")]])
+ loop = make_loop(provider=provider, max_iterations=2)
final, *_ = await loop._run_agent_loop(
messages=[{"role": "user", "content": "read both"}],
session_key=SessionKey(type="cli", channel_id="default", chat_id="media-budget"),
publish_events=False,
- tool_registry=Registry(),
+ tool_registry=_ImageRegistry(),
)
assert final == "done"
@@ -319,85 +221,19 @@ async def test_agent_loop_limits_media_across_parallel_tool_results(temp_dir: Pa
@pytest.mark.asyncio
-async def test_agent_loop_prefers_new_media_across_consecutive_tool_rounds(
- temp_dir: Path, monkeypatch
-):
- monkeypatch.setattr(AgentLoop, "_register_builtin_hooks", lambda self: None)
- monkeypatch.setattr(AgentLoop, "_register_default_tools", lambda self: None)
- monkeypatch.setattr("vikingbot.agent.loop.SubagentManager", _FakeSubagentManager)
+async def test_agent_loop_prefers_new_media_across_consecutive_tool_rounds(make_loop, monkeypatch):
monkeypatch.setattr(loop_module, "MAX_INLINE_TOOL_RESULT_MEDIA_BYTES", 5)
- class Provider(LLMProvider):
- def __init__(self):
- super().__init__()
- self.calls = []
-
- async def chat(self, messages, tools=None, **kwargs):
- self.calls.append(copy.deepcopy(messages))
- call_number = len(self.calls)
- if call_number <= 2:
- return LLMResponse(
- content=None,
- tool_calls=[
- ToolCallRequest(
- id=f"call-{call_number}",
- name="read_image",
- arguments={"label": f"image-{call_number}"},
- tokens=1,
- )
- ],
- )
- return LLMResponse(content="done")
-
- def get_default_model(self) -> str:
- return "fake-model"
-
- def supports_tool_result_media(self, model=None) -> bool:
- return True
-
- class Registry:
- def get_definitions(self, **kwargs):
- return [
- {
- "type": "function",
- "function": {
- "name": "read_image",
- "description": "Read an image",
- "parameters": {"type": "object", "properties": {}},
- },
- }
- ]
-
- async def execute_detailed(self, name, params, **kwargs):
- label = params["label"]
- return ToolExecutionResult(
- result=MultimodalToolResult(
- text=f"{label} result",
- content=[
- {"type": "text", "text": label},
- {
- "type": "image_url",
- "image_url": {"url": "data:image/png;base64,ZGF0YQ=="},
- },
- ],
- ),
- effective_params=params,
- )
-
- provider = Provider()
- loop = AgentLoop(
- bus=MessageBus(),
- provider=provider,
- workspace=temp_dir / "workspace",
- config=Config(storage_workspace=str(temp_dir)),
- max_iterations=3,
+ provider = _MediaProvider(
+ [[_image_call("call-1", "image-1")], [_image_call("call-2", "image-2")]]
)
+ loop = make_loop(provider=provider, max_iterations=3)
final, *_ = await loop._run_agent_loop(
messages=[{"role": "user", "content": "read two images in sequence"}],
session_key=SessionKey(type="cli", channel_id="default", chat_id="media-rounds"),
publish_events=False,
- tool_registry=Registry(),
+ tool_registry=_ImageRegistry(),
)
assert final == "done"
@@ -410,37 +246,26 @@ async def test_agent_loop_prefers_new_media_across_consecutive_tool_rounds(
assert isinstance(tool_messages[1]["content"], list)
-def test_agent_loop_omits_spawn_tool_when_subagents_disabled(temp_dir: Path, monkeypatch):
- monkeypatch.setattr(AgentLoop, "_register_builtin_hooks", lambda self: None)
- monkeypatch.setattr("vikingbot.agent.loop.SubagentManager", _FakeSubagentManager)
-
+def test_agent_loop_omits_spawn_tool_when_subagents_disabled(make_loop, temp_dir: Path):
bus = MessageBus()
provider = _RecordingProvider()
config = Config(storage_workspace=str(temp_dir), agents={"subagent_enabled": False})
- loop = AgentLoop(
+ loop = make_loop(
bus=bus,
provider=provider,
- workspace=temp_dir / "workspace",
model=config.agents.model,
temperature=config.agents.temperature,
config=config,
+ register_tools=True,
)
assert "spawn" not in loop.tools.tool_names
-def test_agent_loop_standalone_omits_openviking_tools(temp_dir: Path, monkeypatch):
- monkeypatch.setattr(AgentLoop, "_register_builtin_hooks", lambda self: None)
- monkeypatch.setattr("vikingbot.agent.loop.SubagentManager", _FakeSubagentManager)
-
+def test_agent_loop_standalone_omits_openviking_tools(make_loop, temp_dir: Path):
config = Config(storage_workspace=str(temp_dir))
- loop = AgentLoop(
- bus=MessageBus(),
- provider=_RecordingProvider(),
- workspace=temp_dir / "workspace",
- config=config,
- )
+ loop = make_loop(provider=_RecordingProvider(), config=config, register_tools=True)
assert config.ov_server.server_url == ""
assert not any(name.startswith("openviking_") for name in loop.tools.tool_names)
@@ -449,28 +274,13 @@ def test_agent_loop_standalone_omits_openviking_tools(temp_dir: Path, monkeypatc
@pytest.mark.asyncio
-async def test_context_prompt_omits_subagent_capability_when_disabled(temp_dir: Path):
- context = ContextBuilder(workspace=temp_dir / "workspace", enable_subagents=False)
- session_key = SessionKey(type="cli", channel_id="default", chat_id="session-1")
-
- prompt = await context._get_identity(session_key)
-
- assert "Spawn subagents" not in prompt
-
-
-async def test_agent_loop_passes_configured_temperature_to_provider(temp_dir: Path, monkeypatch):
- monkeypatch.setattr(AgentLoop, "_register_builtin_hooks", lambda self: None)
- monkeypatch.setattr(AgentLoop, "_register_default_tools", lambda self: None)
- monkeypatch.setattr("vikingbot.agent.loop.SubagentManager", _FakeSubagentManager)
-
+async def test_agent_loop_passes_configured_temperature_to_provider(make_loop, temp_dir: Path):
bus = MessageBus()
provider = _RecordingProvider()
config = Config(storage_workspace=str(temp_dir), agents={"temperature": 0.2})
- loop = AgentLoop(
+ loop = make_loop(
bus=bus,
provider=provider,
- workspace=temp_dir / "workspace",
model=config.agents.model,
temperature=config.agents.temperature,
config=config,
@@ -491,12 +301,8 @@ async def test_agent_loop_passes_configured_temperature_to_provider(temp_dir: Pa
@pytest.mark.asyncio
async def test_agent_loop_makes_final_no_tool_call_when_iteration_limit_reached(
- temp_dir: Path, monkeypatch
+ make_loop, temp_dir: Path
):
- monkeypatch.setattr(AgentLoop, "_register_builtin_hooks", lambda self: None)
- monkeypatch.setattr(AgentLoop, "_register_default_tools", lambda self: None)
- monkeypatch.setattr("vikingbot.agent.loop.SubagentManager", _FakeSubagentManager)
-
class _ToolLimitProvider(LLMProvider):
def __init__(self):
super().__init__()
@@ -567,13 +373,7 @@ async def test_agent_loop_makes_final_no_tool_call_when_iteration_limit_reached(
tools = _ToolRegistry()
bus = MessageBus()
config = Config(storage_workspace=str(temp_dir))
- loop = AgentLoop(
- bus=bus,
- provider=provider,
- workspace=temp_dir / "workspace",
- config=config,
- max_iterations=1,
- )
+ loop = make_loop(bus=bus, provider=provider, config=config, max_iterations=1)
loop.tools = tools
session_key = SessionKey(type="cli", channel_id="default", chat_id="session-limit")
@@ -616,17 +416,15 @@ async def test_agent_loop_makes_final_no_tool_call_when_iteration_limit_reached(
"tool result: useful context",
"tool result: useful context",
]
- assert token_usage == {"prompt_tokens": 17, "completion_tokens": 7, "total_tokens": 24}
+ assert {
+ key: token_usage[key] for key in ("prompt_tokens", "completion_tokens", "total_tokens")
+ } == {"prompt_tokens": 17, "completion_tokens": 7, "total_tokens": 24}
@pytest.mark.asyncio
async def test_agent_loop_evaluates_previous_response_outcome_before_openviking_precommit_clear(
- temp_dir: Path, monkeypatch
+ make_loop, temp_dir: Path, monkeypatch
):
- monkeypatch.setattr(AgentLoop, "_register_builtin_hooks", lambda self: None)
- monkeypatch.setattr(AgentLoop, "_register_default_tools", lambda self: None)
- monkeypatch.setattr("vikingbot.agent.loop.SubagentManager", _FakeSubagentManager)
-
async def fake_run_agent_loop(self, **kwargs):
return "final answer", None, [], {"prompt_tokens": 1, "completion_tokens": 1}, 1
@@ -646,12 +444,7 @@ async def test_agent_loop_evaluates_previous_response_outcome_before_openviking_
"commit_keep_recent_count": 0,
},
)
- loop = AgentLoop(
- bus=bus,
- provider=_FakeProvider(),
- workspace=temp_dir / "workspace",
- config=config,
- )
+ loop = make_loop(bus=bus, config=config)
async def fake_precommit(session, msg):
session.clear()
@@ -682,18 +475,16 @@ async def test_agent_loop_evaluates_previous_response_outcome_before_openviking_
outcome_event = await bus.consume_outbound()
assert outcome_event.event_type == OutboundEventType.RESPONSE_OUTCOME_EVALUATED
assert outcome_event.response_id == "resp-123"
- persisted_session = loop.sessions.get_or_create(session_key, skip_heartbeat=True)
+ persisted_session = SessionManager(config.bot_data_path).get_or_create(
+ session_key, skip_heartbeat=True
+ )
assert persisted_session.metadata["response_outcomes"]["resp-123"]["outcome_label"] == "reasked"
@pytest.mark.asyncio
async def test_agent_loop_build_prompt_history_uses_ov_context_plus_unsynced_tail(
- temp_dir: Path, monkeypatch
+ make_loop, temp_dir: Path, monkeypatch
):
- monkeypatch.setattr(AgentLoop, "_register_builtin_hooks", lambda self: None)
- monkeypatch.setattr(AgentLoop, "_register_default_tools", lambda self: None)
- monkeypatch.setattr("vikingbot.agent.loop.SubagentManager", _FakeSubagentManager)
-
fake_ov_client = _FakeOVClient(
context_payload={
"latest_archive_overview": "Earlier summary",
@@ -704,11 +495,7 @@ async def test_agent_loop_build_prompt_history_uses_ov_context_plus_unsynced_tai
}
)
- async def fake_get_ov_client(self, session_key, openviking_connection=None, actor_peer_id=None):
- del session_key, openviking_connection, actor_peer_id
- return fake_ov_client
-
- monkeypatch.setattr(AgentLoop, "_get_ov_client", fake_get_ov_client)
+ monkeypatch.setattr(AgentLoop, "_get_ov_client", AsyncMock(return_value=fake_ov_client))
bus = MessageBus()
config = Config(
@@ -716,12 +503,7 @@ async def test_agent_loop_build_prompt_history_uses_ov_context_plus_unsynced_tai
ov_server={"server_url": "http://127.0.0.1:1933"},
agents={"session_context_enabled": True, "session_context_token_budget": 321},
)
- loop = AgentLoop(
- bus=bus,
- provider=_FakeProvider(),
- workspace=temp_dir / "workspace",
- config=config,
- )
+ loop = make_loop(bus=bus, config=config)
session_key = SessionKey(type="cli", channel_id="default", chat_id="session-ov-history")
session = loop.sessions.get_or_create(session_key, skip_heartbeat=True)
@@ -748,31 +530,18 @@ async def test_agent_loop_build_prompt_history_uses_ov_context_plus_unsynced_tai
@pytest.mark.asyncio
async def test_agent_loop_build_prompt_history_falls_back_to_loaded_local_history_when_ov_empty(
- temp_dir: Path, monkeypatch
+ make_loop, temp_dir: Path, monkeypatch
):
- monkeypatch.setattr(AgentLoop, "_register_builtin_hooks", lambda self: None)
- monkeypatch.setattr(AgentLoop, "_register_default_tools", lambda self: None)
- monkeypatch.setattr("vikingbot.agent.loop.SubagentManager", _FakeSubagentManager)
-
fake_ov_client = _FakeOVClient(context_payload={"messages": []})
- async def fake_get_ov_client(self, session_key, openviking_connection=None, actor_peer_id=None):
- del self, session_key, openviking_connection, actor_peer_id
- return fake_ov_client
-
- monkeypatch.setattr(AgentLoop, "_get_ov_client", fake_get_ov_client)
+ monkeypatch.setattr(AgentLoop, "_get_ov_client", AsyncMock(return_value=fake_ov_client))
config = Config(
storage_workspace=str(temp_dir),
ov_server={"server_url": "http://127.0.0.1:1933"},
agents={"session_context_enabled": True, "session_context_token_budget": 321},
)
- loop = AgentLoop(
- bus=MessageBus(),
- provider=_FakeProvider(),
- workspace=temp_dir / "workspace",
- config=config,
- )
+ loop = make_loop(config=config)
session_key = SessionKey(type="cli", channel_id="default", chat_id="session-local-fallback")
session = loop.sessions.get_or_create(session_key, skip_heartbeat=True)
@@ -800,21 +569,13 @@ async def test_agent_loop_build_prompt_history_falls_back_to_loaded_local_histor
@pytest.mark.asyncio
async def test_agent_loop_build_prompt_history_skips_tail_when_sync_cursor_is_past_local_messages(
- temp_dir: Path, monkeypatch
+ make_loop, temp_dir: Path, monkeypatch
):
- monkeypatch.setattr(AgentLoop, "_register_builtin_hooks", lambda self: None)
- monkeypatch.setattr(AgentLoop, "_register_default_tools", lambda self: None)
- monkeypatch.setattr("vikingbot.agent.loop.SubagentManager", _FakeSubagentManager)
-
fake_ov_client = _FakeOVClient(
context_payload={"messages": [{"role": "user", "content": "OV user turn"}]}
)
- async def fake_get_ov_client(self, session_key, openviking_connection=None, actor_peer_id=None):
- del self, session_key, openviking_connection, actor_peer_id
- return fake_ov_client
-
- monkeypatch.setattr(AgentLoop, "_get_ov_client", fake_get_ov_client)
+ monkeypatch.setattr(AgentLoop, "_get_ov_client", AsyncMock(return_value=fake_ov_client))
bus = MessageBus()
config = Config(
@@ -822,12 +583,7 @@ async def test_agent_loop_build_prompt_history_skips_tail_when_sync_cursor_is_pa
ov_server={"server_url": "http://127.0.0.1:1933"},
agents={"session_context_enabled": True, "session_context_token_budget": 321},
)
- loop = AgentLoop(
- bus=bus,
- provider=_FakeProvider(),
- workspace=temp_dir / "workspace",
- config=config,
- )
+ loop = make_loop(bus=bus, config=config)
session_key = SessionKey(type="cli", channel_id="default", chat_id="session-ov-cursor-past")
session = loop.sessions.get_or_create(session_key, skip_heartbeat=True)
@@ -845,12 +601,8 @@ async def test_agent_loop_build_prompt_history_skips_tail_when_sync_cursor_is_pa
@pytest.mark.asyncio
async def test_agent_loop_build_prompt_history_enforces_token_budget_for_live_tool_outputs(
- temp_dir: Path, monkeypatch
+ make_loop, temp_dir: Path, monkeypatch
):
- monkeypatch.setattr(AgentLoop, "_register_builtin_hooks", lambda self: None)
- monkeypatch.setattr(AgentLoop, "_register_default_tools", lambda self: None)
- monkeypatch.setattr("vikingbot.agent.loop.SubagentManager", _FakeSubagentManager)
-
tool_output = "x" * 10_000
fake_ov_client = _FakeOVClient(
context_payload={
@@ -871,21 +623,14 @@ async def test_agent_loop_build_prompt_history_enforces_token_budget_for_live_to
}
)
- async def fake_get_ov_client(self, session_key, openviking_connection=None, actor_peer_id=None):
- del self, session_key, openviking_connection, actor_peer_id
- return fake_ov_client
-
- monkeypatch.setattr(AgentLoop, "_get_ov_client", fake_get_ov_client)
+ monkeypatch.setattr(AgentLoop, "_get_ov_client", AsyncMock(return_value=fake_ov_client))
- loop = AgentLoop(
- bus=MessageBus(),
- provider=_FakeProvider(),
- workspace=temp_dir / "workspace",
+ loop = make_loop(
config=Config(
storage_workspace=str(temp_dir),
ov_server={"server_url": "http://127.0.0.1:1933"},
agents={"session_context_enabled": True, "session_context_token_budget": 3000},
- ),
+ )
)
session = loop.sessions.get_or_create(
SessionKey(type="cli", channel_id="default", chat_id="session-large-tools"),
@@ -907,12 +652,8 @@ async def test_agent_loop_build_prompt_history_enforces_token_budget_for_live_to
@pytest.mark.asyncio
async def test_agent_loop_build_prompt_history_preserves_anchor_when_final_needs_truncation(
- temp_dir: Path, monkeypatch
+ make_loop, temp_dir: Path, monkeypatch
):
- monkeypatch.setattr(AgentLoop, "_register_builtin_hooks", lambda self: None)
- monkeypatch.setattr(AgentLoop, "_register_default_tools", lambda self: None)
- monkeypatch.setattr("vikingbot.agent.loop.SubagentManager", _FakeSubagentManager)
-
fake_ov_client = _FakeOVClient(
context_payload={
"messages": [
@@ -922,21 +663,14 @@ async def test_agent_loop_build_prompt_history_preserves_anchor_when_final_needs
}
)
- async def fake_get_ov_client(self, session_key, openviking_connection=None, actor_peer_id=None):
- del self, session_key, openviking_connection, actor_peer_id
- return fake_ov_client
-
- monkeypatch.setattr(AgentLoop, "_get_ov_client", fake_get_ov_client)
+ monkeypatch.setattr(AgentLoop, "_get_ov_client", AsyncMock(return_value=fake_ov_client))
- loop = AgentLoop(
- bus=MessageBus(),
- provider=_FakeProvider(),
- workspace=temp_dir / "workspace",
+ loop = make_loop(
config=Config(
storage_workspace=str(temp_dir),
ov_server={"server_url": "http://127.0.0.1:1933"},
agents={"session_context_enabled": True, "session_context_token_budget": 3000},
- ),
+ )
)
session = loop.sessions.get_or_create(
SessionKey(type="cli", channel_id="default", chat_id="session-long-final"),
@@ -956,74 +690,13 @@ async def test_agent_loop_build_prompt_history_preserves_anchor_when_final_needs
assert sum(loop._history_message_tokens(message) for message in history) <= 3000
-def test_agent_loop_turn_aware_trim_reserves_space_for_short_final():
- history = [
- {"role": "user", "content": "u" * 20},
- {"role": "assistant", "content": "a"},
- ]
-
- trimmed = AgentLoop._trim_history_to_token_budget(history, 10)
-
- assert [message["role"] for message in trimmed] == ["user", "assistant"]
- assert trimmed[0]["content"]
- assert trimmed[1]["content"] == "a"
- assert sum(AgentLoop._history_message_tokens(message) for message in trimmed) <= 10
-
-
-async def test_agent_loop_submits_openviking_session_through_compact_hook(
- temp_dir: Path, monkeypatch
+ "token_threshold, pending_tokens, memory_window",
+ [pytest.param(100, 100, 50, id="token-budget"), pytest.param(1000, 0, 3, id="message-window")],
+)
+async def test_agent_loop_commits_before_model_at_context_limit(
+ make_loop, temp_dir: Path, monkeypatch, token_threshold, pending_tokens, memory_window
):
- monkeypatch.setattr(AgentLoop, "_register_builtin_hooks", lambda self: None)
- monkeypatch.setattr(AgentLoop, "_register_default_tools", lambda self: None)
- monkeypatch.setattr("vikingbot.agent.loop.SubagentManager", _FakeSubagentManager)
-
- calls = []
-
- async def fake_execute_hooks(context, **kwargs):
- calls.append((context, kwargs))
- session = kwargs["session"]
- session.metadata.setdefault("openviking", {})["last_sync_status"] = "success"
- return kwargs
-
- monkeypatch.setattr(loop_module.hook_manager, "execute_hooks", fake_execute_hooks)
-
- bus = MessageBus()
- config = Config(
- storage_workspace=str(temp_dir),
- ov_server={"server_url": "http://127.0.0.1:1933"},
- agents={"session_context_enabled": True},
- )
- loop = AgentLoop(
- bus=bus,
- provider=_FakeProvider(),
- workspace=temp_dir / "workspace",
- config=config,
- )
-
- session_key = SessionKey(type="cli", channel_id="default", chat_id="session-ov-sync")
- session = loop.sessions.get_or_create(session_key, skip_heartbeat=True)
- session.add_message("user", "Need syncing", sender_id="user-1")
- session.add_message("assistant", "Synced reply", sender_id="user-1")
-
- success = await loop._submit_openviking_session(session)
-
- assert success is True
- assert len(calls) == 1
- context, kwargs = calls[0]
- assert context.event_type == "message.compact"
- assert context.session_id == make_openviking_storage_session_id(session_key.safe_name())
- assert kwargs == {"session": session, "force_commit": False}
-
-
-async def test_agent_loop_commits_openviking_before_model_when_pending_tokens_reach_threshold(
- temp_dir: Path, monkeypatch
-):
- monkeypatch.setattr(AgentLoop, "_register_builtin_hooks", lambda self: None)
- monkeypatch.setattr(AgentLoop, "_register_default_tools", lambda self: None)
- monkeypatch.setattr("vikingbot.agent.loop.SubagentManager", _FakeSubagentManager)
-
events = []
async def fake_execute_hooks(context, **kwargs):
@@ -1031,7 +704,7 @@ async def test_agent_loop_commits_openviking_before_model_when_pending_tokens_re
(
"hook",
kwargs["force_commit"],
- kwargs.get("keep_recent_count"),
+ kwargs.get("keep_recent_turn_count"),
kwargs.get("commit_message_threshold"),
)
)
@@ -1040,6 +713,7 @@ async def test_agent_loop_commits_openviking_before_model_when_pending_tokens_re
state["last_sync_status"] = "success"
state["last_pending_tokens"] = 0
state["last_commit_performed"] = bool(kwargs["force_commit"])
+ state["last_commit_local_index"] = len(session.messages) - 1
if not kwargs["force_commit"]:
state["last_synced_local_index"] = len(session.messages) - 1
return kwargs
@@ -1077,16 +751,11 @@ async def test_agent_loop_commits_openviking_before_model_when_pending_tokens_re
agents={
"session_context_enabled": True,
"session_context_token_budget": 321,
- "commit_token_threshold": 100,
- "commit_keep_recent_count": 2,
+ "commit_token_threshold": token_threshold,
+ "commit_keep_recent_turn_count": 2,
},
)
- loop = AgentLoop(
- bus=bus,
- provider=_FakeProvider(),
- workspace=temp_dir / "workspace",
- config=config,
- )
+ loop = make_loop(bus=bus, config=config, memory_window=memory_window)
session_key = SessionKey(type="cli", channel_id="default", chat_id="session-precommit")
session = loop.sessions.get_or_create(session_key, skip_heartbeat=True)
@@ -1095,7 +764,8 @@ async def test_agent_loop_commits_openviking_before_model_when_pending_tokens_re
session.metadata["openviking"] = {
"session_id": session_key.safe_name(),
"last_synced_local_index": 1,
- "last_pending_tokens": 100,
+ "last_pending_tokens": pending_tokens,
+ "last_commit_local_index": -1,
}
await loop.sessions.save(session)
@@ -1114,125 +784,27 @@ async def test_agent_loop_commits_openviking_before_model_when_pending_tokens_re
assert events[1] == ("context", "cli__default__session-precommit", 321)
assert events[2][0] == "model"
assert events[-1] == ("hook", False, None, loop.memory_window)
- persisted_session = loop.sessions.get_or_create(session_key, skip_heartbeat=True)
- assert [message["content"] for message in persisted_session.messages] == [
- "new question",
- "final answer",
- ]
- assert persisted_session.metadata["openviking"]["last_synced_local_index"] == 1
-
-
-async def test_agent_loop_commits_openviking_before_model_when_memory_window_reached(
- temp_dir: Path, monkeypatch
-):
- monkeypatch.setattr(AgentLoop, "_register_builtin_hooks", lambda self: None)
- monkeypatch.setattr(AgentLoop, "_register_default_tools", lambda self: None)
- monkeypatch.setattr("vikingbot.agent.loop.SubagentManager", _FakeSubagentManager)
-
- events = []
-
- async def fake_execute_hooks(context, **kwargs):
- events.append(
- (
- "hook",
- kwargs["force_commit"],
- kwargs.get("keep_recent_count"),
- kwargs.get("commit_message_threshold"),
- )
- )
- session = kwargs["session"]
- session.metadata.setdefault("openviking", {})["last_sync_status"] = "success"
- session.metadata["openviking"]["last_pending_tokens"] = 0
- session.metadata["openviking"]["last_commit_local_index"] = len(session.messages) - 1
- session.metadata["openviking"]["last_commit_performed"] = bool(kwargs["force_commit"])
- return kwargs
-
- async def fake_get_ov_client(self, session_key, openviking_connection=None, actor_peer_id=None):
- del self, session_key, openviking_connection, actor_peer_id
-
- class _Client:
- async def get_session_context(self, session_id, token_budget):
- events.append(("context", session_id, token_budget))
- return {"messages": []}
-
- async def close(self):
- return None
-
- return _Client()
-
- async def fake_run_agent_loop(self, **kwargs):
- events.append(("model", [message.get("content") for message in kwargs["messages"]]))
- return "final answer", None, [], {"prompt_tokens": 1, "completion_tokens": 1}, 1
-
- fake_langfuse = _FakeLangfuseClient()
- monkeypatch.setattr(loop_module.hook_manager, "execute_hooks", fake_execute_hooks)
- monkeypatch.setattr(AgentLoop, "_get_ov_client", fake_get_ov_client)
- monkeypatch.setattr(AgentLoop, "_run_agent_loop", fake_run_agent_loop)
- monkeypatch.setattr(
- "vikingbot.agent.loop.LangfuseClient.get_instance",
- staticmethod(lambda: fake_langfuse),
- )
-
- bus = MessageBus()
- config = Config(
- storage_workspace=str(temp_dir),
- ov_server={"server_url": "http://127.0.0.1:1933"},
- agents={
- "session_context_enabled": True,
- "session_context_token_budget": 321,
- "commit_token_threshold": 1000,
- "commit_keep_recent_count": 2,
- },
- )
- loop = AgentLoop(
- bus=bus,
- provider=_FakeProvider(),
- workspace=temp_dir / "workspace",
- config=config,
- memory_window=3,
+ persisted_session = SessionManager(config.bot_data_path).get_or_create(
+ session_key, skip_heartbeat=True
)
-
- session_key = SessionKey(type="cli", channel_id="default", chat_id="session-window-precommit")
- session = loop.sessions.get_or_create(session_key, skip_heartbeat=True)
- session.add_message("user", "old user", sender_id="user-1")
- session.add_message("assistant", "old assistant", sender_id="user-1")
- session.metadata["openviking"] = {
- "session_id": session_key.safe_name(),
- "last_synced_local_index": 1,
- "last_pending_tokens": 0,
- "last_commit_local_index": -1,
- }
- await loop.sessions.save(session)
-
- response = await loop._process_message(
- InboundMessage(
- session_key=session_key,
- sender_id="user-1",
- content="new question",
- timestamp=datetime.fromisoformat("2026-04-30T00:05:00"),
- )
- )
-
- assert response is not None
- assert response.content == "final answer"
- assert events[0] == ("hook", True, 2, 3)
- assert events[-1] == ("hook", False, None, 3)
- persisted_session = loop.sessions.get_or_create(session_key, skip_heartbeat=True)
assert [message["content"] for message in persisted_session.messages] == [
"new question",
"final answer",
]
+ assert persisted_session.metadata["openviking"]["last_synced_local_index"] == 1
@pytest.mark.asyncio
-async def test_agent_loop_does_not_precommit_again_after_memory_window_commit(
- temp_dir: Path, monkeypatch
+ "last_commit_index, expected_commits",
+ [
+ pytest.param(-1, [True], id="uncommitted-history"),
+ pytest.param(1, [], id="already-committed"),
+ ],
+)
+async def test_agent_loop_precommit_counts_messages_since_last_commit(
+ make_loop, temp_dir: Path, monkeypatch, last_commit_index, expected_commits
):
- monkeypatch.setattr(AgentLoop, "_register_builtin_hooks", lambda self: None)
- monkeypatch.setattr(AgentLoop, "_register_default_tools", lambda self: None)
- monkeypatch.setattr("vikingbot.agent.loop.SubagentManager", _FakeSubagentManager)
-
calls = []
async def fake_execute_hooks(context, **kwargs):
@@ -1246,19 +818,14 @@ async def test_agent_loop_does_not_precommit_again_after_memory_window_commit(
bus = MessageBus()
config = Config(
storage_workspace=str(temp_dir),
+ ov_server={"server_url": "http://127.0.0.1:1933"},
agents={
"session_context_enabled": True,
"commit_token_threshold": 1000,
- "commit_keep_recent_count": 2,
+ "commit_keep_recent_turn_count": 2,
},
)
- loop = AgentLoop(
- bus=bus,
- provider=_FakeProvider(),
- workspace=temp_dir / "workspace",
- config=config,
- memory_window=3,
- )
+ loop = make_loop(bus=bus, config=config, memory_window=3)
session_key = SessionKey(type="cli", channel_id="default", chat_id="session-window-no-repeat")
session = loop.sessions.get_or_create(session_key, skip_heartbeat=True)
@@ -1267,7 +834,7 @@ async def test_agent_loop_does_not_precommit_again_after_memory_window_commit(
session.metadata["openviking"] = {
"session_id": session_key.safe_name(),
"last_pending_tokens": 0,
- "last_commit_local_index": 1,
+ "last_commit_local_index": last_commit_index,
}
await loop._maybe_commit_openviking_before_turn(
@@ -1280,70 +847,13 @@ async def test_agent_loop_does_not_precommit_again_after_memory_window_commit(
),
)
- assert calls == []
-
-
-async def test_agent_loop_post_turn_passes_memory_window_threshold(temp_dir: Path, monkeypatch):
- monkeypatch.setattr(AgentLoop, "_register_builtin_hooks", lambda self: None)
- monkeypatch.setattr(AgentLoop, "_register_default_tools", lambda self: None)
- monkeypatch.setattr("vikingbot.agent.loop.SubagentManager", _FakeSubagentManager)
-
- calls = []
-
- async def fake_execute_hooks(context, **kwargs):
- calls.append(kwargs)
- session = kwargs["session"]
- session.metadata.setdefault("openviking", {})["last_sync_status"] = "success"
- return kwargs
-
- async def fake_run_agent_loop(self, **kwargs):
- return "final answer", None, [], {"prompt_tokens": 1, "completion_tokens": 1}, 1
-
- fake_langfuse = _FakeLangfuseClient()
- monkeypatch.setattr(loop_module.hook_manager, "execute_hooks", fake_execute_hooks)
- monkeypatch.setattr(AgentLoop, "_run_agent_loop", fake_run_agent_loop)
- monkeypatch.setattr(
- "vikingbot.agent.loop.LangfuseClient.get_instance",
- staticmethod(lambda: fake_langfuse),
- )
-
- bus = MessageBus()
- config = Config(
- storage_workspace=str(temp_dir),
- ov_server={"server_url": "http://127.0.0.1:1933"},
- agents={"session_context_enabled": True},
- )
- loop = AgentLoop(
- bus=bus,
- provider=_FakeProvider(),
- workspace=temp_dir / "workspace",
- config=config,
- memory_window=3,
- )
-
- session_key = SessionKey(type="cli", channel_id="default", chat_id="session-post-window")
- await loop._process_message(
- InboundMessage(
- session_key=session_key,
- sender_id="user-1",
- content="new question",
- timestamp=datetime.fromisoformat("2026-04-30T00:05:00"),
- )
- )
-
- assert calls[-1]["force_commit"] is False
- assert calls[-1]["commit_message_threshold"] == 3
+ assert [call["force_commit"] for call in calls] == expected_commits
@pytest.mark.asyncio
async def test_agent_loop_post_turn_clears_local_session_after_openviking_commit(
- temp_dir: Path, monkeypatch
+ make_loop, temp_dir: Path, monkeypatch
):
- monkeypatch.setattr(AgentLoop, "_register_builtin_hooks", lambda self: None)
- monkeypatch.setattr(AgentLoop, "_register_default_tools", lambda self: None)
- monkeypatch.setattr("vikingbot.agent.loop.SubagentManager", _FakeSubagentManager)
-
calls = []
async def fake_execute_hooks(context, **kwargs):
@@ -1373,13 +883,7 @@ async def test_agent_loop_post_turn_clears_local_session_after_openviking_commit
ov_server={"server_url": "http://127.0.0.1:1933"},
agents={"session_context_enabled": True},
)
- loop = AgentLoop(
- bus=bus,
- provider=_FakeProvider(),
- workspace=temp_dir / "workspace",
- config=config,
- memory_window=3,
- )
+ loop = make_loop(bus=bus, config=config, memory_window=3)
session_key = SessionKey(type="cli", channel_id="default", chat_id="session-post-clear")
await loop._process_message(
@@ -1391,7 +895,10 @@ async def test_agent_loop_post_turn_clears_local_session_after_openviking_commit
)
)
- persisted_session = loop.sessions.get_or_create(session_key, skip_heartbeat=True)
+ persisted_session = SessionManager(config.bot_data_path).get_or_create(
+ session_key, skip_heartbeat=True
+ )
+ assert calls[-1]["force_commit"] is False
assert calls[-1]["commit_message_threshold"] == 3
assert persisted_session.messages == []
assert persisted_session.metadata["openviking"]["session_id"] == (
diff --git a/bot/tests/test_bot_model_timeout.py b/bot/tests/test_bot_model_timeout.py
new file mode 100644
index 000000000..51b7e6207
--- /dev/null
+++ b/bot/tests/test_bot_model_timeout.py
@@ -0,0 +1,61 @@
+import json
+from types import SimpleNamespace
+
+import pytest
+from vikingbot.cli.commands import _make_provider
+from vikingbot.config import loader
+
+
+def _chat_response(content: str = "ok"):
+ return SimpleNamespace(
+ choices=[
+ SimpleNamespace(
+ message=SimpleNamespace(content=content, tool_calls=None),
+ finish_reason="stop",
+ )
+ ],
+ usage=SimpleNamespace(prompt_tokens=1, completion_tokens=1, total_tokens=2),
+ )
+
+
+ "agent_options, vlm_options, expected_timeout",
+ [
+ pytest.param({}, {"timeout": 180.0}, 180.0, id="inherit"),
+ pytest.param({}, {}, 60.0, id="default"),
+ pytest.param({"timeout": 45.0}, {"timeout": 180.0}, 45.0, id="override"),
+ ],
+)
+async def test_configured_timeout_reaches_chat_request(
+ tmp_path, monkeypatch, agent_options, vlm_options, expected_timeout
+):
+ path = tmp_path / "ov.conf"
+ path.write_text(
+ json.dumps(
+ {
+ "vlm": {
+ "provider": "litellm",
+ "model": "openai/gpt-4o-mini",
+ "api_key": "test-key",
+ **vlm_options,
+ },
+ "bot": {"agents": agent_options},
+ }
+ )
+ )
+ monkeypatch.setattr(loader, "CONFIG_PATH", path)
+ monkeypatch.setenv("OPENVIKING_CONFIG_FILE", str(path))
+ captured = {}
+
+ async def fake_acompletion(**kwargs):
+ captured.update(kwargs)
+ return _chat_response()
+
+ monkeypatch.setattr("openviking.models.vlm.backends.litellm_vlm.acompletion", fake_acompletion)
+
+ provider = _make_provider(loader.load_config())
+ response = await provider.chat(messages=[{"role": "user", "content": "hi"}])
+
+ assert response.content == "ok"
+ assert captured["timeout"] == expected_timeout
diff --git a/bot/vikingbot/tests/unit/test_bot_provider_thinking.py b/bot/tests/test_bot_provider_thinking.py
similarity index 80%
rename from bot/vikingbot/tests/unit/test_bot_provider_thinking.py
rename to bot/tests/test_bot_provider_thinking.py
index e6dec10e8..6f0900fbf 100644
--- a/bot/vikingbot/tests/unit/test_bot_provider_thinking.py
+++ b/bot/tests/test_bot_provider_thinking.py
@@ -1,34 +1,11 @@
-import contextlib
-import sys
from types import SimpleNamespace
import pytest
+from vikingbot.providers.vlm_adapter import VLMProviderAdapter
from openviking.models.vlm.backends.litellm_vlm import (
LiteLLMVLMProvider as OpenVikingLiteLLMVLMProvider,
)
-from vikingbot.config.schema import AgentsConfig
-from vikingbot.providers.vlm_adapter import VLMProviderAdapter
-
-
-def test_agents_config_defaults_thinking_enabled():
- assert AgentsConfig().thinking is True
- assert AgentsConfig(thinking=False).thinking is False
-
-
-def test_agents_config_defaults_max_tokens_unset():
- assert AgentsConfig().max_tokens is None
- assert AgentsConfig(max_tokens=8192).max_tokens == 8192
-
-
-def test_agents_openviking_retention_defaults_to_turn_budget_values():
- config = AgentsConfig()
-
- assert config.commit_keep_recent_count == 10
- assert AgentsConfig(commit_keep_recent_count=7).commit_keep_recent_count == 7
- assert config.commit_keep_recent_turn_count == 3
- assert config.commit_retained_message_token_budget == 6_000
- assert config.commit_min_raw_tail_steps == 1
def test_vlm_adapter_exposes_only_native_tool_result_media_backends():
@@ -84,34 +61,6 @@ def test_vlm_adapter_uses_litellm_resolved_provider_for_tool_result_media():
def test_make_provider_passes_default_thinking_to_vlm_adapter(monkeypatch):
- monkeypatch.setitem(
- sys.modules,
- "prompt_toolkit",
- SimpleNamespace(PromptSession=object),
- )
- monkeypatch.setitem(
- sys.modules,
- "prompt_toolkit.formatted_text",
- SimpleNamespace(HTML=lambda value: value, FormattedText=lambda value: value),
- )
- monkeypatch.setitem(
- sys.modules,
- "prompt_toolkit.history",
- SimpleNamespace(FileHistory=object),
- )
- monkeypatch.setitem(
- sys.modules,
- "prompt_toolkit.patch_stdout",
- SimpleNamespace(patch_stdout=lambda: contextlib.nullcontext()),
- )
- monkeypatch.setitem(
- sys.modules,
- "prompt_toolkit.styles",
- SimpleNamespace(
- Style=SimpleNamespace(from_dict=lambda value: value),
- ),
- )
-
from vikingbot.cli.commands import _make_provider
captured = {}
diff --git a/bot/vikingbot/tests/unit/test_bot_vlm_credentials.py b/bot/tests/test_bot_vlm_credentials.py
similarity index 61%
rename from bot/vikingbot/tests/unit/test_bot_vlm_credentials.py
rename to bot/tests/test_bot_vlm_credentials.py
index d06e115c9..168f362fb 100644
--- a/bot/vikingbot/tests/unit/test_bot_vlm_credentials.py
+++ b/bot/tests/test_bot_vlm_credentials.py
@@ -4,11 +4,12 @@ from unittest.mock import AsyncMock
import pytest
from typer.testing import CliRunner
+from vikingbot.cli.commands import _make_provider
+from vikingbot.config import loader
+from vikingbot.providers.vlm_adapter import VLMProviderAdapter
from openviking.models.vlm import MultiCredentialVLM
from openviking.models.vlm.backends.litellm_vlm import LiteLLMVLMProvider
-from vikingbot.config import loader
-from vikingbot.providers.vlm_adapter import VLMProviderAdapter
def _write_config(tmp_path, monkeypatch, data):
@@ -19,40 +20,16 @@ def _write_config(tmp_path, monkeypatch, data):
return loader.load_config()
[email protected]("bot_override", [False, True])
[email protected]("legacy_providers", [{"openai": {"api_key": "obsolete-secret"}}, {}, None])
-def test_legacy_providers_do_not_change_model_selection_or_persist(
- tmp_path, monkeypatch, bot_override, legacy_providers
-):
- data = {
- "vlm": {"provider": "openai", "model": "root-model", "api_key": "root-secret"},
- "bot": {"providers": legacy_providers},
- }
- if bot_override:
- data["bot"]["agents"] = {
+def _credential_chain(prefix):
+ return [
+ {
+ "id": f"{prefix}-{name}",
"provider": "openai",
- "model": "bot-model",
- "api_key": "bot-secret",
+ "model": f"{prefix}-{name}",
+ "api_key": f"{prefix}-{name}-key",
}
- warnings = []
- monkeypatch.setattr(loader.logger, "warning", warnings.append)
-
- config = _write_config(tmp_path, monkeypatch, data)
- from vikingbot.cli.commands import _make_provider
-
- provider = _make_provider(config)
- assert provider._vlm.model == ("bot-model" if bot_override else "root-model")
- assert provider._vlm.api_key == ("bot-secret" if bot_override else "root-secret")
- assert any("bot.providers" in message and "ignored" in message for message in warnings)
- assert "obsolete-secret" not in " ".join(warnings)
- path = tmp_path / "ov.conf"
- assert json.loads(path.read_text()) == data # Loading never rewrites the user's file.
-
- loader.save_config(config, path, include_defaults=True)
- saved = json.loads(path.read_text())
- assert "providers" not in saved["bot"]
- assert saved["vlm"] == data["vlm"]
- assert loader.load_config().inherits_root_vlm() is (not bot_override)
+ for name in ("primary", "backup")
+ ]
@pytest.mark.parametrize("explicit_key", [None, "channel-secret", ""])
@@ -146,99 +123,6 @@ def test_status_shows_selected_model_config_without_exposing_secrets(
assert hidden not in result.output
-def test_status_handles_bot_credentials_without_parent_model(tmp_path, monkeypatch):
- from vikingbot.cli import commands
-
- _write_config(
- tmp_path,
- monkeypatch,
- {
- "bot": {
- "agents": {
- "credentials": [
- {"provider": "litellm", "model": "ollama/qwen3"},
- ]
- }
- }
- },
- )
-
- result = CliRunner().invoke(commands.app, ["status"])
-
- assert result.exit_code == 0, result.output
- assert "Model config: bot.agents" in result.output
- assert "Model: ollama/qwen3" in result.output
- assert "Provider: litellm" in result.output
- assert "API key: not set in config" in result.output
-
-
[email protected]("parent_provider", ["openai", "OpenAI", " OpenAI \t"])
[email protected]("credential_provider", [None, "openai"])
-def test_status_inherits_parent_key_only_for_the_normalized_parent_provider(
- tmp_path, monkeypatch, parent_provider, credential_provider
-):
- from vikingbot.cli import commands
-
- _write_config(
- tmp_path,
- monkeypatch,
- {
- "bot": {
- "agents": {
- "provider": parent_provider,
- "model": "gpt-4o",
- "api_key": "openai-secret",
- "credentials": [
- {"id": "primary", "provider": credential_provider},
- {
- "id": "native",
- "provider": "litellm",
- "model": "vertex_ai/gemini-2.5-pro",
- },
- ],
- }
- }
- },
- )
-
- def fail_create(*args, **kwargs):
- raise AssertionError("status must not initialize model backends")
-
- monkeypatch.setattr("openviking.models.vlm.base.VLMFactory.create", fail_create)
- result = CliRunner().invoke(commands.app, ["status"])
-
- assert result.exit_code == 0, result.output
- assert "1. Provider: openai | Model: gpt-4o" in result.output
- assert "2. Provider: litellm | Model: vertex_ai/gemini-2.5-pro" in result.output
- assert result.output.count("API key: configured") == 1
- assert "API key: not set in config" in result.output
- assert "openai-secret" not in result.output
-
-
-def test_status_keeps_root_vlm_providers_support(tmp_path, monkeypatch):
- from vikingbot.cli import commands
-
- _write_config(
- tmp_path,
- monkeypatch,
- {
- "vlm": {
- "model": "root-model",
- "default_provider": "openai",
- "providers": {"openai": {"api_key": "root-secret"}},
- }
- },
- )
-
- result = CliRunner().invoke(commands.app, ["status"])
-
- assert result.exit_code == 0, result.output
- assert "Model config: vlm (inherited)" in result.output
- assert "Provider: openai" in result.output
- assert "API key: configured" in result.output
- assert "root-secret" not in result.output
-
-
def test_bot_inherits_root_vlm_credentials_when_agents_model_is_omitted(tmp_path, monkeypatch):
config = _write_config(
tmp_path,
@@ -246,26 +130,11 @@ def test_bot_inherits_root_vlm_credentials_when_agents_model_is_omitted(tmp_path
{
"vlm": {
"model": "root-primary",
- "credentials": [
- {
- "id": "root-primary",
- "provider": "openai",
- "model": "root-primary",
- "api_key": "root-primary-key",
- },
- {
- "id": "root-backup",
- "provider": "openai",
- "model": "root-backup",
- "api_key": "root-backup-key",
- },
- ],
+ "credentials": _credential_chain("root"),
}
},
)
- from vikingbot.cli.commands import _make_provider
-
provider = _make_provider(config)
assert config.agents.inherits_root_vlm() is True
@@ -279,7 +148,11 @@ def test_bot_inherits_root_vlm_credentials_when_agents_model_is_omitted(tmp_path
]
-def test_agent_max_tokens_overrides_inherited_root_limit(tmp_path, monkeypatch):
[email protected]("bot_override", [False, True], ids=["inherited-model", "bot-model"])
+def test_agent_max_tokens_reaches_provider(tmp_path, monkeypatch, bot_override):
+ agents = {"max_tokens": 8192}
+ if bot_override:
+ agents.update(provider="openai", model="bot-model", api_key="bot-key")
config = _write_config(
tmp_path,
monkeypatch,
@@ -290,19 +163,11 @@ def test_agent_max_tokens_overrides_inherited_root_limit(tmp_path, monkeypatch):
"api_key": "root-key",
"max_tokens": 4096,
},
- "bot": {
- "agents": {
- "max_tokens": 8192,
- }
- },
+ "bot": {"agents": agents},
},
)
-
- from vikingbot.cli.commands import _make_provider
-
provider = _make_provider(config)
-
- assert config.agents.inherits_root_vlm() is True
+ assert config.agents.inherits_root_vlm() is (not bot_override)
assert provider._vlm.max_tokens == 8192
@@ -324,20 +189,7 @@ def test_explicit_bot_model_uses_bot_credentials_instead_of_root(tmp_path, monke
"bot": {
"agents": {
"model": "bot-primary",
- "credentials": [
- {
- "id": "bot-primary",
- "provider": "openai",
- "model": "bot-primary",
- "api_key": "bot-primary-key",
- },
- {
- "id": "bot-backup",
- "provider": "openai",
- "model": "bot-backup",
- "api_key": "bot-backup-key",
- },
- ],
+ "credentials": _credential_chain("bot"),
"failback_timeout_seconds": 30,
"failback_request_count": 5,
}
@@ -345,8 +197,6 @@ def test_explicit_bot_model_uses_bot_credentials_instead_of_root(tmp_path, monke
},
)
- from vikingbot.cli.commands import _make_provider
-
provider = _make_provider(config)
assert config.agents.inherits_root_vlm() is False
@@ -371,27 +221,12 @@ async def test_bot_multi_credentials_preserve_thinking_for_chat(tmp_path, monkey
"agents": {
"model": "bot-primary",
"thinking": True,
- "credentials": [
- {
- "id": "bot-primary",
- "provider": "openai",
- "model": "bot-primary",
- "api_key": "bot-primary-key",
- },
- {
- "id": "bot-backup",
- "provider": "openai",
- "model": "bot-backup",
- "api_key": "bot-backup-key",
- },
- ],
+ "credentials": _credential_chain("bot"),
}
}
},
)
- from vikingbot.cli.commands import _make_provider
-
provider = _make_provider(
config,
langfuse_client=SimpleNamespace(enabled=False, _client=None),
@@ -422,29 +257,10 @@ def test_bot_credentials_without_outer_model_use_bot_chain(tmp_path, monkeypatch
}
],
},
- "bot": {
- "agents": {
- "credentials": [
- {
- "id": "bot-primary",
- "provider": "openai",
- "model": "bot-primary",
- "api_key": "bot-primary-key",
- },
- {
- "id": "bot-backup",
- "provider": "openai",
- "model": "bot-backup",
- "api_key": "bot-backup-key",
- },
- ]
- }
- },
+ "bot": {"agents": {"credentials": _credential_chain("bot")}},
},
)
- from vikingbot.cli.commands import _make_provider
-
provider = _make_provider(config)
assert config.agents.inherits_root_vlm() is False
@@ -477,8 +293,6 @@ def test_explicit_bot_model_without_credentials_keeps_single_model_behavior(tmp_
},
)
- from vikingbot.cli.commands import _make_provider
-
provider = _make_provider(config)
assert config.agents.inherits_root_vlm() is False
@@ -487,30 +301,6 @@ def test_explicit_bot_model_without_credentials_keeps_single_model_behavior(tmp_
assert provider._vlm.model == "bot-model"
-def test_explicit_bot_model_passes_agent_max_tokens(tmp_path, monkeypatch):
- config = _write_config(
- tmp_path,
- monkeypatch,
- {
- "bot": {
- "agents": {
- "provider": "openai",
- "model": "bot-model",
- "api_key": "bot-key",
- "max_tokens": 8192,
- }
- }
- },
- )
-
- from vikingbot.cli.commands import _make_provider
-
- provider = _make_provider(config)
-
- assert config.agents.max_tokens == 8192
- assert provider._vlm.max_tokens == 8192
-
-
def test_bot_credentials_override_or_inherit_agent_max_tokens(tmp_path, monkeypatch):
config = _write_config(
tmp_path,
@@ -539,8 +329,6 @@ def test_bot_credentials_override_or_inherit_agent_max_tokens(tmp_path, monkeypa
},
)
- from vikingbot.cli.commands import _make_provider
-
provider = _make_provider(config)
assert isinstance(provider._vlm, MultiCredentialVLM)
@@ -562,8 +350,6 @@ def test_explicit_litellm_provider_uses_vlm_adapter(tmp_path, monkeypatch):
},
)
- from vikingbot.cli.commands import _make_provider
-
provider = _make_provider(config)
assert isinstance(provider, VLMProviderAdapter)
@@ -585,8 +371,6 @@ def test_bot_model_without_provider_rejects_legacy_fallback(tmp_path, monkeypatc
},
)
- from vikingbot.cli.commands import _make_provider
-
with pytest.raises(RuntimeError, match="Set provider to 'litellm'"):
_make_provider(config)
diff --git a/bot/vikingbot/tests/unit/test_bot_vlm_streaming.py b/bot/tests/test_bot_vlm_streaming.py
similarity index 100%
rename from bot/vikingbot/tests/unit/test_bot_vlm_streaming.py
rename to bot/tests/test_bot_vlm_streaming.py
index 70697d30e..ba0a46250 100644
--- a/bot/vikingbot/tests/unit/test_bot_vlm_streaming.py
+++ b/bot/tests/test_bot_vlm_streaming.py
@@ -1,9 +1,9 @@
from types import SimpleNamespace
import pytest
+from vikingbot.providers.vlm_adapter import VLMProviderAdapter
from openviking.models.vlm import FailoverVLM, MultiCredentialVLM, OpenAIVLM, VolcEngineVLM
-from vikingbot.providers.vlm_adapter import VLMProviderAdapter
class FakeCompletions:
diff --git a/bot/tests/test_channel_delivery_metadata.py b/bot/tests/test_channel_delivery_metadata.py
index c9e3cfdaa..48deb4b42 100644
--- a/bot/tests/test_channel_delivery_metadata.py
+++ b/bot/tests/test_channel_delivery_metadata.py
@@ -2,25 +2,20 @@
# SPDX-License-Identifier: AGPL-3.0
"""Regression tests for preserving channel delivery metadata."""
-import sys
-from pathlib import Path
from types import SimpleNamespace
import pytest
-
-sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
-
-from vikingbot.agent.loop import AgentLoop # noqa: E402
-from vikingbot.agent.subagent import SubagentManager # noqa: E402
-from vikingbot.agent.tools.cron import CronTool # noqa: E402
-from vikingbot.agent.tools.message import MessageTool # noqa: E402
-from vikingbot.agent.tools.spawn import SpawnTool # noqa: E402
-from vikingbot.bus.events import InboundMessage, OutboundMessage # noqa: E402
-from vikingbot.bus.queue import MessageBus # noqa: E402
-from vikingbot.channels.feishu import FeishuChannel # noqa: E402
-from vikingbot.config.schema import Config, FeishuChannelConfig, SessionKey # noqa: E402
-from vikingbot.cron.service import CronService # noqa: E402
-from vikingbot.cron.types import CronSchedule # noqa: E402
+from vikingbot.agent.loop import AgentLoop
+from vikingbot.agent.subagent import SubagentManager
+from vikingbot.agent.tools.cron import CronTool
+from vikingbot.agent.tools.message import MessageTool
+from vikingbot.agent.tools.spawn import SpawnTool
+from vikingbot.bus.events import InboundMessage, OutboundMessage
+from vikingbot.bus.queue import MessageBus
+from vikingbot.channels.feishu import FeishuChannel
+from vikingbot.config.schema import Config, FeishuChannelConfig, SessionKey
+from vikingbot.cron.service import CronService
+from vikingbot.cron.types import CronSchedule
@pytest.mark.asyncio
@@ -41,7 +36,6 @@ async def test_message_tool_preserves_channel_metadata():
assert result.startswith("Message sent")
assert sent[0].metadata == metadata
- assert sent[0].metadata is not metadata
@pytest.mark.asyncio
@@ -95,7 +89,6 @@ async def test_subagent_announcement_preserves_channel_metadata(tmp_path):
inbound = await bus.consume_inbound()
assert inbound.metadata == metadata
- assert inbound.metadata is not metadata
assert inbound.openviking_connection is connection
@@ -152,7 +145,6 @@ async def test_system_message_response_preserves_channel_metadata(tmp_path, monk
assert outbound.content == "summary"
assert outbound.metadata == metadata
- assert outbound.metadata is not metadata
assert captured["connection"] is connection
@@ -192,19 +184,6 @@ async def test_cron_tool_persists_only_delivery_metadata(tmp_path):
}
-def test_cron_service_accepts_missing_channel_metadata(tmp_path):
- service = CronService(tmp_path / "jobs.json")
- job = service.add_job(
- name="cli-job",
- schedule=CronSchedule(kind="every", every_ms=3600),
- message="hello",
- session_key=SessionKey(type="cli", channel_id="default", chat_id="default"),
- deliver=True,
- )
-
- assert job.payload.channel_metadata == {}
-
-
@pytest.mark.parametrize(
"schedule",
[
@@ -302,109 +281,36 @@ def test_feishu_uses_thread_root_for_scheduled_delivery():
)
-async def test_feishu_send_skips_normal_message_without_reply_to():
- channel = FeishuChannel(FeishuChannelConfig(app_id="cli_app"), MessageBus())
- channel._client = object()
-
- await channel.send(
- OutboundMessage(
- session_key=SessionKey(type="feishu", channel_id="cli_app", chat_id="oc_chat"),
- content="hello",
- )
- )
-
[email protected]("retry", [False, True], ids=["jpeg-upload", "normalize-and-retry"])
+async def test_feishu_uploads_jpeg(monkeypatch, retry):
+ import httpx
-async def test_feishu_upload_image_uses_detected_jpeg_format(monkeypatch):
channel = FeishuChannel(FeishuChannelConfig(app_id="cli_app"), MessageBus())
- jpeg_bytes = b"\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01fake-jpeg"
- captured = {}
-
- async def fake_token():
- return "tenant-token"
-
- class FakeResponse:
- is_error = False
- status_code = 200
- text = ""
-
- def raise_for_status(self):
- return None
-
- def json(self):
- return {"code": 0, "data": {"image_key": "img_key"}}
-
- class FakeClient:
- def __init__(self, timeout):
- self.timeout = timeout
-
- async def __aenter__(self):
- return self
-
- async def __aexit__(self, exc_type, exc, tb):
- return None
-
- async def post(self, url, headers, data, files):
- captured.update(url=url, headers=headers, data=data, files=files)
- return FakeResponse()
-
- monkeypatch.setattr(channel, "_get_tenant_access_token", fake_token)
- monkeypatch.setattr("vikingbot.channels.feishu.httpx.AsyncClient", FakeClient)
-
- image_key = await channel._upload_image_to_feishu(jpeg_bytes)
-
- filename, file_obj, mime_type = captured["files"]["image"]
- assert image_key == "img_key"
- assert filename == "image.jpg"
- assert mime_type == "image/jpeg"
- assert file_obj.read() == jpeg_bytes
-
-
-async def test_feishu_upload_retries_with_normalized_image_after_bad_request(monkeypatch):
- channel = FeishuChannel(FeishuChannelConfig(app_id="cli_app"), MessageBus())
- original_bytes = b"\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01original-with-metadata"
- normalized_bytes = b"\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01normalized"
+ original = b"\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01original-with-metadata"
+ normalized = b"\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01normalized"
uploads = []
- async def fake_token():
- return "tenant-token"
+ def handle(request):
+ uploads.append(request.content)
+ assert b'filename="image.jpg"' in request.content
+ assert b"image/jpeg" in request.content
+ if retry and len(uploads) == 1:
+ return httpx.Response(400, json={"code": 234011, "msg": "can not recognize image"})
+ return httpx.Response(200, json={"code": 0, "data": {"image_key": "img_key"}})
- class FakeResponse:
- def __init__(self, status_code, body):
- self.status_code = status_code
- self.text = body
- self.is_error = status_code >= 400
-
- def json(self):
- return {"code": 0, "data": {"image_key": "img_key"}}
-
- class FakeClient:
- def __init__(self, timeout):
- self.timeout = timeout
-
- async def __aenter__(self):
- return self
-
- async def __aexit__(self, exc_type, exc, tb):
- return None
-
- async def post(self, url, headers, data, files):
- filename, file_obj, mime_type = files["image"]
- uploads.append((filename, mime_type, file_obj.read()))
- if len(uploads) == 1:
- return FakeResponse(400, '{"code":234011,"msg":"can not recognize image"}')
- return FakeResponse(200, '{"code":0}')
-
- monkeypatch.setattr(channel, "_get_tenant_access_token", fake_token)
- monkeypatch.setattr(channel, "_normalize_image_for_feishu", lambda data: normalized_bytes)
- monkeypatch.setattr("vikingbot.channels.feishu.httpx.AsyncClient", FakeClient)
-
- image_key = await channel._upload_image_to_feishu(original_bytes)
+ async def token():
+ return "tenant-token"
- assert image_key == "img_key"
- assert uploads == [
- ("image.jpg", "image/jpeg", original_bytes),
- ("image.jpg", "image/jpeg", normalized_bytes),
- ]
+ real_client = httpx.AsyncClient
+ transport = httpx.MockTransport(handle)
+ monkeypatch.setattr(channel, "_get_tenant_access_token", token)
+ monkeypatch.setattr(channel, "_normalize_image_for_feishu", lambda data: normalized)
+ monkeypatch.setattr(
+ "vikingbot.channels.feishu.httpx.AsyncClient",
+ lambda **kwargs: real_client(transport=transport, **kwargs),
+ )
+ assert await channel._upload_image_to_feishu(original) == "img_key"
+ assert original in uploads[0]
+ assert len(uploads) == (2 if retry else 1)
+ if retry:
+ assert normalized in uploads[1]
diff --git a/bot/tests/test_channel_sender_name.py b/bot/tests/test_channel_sender_name.py
deleted file mode 100644
index 2d944a057..000000000
--- a/bot/tests/test_channel_sender_name.py
+++ /dev/null
@@ -1,34 +0,0 @@
-# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.
-# SPDX-License-Identifier: AGPL-3.0
-"""Regression tests for channel sender names."""
-
-import json
-import sys
-from pathlib import Path
-
-import pytest
-
-sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
-
-from vikingbot.bus.queue import MessageBus # noqa: E402
-from vikingbot.channels.whatsapp import WhatsAppChannel # noqa: E402
-from vikingbot.config.schema import WhatsAppChannelConfig # noqa: E402
-
-
-async def test_whatsapp_sender_name_falls_back_to_sender_id():
- bus = MessageBus()
- channel = WhatsAppChannel(WhatsAppChannelConfig(), bus)
-
- await channel._handle_bridge_message(
- json.dumps(
- {
- "type": "message",
- "sender": "[email protected]",
- "content": "hello",
- }
- )
- )
-
- inbound = await bus.consume_inbound()
- assert inbound.sender_name == "123456789"
diff --git a/bot/tests/test_chat_functionality.py b/bot/tests/test_chat_functionality.py
index e60ab55bf..0d715928e 100644
--- a/bot/tests/test_chat_functionality.py
+++ b/bot/tests/test_chat_functionality.py
@@ -1,176 +1,54 @@
# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.
# SPDX-License-Identifier: AGPL-3.0
-"""Tests for vikingbot chat functionality - single message and interactive modes."""
-
-import tempfile
-from pathlib import Path
+"""CLI response delivery and provider-compatible conversation history."""
import pytest
from vikingbot.bus.events import OutboundMessage
from vikingbot.bus.queue import MessageBus
from vikingbot.channels.chat import ChatChannel, ChatChannelConfig
from vikingbot.channels.single_turn import SingleTurnChannel, SingleTurnChannelConfig
-from vikingbot.cli.commands import _CHAT_PROMPT_MESSAGE, _CHAT_PROMPT_STYLE
from vikingbot.config.schema import SessionKey
from vikingbot.session.manager import Session
-def temp_workspace():
- """Create a temporary workspace directory."""
- with tempfile.TemporaryDirectory() as tmpdir:
- yield Path(tmpdir)
-
-
-def message_bus():
- """Create a MessageBus instance."""
- return MessageBus()
-
-
-class TestSingleTurnChannel:
- """Tests for SingleTurnChannel (vikingbot chat -m xxx)."""
-
- def test_single_turn_channel_initialization(self, message_bus, temp_workspace):
- """Test that SingleTurnChannel can be initialized correctly."""
- config = SingleTurnChannelConfig()
- channel = SingleTurnChannel(
- config,
- message_bus,
- workspace_path=temp_workspace,
- message="Hello, test",
- session_id="test-session",
- markdown=True,
- )
-
- assert channel is not None
- assert channel.name == "single_turn"
- assert channel.message == "Hello, test"
- assert channel.session_id == "test-session"
-
- @pytest.mark.asyncio
- async def test_single_turn_channel_receives_response(self, message_bus, temp_workspace):
- """Test that SingleTurnChannel can receive and store responses."""
- config = SingleTurnChannelConfig()
- test_message = "Hello, test"
- channel = SingleTurnChannel(
- config,
- message_bus,
- workspace_path=temp_workspace,
- message=test_message,
- session_id="test-session",
- markdown=True,
- )
-
- # Create a test response
- session_key = SessionKey(type="cli", channel_id="default", chat_id="test-session")
- test_response = "This is a test response from the bot"
-
- # Send the response
- await channel.send(
- OutboundMessage(
- session_key=session_key,
- content=test_response,
- )
- )
-
- # Check that the response was stored
- assert channel._last_response == test_response
- assert channel._response_received.is_set()
-
-
-class TestSessionHistoryProviderSpecificFields:
- """Tests provider-specific history reconstruction from persisted session messages."""
-
- def test_deepseek_history_includes_reasoning_content(self):
- session = Session(key=SessionKey(type="cli", channel_id="default", chat_id="test-session"))
- session.add_message("user", "hello")
- session.add_message(
- "assistant",
- "hi",
- reasoning_content="internal reasoning",
- )
-
- history = session.get_history(provider_name="deepseek")
-
- assert history == [
- {"role": "user", "content": "hello"},
- {
- "role": "assistant",
- "content": "hi",
- "reasoning_content": "internal reasoning",
- },
- ]
-
- def test_non_deepseek_history_omits_reasoning_content(self):
- session = Session(key=SessionKey(type="cli", channel_id="default", chat_id="test-session"))
- session.add_message("user", "hello")
- session.add_message(
- "assistant",
- "hi",
- reasoning_content="internal reasoning",
- )
-
- history = session.get_history(provider_name="openai")
-
- assert history == [
- {"role": "user", "content": "hello"},
- {"role": "assistant", "content": "hi"},
- ]
-
-
-class TestChatChannel:
- def test_interactive_prompt_uses_terminal_foreground_and_visible_label(self):
- assert _CHAT_PROMPT_MESSAGE == [("class:user-label", "You: ")]
- rules = dict(_CHAT_PROMPT_STYLE.style_rules)
- assert rules[""] == "fg:ansidefault"
- assert rules["user-label"] == "bold fg:ansicyan"
- assert all("ansiblack" not in style for style in rules.values())
-
- def test_chat_channel_initialization(self, message_bus, temp_workspace):
- """Test that ChatChannel can be initialized correctly."""
- config = ChatChannelConfig()
- channel = ChatChannel(
- config,
- message_bus,
- workspace_path=temp_workspace,
- session_id="test-session",
- markdown=True,
- logs=False,
- )
-
- assert channel is not None
- assert channel.name == "chat"
- assert channel.session_id == "test-session"
-
- @pytest.mark.asyncio
- async def test_chat_channel_send_response(self, message_bus, temp_workspace):
- """Test that ChatChannel can receive and store responses."""
- config = ChatChannelConfig()
- channel = ChatChannel(
- config,
- message_bus,
- workspace_path=temp_workspace,
- session_id="test-session",
- markdown=True,
- logs=False,
- )
-
- # Start the channel in background (it will wait for input)
- channel._running = True
-
- # Create a test response
- session_key = SessionKey(type="cli", channel_id="default", chat_id="test-session")
- test_response = "This is a test response from the bot"
-
- # Send the response
- await channel.send(
- OutboundMessage(
- session_key=session_key,
- content=test_response,
- )
- )
-
- # Check that the response was stored
- assert channel._last_response == test_response
- assert channel._response_received.is_set()
+ "channel_cls, config_cls, options",
+ [
+ pytest.param(
+ SingleTurnChannel, SingleTurnChannelConfig, {"message": "hello"}, id="single-turn"
+ ),
+ pytest.param(ChatChannel, ChatChannelConfig, {"logs": False}, id="interactive"),
+ ],
+)
+async def test_cli_channel_receives_response(tmp_path, channel_cls, config_cls, options):
+ channel = channel_cls(
+ config_cls(),
+ MessageBus(),
+ workspace_path=tmp_path,
+ session_id="test-session",
+ markdown=True,
+ **options,
+ )
+ channel._running = True
+ await channel.send(
+ OutboundMessage(
+ session_key=SessionKey(type="cli", channel_id="default", chat_id="test-session"),
+ content="test response",
+ )
+ )
+ assert channel._last_response == "test response"
+ assert channel._response_received.is_set()
+
+
[email protected]("provider", ["deepseek", "openai"])
+def test_history_includes_reasoning_only_for_deepseek(provider):
+ session = Session(key=SessionKey(type="cli", channel_id="default", chat_id="test-session"))
+ session.add_message("user", "hello")
+ session.add_message("assistant", "hi", reasoning_content="internal reasoning")
+ assistant = {"role": "assistant", "content": "hi"}
+ if provider == "deepseek":
+ assistant["reasoning_content"] = "internal reasoning"
+ assert session.get_history(provider_name=provider) == [
+ {"role": "user", "content": "hello"},
+ assistant,
+ ]
diff --git a/bot/tests/test_compile.py b/bot/tests/test_compile.py
index 9c571662a..8666a8843 100644
--- a/bot/tests/test_compile.py
+++ b/bot/tests/test_compile.py
@@ -9,7 +9,6 @@ import pytest
from vikingbot.agent.loop import (
AgentIterationLimitExceeded,
AgentLoop,
- render_budget_reminder,
)
from vikingbot.agent.tools.base import MultimodalToolResult, Tool, ToolContext
from vikingbot.agent.tools.compile import (
@@ -20,7 +19,6 @@ from vikingbot.agent.tools.compile import (
from vikingbot.agent.tools.ov_file import (
VikingExportTool,
VikingMultiReadTool,
- local_path_for_viking_uri,
)
from vikingbot.agent.tools.registry import ToolRegistry
from vikingbot.compile.models import (
@@ -41,28 +39,17 @@ from vikingbot.compile.readlist import (
)
from vikingbot.compile.renderer import (
WikiRenderer,
- wiki_page_path_from_title,
)
from vikingbot.compile.service import BotCompileService, CompileCapabilities
from vikingbot.compile.store import CompileTaskStore
from vikingbot.config.schema import (
DirectBackendConfig,
SandboxBackend,
- SandboxConfig,
- SandboxMode,
SessionKey,
)
-from vikingbot.sandbox import SandboxManager
-from vikingbot.sandbox.backends.srt import SrtBackend
from vikingbot.sandbox.base import SandboxFileInfo
-from vikingbot.utils.session_paths import portable_path_component
-from openviking.core.skill_loader import SkillLoader
from openviking.session.memory.utils.memory_file_utils import MemoryFileUtils
-from openviking.session.memory.utils.resource_refs import (
- content_references_resource,
- unlink_resource_references_from_memory,
-)
from openviking_cli.exceptions import OpenVikingError
@@ -80,177 +67,6 @@ def _page(page_id: int, title: str, **overrides):
return value
-def test_skill_loader_distinguishes_missing_and_explicit_empty_allowed_tools():
- missing = SkillLoader.parse("---\nname: a\ndescription: A\n---\nDo it")
- empty = SkillLoader.parse("---\nname: a\ndescription: A\nallowed-tools: []\n---\nDo it")
- assert missing["allowed_tools_declared"] is False
- assert empty["allowed_tools_declared"] is True
- assert "allowed-tools: ''" in SkillLoader.to_skill_md(empty)
-
-
-def test_skill_loader_accepts_standard_and_legacy_allowed_tools_forms():
- standard = SkillLoader.parse(
- "---\nname: a\ndescription: A\nallowed-tools: Read Write Bash(git:*)\n---\nDo it"
- )
- ara = SkillLoader.parse(
- "---\nname: a\ndescription: A\n"
- "allowed-tools: Read, Write, Bash(python *|git clone *), Glob\n---\nDo it"
- )
- legacy = SkillLoader.parse(
- "---\nname: a\ndescription: A\nallowed-tools: [Read, Write]\n---\nDo it"
- )
-
- assert standard["allowed_tools"] == ["Read", "Write", "Bash(git:*)"]
- assert ara["allowed_tools"] == [
- "Read",
- "Write",
- "Bash(python *|git clone *)",
- "Glob",
- ]
- assert legacy["allowed_tools"] == ["Read", "Write"]
-
-
-def test_skill_loader_rejects_invalid_allowed_tools():
- with pytest.raises(ValueError, match="space-separated string or an array of strings"):
- SkillLoader.parse("---\nname: a\ndescription: A\nallowed-tools: [Read, 3]\n---\nDo it")
- with pytest.raises(ValueError, match="unbalanced parentheses"):
- SkillLoader.parse(
- "---\nname: a\ndescription: A\nallowed-tools: Read Bash(git:*\n---\nDo it"
- )
-
-
-def test_compile_bundle_schema_distinguishes_wiki_pages_and_artifact_files():
- schema = WikiBundleDraft.model_json_schema()
- properties = schema["properties"]
- page_properties = schema["$defs"]["WikiPageDraft"]["properties"]
-
- assert "Actual Wiki pages only" in properties["pages"]["description"]
- assert "Markdown, YAML, JSON" in properties["files"]["description"]
- assert "generated Wiki pages only" in properties["links"]["description"]
- assert "known source URIs" in page_properties["body_markdown"]["description"]
- assert (
- "editable UTF-8 Markdown Wiki page" in page_properties["body_workspace_path"]["description"]
- )
- assert (
- "__compile_staging__/target_checkout/"
- in page_properties["body_workspace_path"]["description"]
- )
- assert "filename derives from title" in page_properties["path_hint"]["description"]
- assert "supplied source roots" in page_properties["source_ids"]["description"]
- assert "preserve every required path and format" in properties["files"]["description"]
-
-
-def test_compile_limit_defaults_match_the_resource_envelope():
- limits = CompileLimits()
-
- assert limits.concurrent_tasks == 10
- assert limits.accepted_tasks == 40
- assert limits.accepted_tasks_per_principal == 10
- assert limits.agent_iterations == 60
- assert limits.source_files == 5000
- assert limits.source_total_bytes == 1024 * 1024 * 1024
- assert limits.target_total_bytes == 1024 * 1024 * 1024
- assert limits.salvage_grace_seconds == 120
- assert limits.cleanup_grace_seconds == 40
- assert limits.target_inventory_entries == 2000
- assert limits.target_catalog_pages == 10
- assert limits.output_pages == 128
- assert limits.output_files == 128
- assert limits.output_operations == 256
- assert DirectBackendConfig().allow_compile_exec is True
-
-def test_wiki_page_requires_exactly_one_body_source():
- body = _page(1, "One")
- body.pop("body_markdown")
-
- with pytest.raises(ValueError, match="exactly one of body_markdown"):
- WikiBundleDraft.model_validate({"pages": [body]})
- with pytest.raises(ValueError, match="exactly one of body_markdown"):
- WikiBundleDraft.model_validate(
- {
- "pages": [
- {
- **body,
- "body_markdown": "Inline",
- "body_workspace_path": "pages/one.md",
- }
- ]
- }
- )
-
-
-def test_submit_tool_rejects_raw_payload_wrapper_with_actionable_hint():
- tool = SubmitWikiBundleTool(
- source_ids={"src_1"},
- catalog_uris=set(),
- target_uri="viking://resources/wiki",
- limits=CompileLimits(),
- )
-
- assert tool.validate_params({"raw": "{}"}) == [
- "use the tool schema directly; do not wrap the payload in a JSON string"
- ]
- assert {"pages", "files"} <= set(tool.parameters["required"])
- assert "missing required files" in tool.validate_params({"pages": []})
- assert tool.validate_params({"pages": [], "files": []}) == []
-
-
-def test_submit_tool_schema_requires_workspace_page_bodies_when_available():
- tool = SubmitWikiBundleTool(
- source_ids={"src_1"},
- catalog_uris=set(),
- target_uri="viking://resources/wiki",
- limits=CompileLimits(),
- require_workspace_pages=True,
- )
-
- page_schema = tool.parameters["$defs"]["WikiPageDraft"]
- assert "body_markdown" not in page_schema["properties"]
- assert "body_workspace_path" in page_schema["required"]
-
-
-def test_submit_tool_checkout_schema_takes_no_file_manifest():
- tool = SubmitTargetCheckoutTool(
- target_uri="viking://resources/wiki",
- source_roots={"src_1": "viking://resources/source"},
- limits=CompileLimits(),
- )
-
- assert tool.parameters == {
- "type": "object",
- "properties": {},
- "additionalProperties": False,
- }
- assert "Pass no pages, files, paths, or content" in tool.description
-
-
- "target_uri",
- ["viking://resources/wiki", "viking://agent/skills"],
-)
[email protected]("exec_enabled", [True, False])
-def test_submit_tool_schema_requires_workspace_artifacts_when_available(target_uri, exec_enabled):
- tool = SubmitWikiBundleTool(
- source_ids={"src_1"},
- catalog_uris=set(),
- target_uri=target_uri,
- limits=CompileLimits(),
- require_workspace_files=True,
- exec_enabled=exec_enabled,
- )
-
- file_schema = tool.parameters["$defs"]["CompileFileDraft"]
- assert "content" not in file_schema["properties"]
- assert "workspace_path" in file_schema["required"]
- hint = tool.validate_params({"raw": "{}"})[0]
- assert ("write_file or exec" in tool.description) is exec_enabled
- assert ("write_file or exec" in hint) is exec_enabled
- assert "workspace_path instead of inline content" in hint
- if not exec_enabled:
- assert "with write_file," in tool.description
- assert "with write_file and submit" in hint
-
-
@pytest.mark.asyncio
async def test_submit_tool_accepts_only_one_complete_skill_package():
tool = SubmitWikiBundleTool(
@@ -260,13 +76,6 @@ async def test_submit_tool_accepts_only_one_complete_skill_package():
limits=CompileLimits(),
)
- schema = tool.parameters
- assert set(schema["properties"]) == {"files"}
- assert schema["required"] == ["files"]
- assert set(schema["$defs"]) == {"CompileFileDraft"}
- assert "update_uri" not in schema["$defs"]["CompileFileDraft"]["properties"]
- assert "path" in schema["$defs"]["CompileFileDraft"]["required"]
-
accepted = await tool.execute(
ToolContext(),
files=[
@@ -383,104 +192,6 @@ def test_renderer_creates_okf_pages_links_and_source_fallbacks():
assert "- [source](viking://resources/source)" in first["content"]
[email protected]("name", ["a#one.md", "a%23one.md"])
[email protected]("inline", [True, False])
[email protected]("scope", ["resources", "user/alice/memories"])
-def test_renderer_encodes_literal_source_filenames(name, inline, scope):
- source = f"viking://resources/source#1/{name}"
- bundle = WikiBundleDraft.model_validate(
- {"pages": [_page(1, "Overview", body_markdown=f"Source: {source}" if inline else "Body")]}
- )
- rendered = WikiRenderer().render(
- bundle=bundle,
- target_uri=f"viking://{scope}/wiki",
- source_roots={"src_1": source},
- catalog_uris=set(),
- existing_raw={},
- )
- encoded = source.replace("%", "%25").replace("#", "%23")
- assert rendered.operations[0]["content"].count(f"]({encoded})") == 1
- if scope != "resources":
- mf = MemoryFileUtils.read(rendered.operations[0]["content"])
- assert mf.extra_fields["resource_refs"][0]["resource_uri"] == source
- assert content_references_resource(mf.content, source)
- assert unlink_resource_references_from_memory(mf, source)
- assert "resource_refs" not in mf.extra_fields
- assert not content_references_resource(mf.content, source)
-
-
-def test_renderer_preserves_existing_link_without_adding_another_mention_or_backlink():
- bundle = WikiBundleDraft.model_validate(
- {
- "pages": [
- _page(
- 1,
- "Overview",
- body_markdown=(
- 'Read [Details](./details.md "details") next. Details appears again.'
- ),
- ),
- _page(2, "Details"),
- ],
- }
- )
-
- rendered = WikiRenderer().render(
- bundle=bundle,
- target_uri="viking://resources/wiki",
- source_roots={"src_1": "viking://resources/source"},
- catalog_uris=set(),
- existing_raw={},
- )
- operations = {operation["uri"]: operation["content"] for operation in rendered.operations}
-
- assert (
- operations["viking://resources/wiki/overview.md"].count('[Details](./details.md "details")')
- == 1
- )
- assert "Details appears again" in operations["viking://resources/wiki/overview.md"]
- assert "Related pages" not in operations["viking://resources/wiki/details.md"]
- assert rendered.link_count == 0
-
-
-def test_renderer_links_first_filename_mention_in_body_but_not_page_title():
- bundle = WikiBundleDraft.model_validate(
- {
- "pages": [
- _page(
- 1,
- "Beta Overview",
- path_hint="entity/beta-overview.md",
- body_markdown="# Beta Overview\n\nBeta appears first. Beta appears again.",
- ),
- _page(
- 2,
- "Beta",
- path_hint="concept/beta.md",
- body_markdown="# Beta\n\nDefinition.",
- ),
- ]
- }
- )
-
- rendered = WikiRenderer().render(
- bundle=bundle,
- target_uri="viking://resources/wiki",
- source_roots={"src_1": "viking://resources/source"},
- catalog_uris=set(),
- existing_raw={},
- )
- content = {operation["uri"]: operation["content"] for operation in rendered.operations}[
- "viking://resources/wiki/entity/beta-overview.md"
- ]
-
- assert "# Beta Overview" in content
- assert "# [Beta]" not in content
- assert content.count("](../concept/beta.md)") == 1
- assert "[Beta](../concept/beta.md) appears first. Beta appears again." in content
- assert rendered.link_count == 1
-
-
def test_renderer_auto_links_existing_target_pages_when_a_new_page_is_added():
alpha_uri = "viking://resources/wiki/entity/alpha.md"
old_alpha = (
@@ -520,137 +231,6 @@ def test_renderer_auto_links_existing_target_pages_when_a_new_page_is_added():
assert "Related pages" not in operations[alpha_uri]["content"]
-def test_renderer_skips_ambiguous_duplicate_filenames_across_directories():
- existing_raw = {
- "viking://resources/wiki/entity/topic.md": (
- "---\ntype: entity\ntitle: Entity Topic\ndescription: Entity\n---\n\n"
- "# Entity Topic\n\nEntity body.\n"
- ),
- "viking://resources/wiki/concept/topic.md": (
- "---\ntype: concept\ntitle: Concept Topic\ndescription: Concept\n---\n\n"
- "# Concept Topic\n\nConcept body.\n"
- ),
- }
- bundle = WikiBundleDraft.model_validate(
- {"pages": [_page(1, "Overview", body_markdown="Topic is intentionally ambiguous.")]}
- )
-
- rendered = WikiRenderer().render(
- bundle=bundle,
- target_uri="viking://resources/wiki",
- source_roots={"src_1": "viking://resources/source"},
- catalog_uris=set(),
- existing_raw=existing_raw,
- )
- overview = next(
- operation["content"]
- for operation in rendered.operations
- if operation["uri"] == "viking://resources/wiki/overview.md"
- )
-
- assert "Topic is intentionally ambiguous." in overview
- assert "[Topic]" not in overview
- assert rendered.updated == []
-
-
-def test_wiki_page_title_path_normalizes_spaced_dashes_only():
- assert (
- wiki_page_path_from_title("Experimental Designs - Residual Networks")
- == "Experimental_Designs_Residual_Networks"
- )
- assert wiki_page_path_from_title("One-Page Overview") == "One-Page_Overview"
-
-
-def test_renderer_linkifies_source_uris_without_repeating_them():
- source_detail = "viking://resources/source/chapter_1.md"
- outside = "viking://resources/outside/chapter.md"
- bundle = WikiBundleDraft.model_validate(
- {
- "pages": [
- _page(
- 1,
- "Overview",
- body_markdown=(
- "Read Details next.\n\n"
- f"Source: {source_detail} section 2\n\n"
- f"Keep code unchanged: `{source_detail}`\n\n"
- f"Outside stays plain: {outside}"
- ),
- ),
- _page(2, "Details"),
- ],
- "links": [{"f": 1, "t": 2, "match_text": "Details"}],
- }
- )
-
- rendered = WikiRenderer().render(
- bundle=bundle,
- target_uri="viking://resources/wiki",
- source_roots={"src_1": "viking://resources/source"},
- catalog_uris=set(),
- existing_raw={},
- )
- operations = {operation["uri"]: operation["content"] for operation in rendered.operations}
- overview = operations["viking://resources/wiki/overview.md"]
- details = operations["viking://resources/wiki/details.md"]
-
- assert "[Details](./details.md)" in overview
- assert f"[chapter_1]({source_detail})" in overview
- assert f"`{source_detail}`" in overview
- assert outside in overview and f"]({outside})" not in overview
- assert overview.count(f"]({source_detail})") == 1
- assert "[source](viking://resources/source)" not in overview
- assert "# Citations" not in overview
- assert "## Related pages" not in details
- assert "- [Overview](./overview.md)" not in details
- assert "## Sources" in details
- assert rendered.link_count == 1
-
-
-def test_renderer_adds_raw_text_and_workspace_binary_to_same_bundle():
- paper = "---\ntitle: ARA Demo\nauthors: [Ada]\n---\n\n# Layer Index\n"
- image_bytes = b"\x89PNG\r\n\x1a\nfigure"
- bundle = WikiBundleDraft.model_validate(
- {
- "pages": [_page(1, "Overview")],
- "files": [
- {"path": "PAPER.md", "content": paper},
- {
- "path": "trace/exploration_tree.yaml",
- "content": "nodes: []\n",
- },
- {
- "path": "evidence/figures/figure1.png",
- "workspace_path": "ara-output/figure1.png",
- },
- ],
- }
- )
- rendered = WikiRenderer().render(
- bundle=bundle,
- target_uri="viking://resources/wiki",
- source_roots={"src_1": "viking://resources/source"},
- catalog_uris=set(),
- existing_raw={},
- file_payloads=[None, None, image_bytes],
- )
- operations = {operation["uri"]: operation for operation in rendered.operations}
- assert operations["viking://resources/wiki/PAPER.md"]["content"] == paper
- assert (
- operations["viking://resources/wiki/trace/exploration_tree.yaml"]["content"]
- == "nodes: []\n"
- )
- encoded = operations["viking://resources/wiki/evidence/figures/figure1.png"]["content_base64"]
- assert base64.b64decode(encoded) == image_bytes
- assert rendered.created == [
- "viking://resources/wiki/overview.md",
- "viking://resources/wiki/PAPER.md",
- "viking://resources/wiki/trace/exploration_tree.yaml",
- "viking://resources/wiki/evidence/figures/figure1.png",
- ]
- assert rendered.wiki_uris == ["viking://resources/wiki/overview.md"]
-
-
@pytest.mark.asyncio
async def test_compile_rejects_combined_output_operation_overflow():
limits = CompileLimits(output_pages=2, output_files=2, output_operations=3)
@@ -682,354 +262,68 @@ async def test_compile_rejects_combined_output_operation_overflow():
)
-def test_renderer_accepts_minimal_okf_artifact_with_unknown_fields():
- content = (
- "---\n"
- "type: research_artifact\n"
- "ara_version: 1.0\n"
- "custom: anything\n"
- "---\n\n"
- "# Research artifact\n"
- )
- bundle = WikiBundleDraft.model_validate(
- {"pages": [], "files": [{"path": "research.md", "content": content}]}
- )
-
- rendered = WikiRenderer().render(
- bundle=bundle,
- target_uri="viking://resources/wiki",
- source_roots={},
- catalog_uris=set(),
- existing_raw={},
- )
-
- assert rendered.operations[0]["content"] == content
- assert rendered.wiki_uris == ["viking://resources/wiki/research.md"]
-
-
@pytest.mark.parametrize(
- ("content", "message"),
+ "instruction, provided, model_reply, expected_language, input_kind, selected_text, omitted_text",
[
- ("---\ntype:\n---\n", 'field "type" must be a non-empty string'),
- ("---\ntype: [concept]\n---\n", 'field "type" must be a non-empty string'),
- ("---\ntype: concept\nauthors: [\n---\n", "invalid YAML frontmatter"),
- ("---\ntype: concept\n# no closing delimiter", "unterminated YAML frontmatter"),
+ pytest.param(
+ "请用中文输出",
+ True,
+ "zh-CN",
+ "zh-CN",
+ "user_instruction",
+ "请用中文输出",
+ "source sample",
+ id="explicit-language",
+ ),
+ pytest.param(
+ DEFAULT_COMPILE_INSTRUCTION,
+ False,
+ "ja",
+ "en",
+ "source_content",
+ "source sample",
+ DEFAULT_COMPILE_INSTRUCTION,
+ id="source-language",
+ ),
],
)
-def test_renderer_rejects_invalid_declared_okf_artifact(content, message):
- bundle = WikiBundleDraft.model_validate(
- {"pages": [], "files": [{"path": "invalid.md", "content": content}]}
- )
+async def test_wiki_language_classifier_selects_input(
+ instruction, provided, model_reply, expected_language, input_kind, selected_text, omitted_text
+):
+ from unittest.mock import AsyncMock
- with pytest.raises(ValueError, match=message):
- WikiRenderer().render(
- bundle=bundle,
- target_uri="viking://resources/wiki",
- source_roots={},
- catalog_uris=set(),
- existing_raw={},
+ provider = SimpleNamespace(
+ chat=AsyncMock(
+ return_value=SimpleNamespace(
+ content=model_reply,
+ usage={"prompt_tokens": 12, "completion_tokens": 1},
+ )
)
-
-
-def test_renderer_checks_size_before_parsing_okf_artifact():
- content = "---\ntype: [broken\n---\n"
- bundle = WikiBundleDraft.model_validate(
- {"pages": [], "files": [{"path": "large.md", "content": content}]}
)
-
- with pytest.raises(ValueError, match="final content size limit"):
- WikiRenderer(CompileLimits(output_total_bytes=8)).render(
- bundle=bundle,
- target_uri="viking://resources/wiki",
- source_roots={},
- catalog_uris=set(),
- existing_raw={},
- )
-
-
-def test_renderer_rejects_page_path_colliding_with_artifact():
- bundle = WikiBundleDraft.model_validate({"pages": [_page(1, "PAPER", path_hint="PAPER.md")]})
-
- with pytest.raises(ValueError, match="already exists"):
- WikiRenderer().render(
- bundle=bundle,
- target_uri="viking://resources/wiki",
- source_roots={"src_1": "viking://resources/source"},
- catalog_uris=set(),
- file_catalog_uris={"viking://resources/wiki/PAPER.md"},
- existing_raw={},
- )
-
-
-def test_renderer_raw_update_cannot_remove_okf_from_existing_wiki_page():
- uri = "viking://resources/wiki/existing.md"
- bundle = WikiBundleDraft.model_validate(
- {"pages": [], "files": [{"update_uri": uri, "content": "# Plain Markdown"}]}
+ service = object.__new__(BotCompileService)
+ service.agent_loop = SimpleNamespace(provider=provider, model="test-model")
+ request = SanitizedCompileRequest.model_validate(
+ {
+ "from": ["viking://resources/source"],
+ "to": "viking://resources/wiki",
+ "skill": "viking://agent/skills/wiki",
+ "instruction": instruction,
+ "instruction_provided": provided,
+ }
)
-
- with pytest.raises(ValueError, match="must retain valid OKF"):
- WikiRenderer().render(
- bundle=bundle,
- target_uri="viking://resources/wiki",
- source_roots={},
- catalog_uris={uri},
- file_catalog_uris={uri},
- existing_raw={},
- existing_bytes={uri: b"---\ntype: concept\n---\n\n# Existing"},
- )
-
-
-def test_renderer_raw_file_update_uses_upsert_and_detects_unchanged():
- uri = "viking://resources/wiki/trace/exploration_tree.yaml"
- old = b"nodes: []\n"
- unchanged_bundle = WikiBundleDraft.model_validate(
- {"pages": [], "files": [{"update_uri": uri, "content": old.decode()}]}
- )
- renderer = WikiRenderer()
- unchanged = renderer.render(
- bundle=unchanged_bundle,
- target_uri="viking://resources/wiki",
- source_roots={},
- catalog_uris=set(),
- existing_raw={},
- file_catalog_uris={uri},
- existing_bytes={uri: old},
- )
- assert unchanged.unchanged == [uri]
- assert unchanged.operations == []
-
- changed_bundle = WikiBundleDraft.model_validate(
- {"pages": [], "files": [{"update_uri": uri, "content": "nodes: [root]\n"}]}
- )
- changed = renderer.render(
- bundle=changed_bundle,
- target_uri="viking://resources/wiki",
- source_roots={},
- catalog_uris=set(),
- existing_raw={},
- file_catalog_uris={uri},
- existing_bytes={uri: old},
- )
- assert changed.updated == [uri]
- assert changed.operations[0]["mode"] == "upsert"
-
-
-def test_renderer_empty_existing_update_uses_upsert_and_preserves_uri():
- uri = "viking://resources/wiki/empty.md"
- bundle = WikiBundleDraft.model_validate(
- {
- "pages": [
- _page(1, "Empty", path_hint=None, update_uri=uri),
- ]
- }
- )
- rendered = WikiRenderer().render(
- bundle=bundle,
- target_uri="viking://resources/wiki",
- source_roots={"src_1": "viking://resources/source"},
- catalog_uris={uri},
- existing_raw={uri: ""},
- )
- assert rendered.updated == [uri]
- assert rendered.created == []
- assert rendered.operations[0]["mode"] == "upsert"
-
-
-def test_renderer_preserves_unknown_frontmatter_and_skill_owned_citations():
- uri = "viking://resources/wiki/topic.md"
- old = """---
-type: legacy
-title: Old
-description: Old summary
-custom: keep-me
----
-
-Old body
-
-# Citations
-
-[1] [Detail](viking://resources/source/detail.md)
-[2] [Outside](viking://resources/other/no.md)
-"""
- bundle = WikiBundleDraft.model_validate(
- {
- "pages": [
- _page(
- 1,
- "Topic",
- update_uri=uri,
- path_hint=None,
- tags=[" stable ", "stable", "new"],
- body_markdown=(
- "New body\n\n# Citations\n\n"
- "[9] [Detail](viking://resources/source/detail.md)"
- ),
- )
- ]
- }
- )
- rendered = WikiRenderer().render(
- bundle=bundle,
- target_uri="viking://resources/wiki",
- source_roots={"src_1": "viking://resources/source"},
- catalog_uris={uri},
- existing_raw={uri: old},
- )
- content = rendered.operations[0]["content"]
- assert "custom: keep-me" in content
- assert "tags: [stable, new]" in content
- assert content.count("viking://resources/source/detail.md") == 1
- assert "viking://resources/other/no.md" not in content
- assert "[source](viking://resources/source)" not in content
-
-
-def test_renderer_does_not_repeat_inline_source_links():
- bundle = WikiBundleDraft.model_validate(
- {
- "pages": [
- _page(
- 1,
- "主题",
- body_markdown=("事实来自[原始资料](viking://resources/source/detail.md)。"),
- )
- ]
- }
- )
- rendered = WikiRenderer().render(
- bundle=bundle,
- target_uri="viking://resources/wiki",
- source_roots={"src_1": "viking://resources/source"},
- catalog_uris=set(),
- existing_raw={},
- )
- content = rendered.operations[0]["content"]
- assert content.count("viking://resources/source/detail.md") == 1
- assert "## 参考来源" not in content
- assert "# Citations" not in content
-
-
-def test_renderer_uses_chinese_source_heading_from_compile_language():
- bundle = WikiBundleDraft.model_validate(
- {"pages": [_page(1, "主题", body_markdown="这是没有显式来源链接的正文。")]}
- )
- rendered = WikiRenderer().render(
- bundle=bundle,
- target_uri="viking://resources/wiki",
- source_roots={"src_1": "viking://resources/source"},
- catalog_uris=set(),
- existing_raw={},
- wiki_language="zh-CN",
- )
- content = rendered.operations[0]["content"]
- assert "## 来源" in content
- assert "# Citations" not in content
-
-
-def test_renderer_uses_compile_language_for_sources_without_related_pages_section():
- bundle = WikiBundleDraft.model_validate(
- {
- "pages": [
- _page(1, "入口", body_markdown="参见主题。"),
- _page(2, "主题", body_markdown="English body retained from an older page."),
- ],
- "links": [{"f": 1, "t": 2, "match_text": "主题"}],
- }
- )
- rendered = WikiRenderer().render(
- bundle=bundle,
- target_uri="viking://resources/wiki",
- source_roots={"src_1": "viking://resources/source"},
- catalog_uris=set(),
- existing_raw={},
- wiki_language="zh-CN",
- )
- content = rendered.operations[1]["content"]
- assert "## 来源" in content
- assert "## 相关页面" not in content
- assert "## Related pages" not in content
- assert "## Sources" not in content
-
-
-async def test_wiki_language_classifier_uses_real_instruction_and_one_model_call():
- class Provider:
- def __init__(self):
- self.calls = []
-
- async def chat(self, **kwargs):
- self.calls.append(kwargs)
- return SimpleNamespace(
- content="zh-CN",
- usage={"prompt_tokens": 12, "completion_tokens": 1},
- )
-
- provider = Provider()
- service = object.__new__(BotCompileService)
- service.agent_loop = SimpleNamespace(provider=provider, model="test-model")
- request = SanitizedCompileRequest.model_validate(
- {
- "from": ["viking://resources/source"],
- "to": "viking://resources/wiki",
- "skill": "viking://agent/skills/wiki",
- "instruction": "请用中文输出",
- "instruction_provided": True,
- }
- )
-
- language, usage = await service._classify_wiki_language(
- request=request,
- sources=[{"overview": "source overview"}],
- source_sample="source sample",
- session_key=_FakeCompileSessionKey(),
- )
-
- assert language == "zh-CN"
- assert usage == {"prompt_tokens": 12, "completion_tokens": 1}
- assert len(provider.calls) == 1
- call = provider.calls[0]
- assert call["max_tokens"] == 64
- assert call["temperature"] == 0.0
- assert call["session_id"] == "cmp:wiki-language"
- assert "input_kind=user_instruction" in call["messages"][1]["content"]
- assert "请用中文输出" in call["messages"][1]["content"]
- assert "source sample" not in call["messages"][1]["content"]
-
-
-async def test_wiki_language_classifier_ignores_default_instruction_and_defaults_non_chinese_to_en():
- class Provider:
- def __init__(self):
- self.message = ""
-
- async def chat(self, **kwargs):
- self.message = kwargs["messages"][1]["content"]
- return SimpleNamespace(content="ja", usage={})
-
- provider = Provider()
- service = object.__new__(BotCompileService)
- service.agent_loop = SimpleNamespace(provider=provider, model="test-model")
- request = SanitizedCompileRequest.model_validate(
- {
- "from": ["viking://resources/source"],
- "to": "viking://resources/wiki",
- "skill": "viking://agent/skills/wiki",
- "instruction": DEFAULT_COMPILE_INSTRUCTION,
- "instruction_provided": False,
- }
- )
-
- language, usage = await service._classify_wiki_language(
- request=request,
- sources=[{"overview": "fallback source overview"}],
- source_sample="这是实际的资源文本。",
- session_key=_FakeCompileSessionKey(),
- )
-
- assert language == "en"
- assert usage == {}
- assert "input_kind=source_content" in provider.message
- assert "这是实际的资源文本。" in provider.message
- assert DEFAULT_COMPILE_INSTRUCTION not in provider.message
+ language, usage = await service._classify_wiki_language(
+ request=request,
+ sources=[{"overview": "source overview"}],
+ source_sample="source sample",
+ session_key=_FakeCompileSessionKey(),
+ )
+ assert language == expected_language
+ assert usage == {"prompt_tokens": 12, "completion_tokens": 1}
+ provider.chat.assert_awaited_once()
+ content = provider.chat.await_args.kwargs["messages"][1]["content"]
+ assert f"input_kind={input_kind}" in content
+ assert selected_text in content
+ assert omitted_text not in content
def test_memory_renderer_round_trips_fields_and_only_bumps_changed_version():
@@ -1087,226 +381,7 @@ def test_memory_renderer_round_trips_fields_and_only_bumps_changed_version():
@pytest.mark.asyncio
-async def test_submit_tool_rejects_protected_anchor_and_path_collision():
- tool = SubmitWikiBundleTool(
- source_ids={"src_1"},
- catalog_uris=set(),
- file_catalog_uris={"viking://resources/wiki/existing.md"},
- target_uri="viking://resources/wiki",
- limits=CompileLimits(),
- )
- context = ToolContext()
- collision = await tool.execute(
- context,
- pages=[_page(1, "Existing", path_hint="existing.md")],
- )
- assert collision.startswith("Error:")
- protected = await tool.execute(
- context,
- pages=[
- _page(1, "One", body_markdown="`Two`"),
- _page(2, "Two"),
- ],
- links=[{"f": 1, "t": 2, "match_text": "Two"}],
- )
- assert protected.startswith("Wiki bundle accepted")
- assert "was not found and the link was dropped" in protected
- assert tool.bundle is not None and tool.bundle.links == []
-
- accepted = await tool.execute(context, pages=[], links=[])
- assert not accepted.startswith("Error:")
- assert tool.bundle is not None and tool.bundle.pages == []
-
-
-async def test_submit_tool_accepts_existing_link_only_when_target_matches():
- tool = SubmitWikiBundleTool(
- source_ids={"src_1"},
- catalog_uris=set(),
- target_uri="viking://resources/wiki",
- limits=CompileLimits(),
- )
- context = ToolContext()
-
- accepted = await tool.execute(
- context,
- pages=[
- _page(1, "One", body_markdown="参见 [L2 行为标签库](./two.md)。"),
- _page(2, "Two"),
- ],
- links=[{"f": 1, "t": 2, "match_text": "行为标签库"}],
- )
- assert accepted.startswith("Wiki bundle accepted")
- assert tool.bundle is not None and len(tool.bundle.links) == 1
-
- accepted = await tool.execute(
- context,
- pages=[
- _page(1, "One", body_markdown="See [Version](./foo(1).md)."),
- _page(2, "Version", path_hint="foo(1).md"),
- ],
- links=[{"f": 1, "t": 2, "match_text": "Version"}],
- )
- assert accepted.startswith("Wiki bundle accepted")
- assert tool.bundle is not None and len(tool.bundle.links) == 1
-
- rejected = await tool.execute(
- context,
- pages=[
- _page(1, "One", body_markdown="参见 [行为标签库](./three.md)。"),
- _page(2, "Two"),
- _page(3, "Three"),
- ],
- links=[{"f": 1, "t": 2, "match_text": "行为标签库"}],
- )
- assert rejected.startswith("Wiki bundle accepted")
- assert "was not found and the link was dropped" in rejected
- assert tool.bundle is not None and tool.bundle.links == []
-
-
-async def test_submit_tool_checks_size_before_parsing_okf_artifact():
- tool = SubmitWikiBundleTool(
- source_ids={"src_1"},
- catalog_uris=set(),
- target_uri="viking://resources/wiki",
- limits=CompileLimits(output_total_bytes=8),
- )
-
- result = await tool.execute(
- ToolContext(),
- pages=[],
- files=[
- {
- "path": "large.md",
- "content": "---\ntype: [broken\n---\n",
- }
- ],
- )
-
- assert result.startswith("Error: Invalid Wiki bundle:")
- assert "draft content size limit exceeded" in result
-
-
-async def test_submit_tool_raw_update_cannot_remove_okf_from_existing_wiki_page():
- uri = "viking://resources/wiki/existing.md"
- tool = SubmitWikiBundleTool(
- source_ids={"src_1"},
- catalog_uris={uri},
- file_catalog_uris={uri},
- target_uri="viking://resources/wiki",
- limits=CompileLimits(),
- )
-
- result = await tool.execute(
- ToolContext(),
- pages=[],
- files=[{"update_uri": uri, "content": "# Plain Markdown"}],
- )
-
- assert result.startswith("Error: Invalid Wiki bundle:")
- assert "must retain valid OKF frontmatter" in result
-
-
-async def test_submit_tool_resolves_existing_updates_outside_relevant_catalog():
- wiki_uri = "viking://resources/wiki/existing.md"
- artifact_uri = "viking://resources/wiki/PAPER.md"
- resolved = []
-
- async def resolve(uri):
- resolved.append(uri)
- return uri == wiki_uri
-
- catalog_uris = set()
- tool = SubmitWikiBundleTool(
- source_ids={"src_1"},
- catalog_uris=catalog_uris,
- file_catalog_uris={wiki_uri, artifact_uri},
- target_uri="viking://resources/wiki",
- limits=CompileLimits(),
- wiki_uri_resolver=resolve,
- )
-
- accepted = await tool.execute(
- ToolContext(),
- pages=[_page(1, "Existing", update_uri=wiki_uri, path_hint=None)],
- )
- assert accepted.startswith("Wiki bundle accepted")
- assert wiki_uri in catalog_uris
-
- rejected = await tool.execute(
- ToolContext(),
- pages=[],
- files=[{"update_uri": wiki_uri, "content": "# Plain Markdown"}],
- )
- assert "must retain valid OKF frontmatter" in rejected
-
- artifact = await tool.execute(
- ToolContext(),
- pages=[],
- files=[{"update_uri": artifact_uri, "content": "# Paper"}],
- )
- assert artifact.startswith("Wiki bundle accepted")
- assert resolved == [wiki_uri, artifact_uri]
-
-
-async def test_submit_tool_reports_structurally_invalid_links():
- tool = SubmitWikiBundleTool(
- source_ids={"src_1"},
- catalog_uris=set(),
- target_uri="viking://resources/wiki",
- limits=CompileLimits(),
- )
-
- result = await tool.execute(
- ToolContext(),
- pages=[_page(1, "One"), _page(2, "Two")],
- links=[
- {"f": 1, "t": 2, "match_text": "Two"},
- {"f": 1, "t": 1, "match_text": "One"},
- {"f": 1, "t": 3, "match_text": "Three"},
- ],
- )
-
- assert result.startswith("Error: Invalid Wiki bundle: 2 invalid link(s):")
- assert "links[1] must not be a self-link" in result
- assert "links[2] endpoints must reference bundle pages" in result
- assert tool.bundle is None
-
-
-async def test_submit_tool_drops_unrenderable_links_instead_of_failing():
- tool = SubmitWikiBundleTool(
- source_ids={"src_1"},
- catalog_uris=set(),
- target_uri="viking://resources/wiki",
- limits=CompileLimits(),
- )
-
- result = await tool.execute(
- ToolContext(),
- pages=[
- _page(1, "One", body_markdown="First page body"),
- _page(2, "Two"),
- _page(3, "Three"),
- ],
- links=[
- {"f": 1, "t": 2, "match_text": "Missing One"},
- {"f": 1, "t": 3, "match_text": "Missing Two"},
- ],
- )
-
- assert result.startswith("Wiki bundle accepted with 3 page(s)")
- assert "was not found and the link was dropped" in result
- assert tool.bundle is not None
- assert tool.bundle.links == []
-
-
-async def test_submit_tool_accepts_inline_content_by_default():
+async def test_submit_tool_drops_unrenderable_links_instead_of_failing():
tool = SubmitWikiBundleTool(
source_ids={"src_1"},
catalog_uris=set(),
@@ -1314,23 +389,23 @@ async def test_submit_tool_accepts_inline_content_by_default():
limits=CompileLimits(),
)
- page_schema = tool.parameters["$defs"]["WikiPageDraft"]
- file_schema = tool.parameters["$defs"]["CompileFileDraft"]
- assert "body_markdown" in page_schema["properties"]
- assert "body_workspace_path" in page_schema["properties"]
- assert "content" in file_schema["properties"]
- assert "workspace_path" in file_schema["properties"]
-
result = await tool.execute(
ToolContext(),
- pages=[_page(1, "Home")],
- files=[{"path": "extra/notes.yaml", "content": "kind: notes\n"}],
+ pages=[
+ _page(1, "One", body_markdown="First page body"),
+ _page(2, "Two"),
+ _page(3, "Three"),
+ ],
+ links=[
+ {"f": 1, "t": 2, "match_text": "Missing One"},
+ {"f": 1, "t": 3, "match_text": "Missing Two"},
+ ],
)
- assert result == "Wiki bundle accepted with 1 page(s) and 1 file(s)."
+ assert result.startswith("Wiki bundle accepted with 3 page(s)")
+ assert "was not found and the link was dropped" in result
assert tool.bundle is not None
- assert tool.bundle.pages[0].body_markdown == "Body for Home"
- assert tool.bundle.files[0].content == "kind: notes\n"
+ assert tool.bundle.links == []
@pytest.mark.asyncio
@@ -1410,37 +485,6 @@ async def test_multi_read_returns_images_as_tool_result_content(monkeypatch):
}
-async def test_multi_read_treats_typescript_as_text_and_preserves_window(monkeypatch):
- typescript_uri = "viking://resources/web/index.ts"
-
- class FakeClient:
- async def read_content(self, uri, level="abstract", offset=0, limit=-1, **kwargs):
- assert uri == typescript_uri
- assert level == "read"
- assert (offset, limit) == (4, 2)
- return "export const answer = 42;"
-
- async def download_bytes(self, uri):
- raise AssertionError("TypeScript reads must not use the media download path")
-
- tool = VikingMultiReadTool()
-
- async def fake_get_client(ctx):
- return FakeClient()
-
- async def no_release(ctx, client):
- return None
-
- monkeypatch.setattr(tool, "_get_client", fake_get_client)
- monkeypatch.setattr(tool, "_release_client", no_release)
-
- result = await tool.execute(ToolContext(), uris=[typescript_uri], offset=4, limit=2)
-
- assert isinstance(result, str)
- assert "export const answer = 42;" in result
-
-
@pytest.mark.asyncio
async def test_multi_read_limits_aggregate_inline_image_bytes(monkeypatch):
first_uri = "viking://resources/first.png"
@@ -1842,7 +886,8 @@ async def test_compact_tool_loop_incremental_merges_previous_note():
@pytest.mark.asyncio
async def test_compact_tool_loop_chunks_large_transcript_and_merges():
- loop = _compact_loop(_FakeChatProvider("SEGMENT"))
+ provider = _FakeChatProvider("SEGMENT")
+ loop = _compact_loop(provider)
# Build enough history that the transcript spans multiple summarization chunks
# (each chunk is capped at 32k chars; each message at 6k).
@@ -1856,6 +901,7 @@ async def test_compact_tool_loop_chunks_large_transcript_and_merges():
out = await loop._compact_tool_loop(messages, _FakeCompileSessionKey(), budget_chars=20_000)
+ assert provider.calls >= 3 # Multiple source segments plus the final merge.
assert out[2]["content"].startswith("[Context compaction]")
assert "SEGMENT" in out[2]["content"]
# Budget-aware window: at least one complete recent turn is retained and the
@@ -1864,30 +910,6 @@ async def test_compact_tool_loop_chunks_large_transcript_and_merges():
assert out[3:][0]["role"] in {"user", "assistant"}
-async def test_compact_tool_loop_keeps_system_messages_and_preserves_budget_churn_free():
- loop = _compact_loop(_FakeChatProvider("SUMMARY"))
-
- messages = [
- {"role": "system", "content": "SYS"},
- {"role": "user", "content": "TASK"},
- {"role": "user", "content": "[Context compaction]\n## Key facts\nold facts"},
- {"role": "assistant", "content": "recent action"},
- {"role": "user", "content": "recent result"},
- ]
- # Not enough history since the last compaction to warrant a new summarization
- # call: re-emits the existing (small) state without churn.
- out = await loop._compact_tool_loop(messages, _FakeCompileSessionKey())
- assert out == messages
-
-
-def test_local_path_for_viking_uri_drops_namespace_prefix():
- assert local_path_for_viking_uri("viking://resources/a/b.md") == "a/b.md"
- assert local_path_for_viking_uri("viking://user/alice/resources/a/b.md") == "a/b.md"
- assert local_path_for_viking_uri("viking://user/alice/skills/s/guide.md") == "s/guide.md"
- assert local_path_for_viking_uri("viking://resources/sessions/x.jsonl") == "sessions/x.jsonl"
-
-
@pytest.mark.asyncio
async def test_materialize_sources_exports_full_tree_and_writes_manifest():
service = object.__new__(BotCompileService)
@@ -2055,6 +1077,7 @@ async def test_materialize_sources_enforces_actual_total_size():
@pytest.mark.asyncio
async def test_materialize_sources_bounds_downloaded_payloads(monkeypatch):
state = {"pending": 0, "peak": 0}
+ writes = {}
class Client:
async def download_bytes(self, uri):
@@ -2065,9 +1088,10 @@ async def test_materialize_sources_bounds_downloaded_payloads(monkeypatch):
class Sandbox:
async def write_file(self, path, content):
- del path, content
- await asyncio.sleep(0.01)
- state["pending"] -= 1
+ if path.endswith(".txt"):
+ writes[path] = content
+ await asyncio.sleep(0)
+ state["pending"] -= 1
monkeypatch.setattr("vikingbot.compile.service._MATERIALIZE_CONCURRENCY", 2)
service = object.__new__(BotCompileService)
@@ -2088,7 +1112,9 @@ async def test_materialize_sources_bounds_downloaded_payloads(monkeypatch):
await service._materialize_sources(client=Client(), sources=sources, sandbox=Sandbox())
- assert state["peak"] <= 2
+ assert writes == {f"compile_resources/src_1/s/{index}.txt": "x" for index in range(5)}
+ assert 1 <= state["peak"] <= 2
+ assert state["pending"] == 0
@pytest.mark.asyncio
@@ -2136,256 +1162,6 @@ async def test_materialize_target_checkout_preserves_paths_and_bytes():
}
-def test_compile_prompt_mentions_materialized_manifest_when_available():
- request = SanitizedCompileRequest.model_validate(
- {
- "from": ["viking://resources/source"],
- "to": "viking://resources/wiki",
- "skill": "viking://agent/skills/wiki",
- "instruction": "Compile the research",
- }
- )
-
- system, _user = BotCompileService._build_prompts(
- request=request,
- skill_name="wiki",
- skill_content="Write Wiki pages.",
- catalog=[],
- capabilities=CompileCapabilities(exec_enabled=True),
- materialized_manifest="compile_resources/_manifest.tsv",
- materialize_warnings=["failed to materialize viking://resources/source/b.bin: bad"],
- )
-
- assert "Source files are already materialized locally" in system
- assert "compile_resources/_manifest.tsv" in system
- assert "Do NOT" in system
- assert "could NOT be materialized" in system
- assert "viking://resources/source/b.bin" in system
-
-
-def test_compile_prompt_describes_editable_target_checkout():
- request = SanitizedCompileRequest.model_validate(
- {
- "from": ["viking://resources/source"],
- "to": "viking://resources/output",
- "skill": "viking://agent/skills/compiler",
- "instruction": "Refresh the output",
- }
- )
-
- system, user = BotCompileService._build_prompts(
- request=request,
- skill_name="compiler",
- skill_content="Produce the required files.",
- catalog=[],
- capabilities=CompileCapabilities(exec_enabled=True),
- target_checkout_enabled=True,
- )
-
- assert "`__compile_staging__/target_checkout/`" in system
- assert "editable output working tree" in system
- assert "update existing files in place" in system
- assert "submit_wiki_bundle with no arguments" in system
- assert "complete UTF-8 OKF Markdown file" in system
- assert "commits only validated changes" in system
- assert "Inspect the editable target checkout" in user
-
-
-async def test_submit_tool_requires_workspace_paths_for_artifacts():
- tool = SubmitWikiBundleTool(
- source_ids={"src_1"},
- catalog_uris=set(),
- target_uri="viking://resources/wiki",
- limits=CompileLimits(),
- require_workspace_files=True,
- )
- result = await tool.execute(
- ToolContext(),
- pages=[],
- files=[
- {
- "path": "logic/claims.md",
- "content": "# Claims",
- },
- {
- "path": "logic/concepts.md",
- "workspace_path": "ara-output/logic/concepts.md",
- },
- ],
- )
-
- assert result.startswith("Error: Invalid Wiki bundle:")
- assert "must be generated with write_file" in result
- assert tool.bundle is None
-
- single_inline = await tool.execute(
- ToolContext(),
- pages=[],
- files=[
- {
- "path": "PAPER.md",
- "content": "---\ntitle: ARA Paper\nauthors: [Ada]\n---\n\n# Paper",
- }
- ],
- )
- assert single_inline.startswith("Error: Invalid Wiki bundle:")
- assert "submitted using workspace_path instead of inline content" in single_inline
-
- inline_tool = SubmitWikiBundleTool(
- source_ids={"src_1"},
- catalog_uris=set(),
- target_uri="viking://resources/wiki",
- limits=CompileLimits(),
- )
- rejected = await inline_tool.execute(
- ToolContext(),
- pages=[],
- files=[{"path": "concept.md", "content": "---\ntype: ''\n---\n"}],
- )
- assert rejected.startswith("Error: Invalid Wiki bundle:")
- assert 'field "type" must be a non-empty string' in rejected
-
-
-async def test_submit_tool_reads_explicit_workspace_file_and_rejects_memory_files():
- class Sandbox:
- async def read_file_bytes(self, path):
- assert path == "ara-output/figure.png"
- return b"PNG"
-
- class Manager:
- async def get_sandbox(self, session_key):
- assert session_key is not None
- return Sandbox()
-
- context = ToolContext(
- session_key=SessionKey(type="compile", channel_id="cmp", chat_id="cmp"),
- sandbox_manager=Manager(),
- )
- resource_tool = SubmitWikiBundleTool(
- source_ids={"src_1"},
- catalog_uris=set(),
- target_uri="viking://resources/wiki",
- limits=CompileLimits(),
- )
- params = {
- "pages": [],
- "files": [
- {
- "path": "evidence/figure.png",
- "workspace_path": "ara-output/figure.png",
- }
- ],
- }
- accepted = await resource_tool.execute(context, **params)
- assert not accepted.startswith("Error:")
- assert resource_tool.file_payloads == [b"PNG"]
-
- memory_tool = SubmitWikiBundleTool(
- source_ids={"src_1"},
- catalog_uris=set(),
- target_uri="viking://user/alice/memories/preferences/wiki",
- limits=CompileLimits(),
- )
- rejected = await memory_tool.execute(
- context,
- pages=[],
- files=[{"path": "trace/tree.yaml", "content": "nodes: []"}],
- )
- assert rejected.startswith("Error:")
- assert "Resource targets" in rejected
-
-
-async def test_submit_tool_rejects_non_utf8_declared_okf_workspace_markdown():
- class Sandbox:
- async def read_file_bytes(self, path):
- assert path == "generated/concept.md"
- return b"---\ntype: concept\n---\n\xff"
-
- class Manager:
- async def get_sandbox(self, session_key):
- return Sandbox()
-
- context = ToolContext(
- session_key=SessionKey(type="compile", channel_id="cmp", chat_id="cmp"),
- sandbox_manager=Manager(),
- )
- tool = SubmitWikiBundleTool(
- source_ids={"src_1"},
- catalog_uris=set(),
- target_uri="viking://resources/wiki",
- limits=CompileLimits(),
- )
-
- rejected = await tool.execute(
- context,
- pages=[],
- files=[
- {
- "path": "concept.md",
- "workspace_path": "generated/concept.md",
- }
- ],
- )
-
- assert rejected.startswith("Error: Invalid Wiki bundle:")
- assert "must be UTF-8" in rejected
-
-
-async def test_submit_tool_materializes_workspace_page_body_before_validation():
- class Sandbox:
- async def read_file_bytes(self, path):
- return {
- "__compile_staging__/wiki_pages/overview.md": b"Read Details next.",
- "__compile_staging__/wiki_pages/details.md": b"Details body.",
- }[path]
-
- class Manager:
- async def get_sandbox(self, session_key):
- assert session_key is not None
- return Sandbox()
-
- context = ToolContext(
- session_key=SessionKey(type="compile", channel_id="cmp", chat_id="cmp"),
- sandbox_manager=Manager(),
- )
- tool = SubmitWikiBundleTool(
- source_ids={"src_1"},
- catalog_uris=set(),
- target_uri="viking://resources/wiki",
- limits=CompileLimits(),
- require_workspace_pages=True,
- )
- overview = _page(1, "Overview")
- overview.pop("body_markdown")
- overview["body_workspace_path"] = "__compile_staging__/wiki_pages/overview.md"
- details = _page(2, "Details")
- details.pop("body_markdown")
- details["body_workspace_path"] = "__compile_staging__/wiki_pages/details.md"
-
- accepted = await tool.execute(
- context,
- pages=[overview, details],
- files=[],
- links=[{"f": 1, "t": 2, "match_text": "Details"}],
- )
- assert accepted == "Wiki bundle accepted with 2 page(s) and 0 file(s)."
- assert tool.bundle is not None
- assert tool.bundle.pages[0].body_markdown == "Read Details next."
- assert tool.bundle.pages[0].body_workspace_path is None
-
- rejected = await tool.execute(
- context,
- pages=[_page(1, "Inline")],
- files=[],
- )
- assert "must be generated with write_file" in rejected
- assert tool.bundle is None
-
-
@pytest.mark.asyncio
async def test_submit_tool_checkout_writes_complete_tree_with_upsert():
relative_files = {
@@ -2509,150 +1285,26 @@ async def test_submit_tool_checkout_rejects_incomplete_wiki_frontmatter():
async def read_file_bytes(self, path, *, max_bytes=None):
del path, max_bytes
- return self.payload
-
- class Manager:
- async def get_sandbox(self, session_key):
- del session_key
- return Sandbox()
-
- tool = SubmitTargetCheckoutTool(
- target_uri="viking://resources/wiki",
- source_roots={},
- limits=CompileLimits(),
- )
- context = ToolContext(
- session_key=SessionKey(type="compile", channel_id="cmp", chat_id="cmp"),
- sandbox_manager=Manager(),
- )
-
- incomplete = await tool.execute(context)
- assert "must have non-empty YAML frontmatter fields: title, description" in incomplete
- assert tool.bundle is None
-
-
-async def test_submit_tool_checkout_rejects_manifest_arguments():
- tool = SubmitTargetCheckoutTool(
- target_uri="viking://resources/wiki",
- source_roots={},
- limits=CompileLimits(),
- )
-
- result = await tool.execute(ToolContext(), pages=[], files=[])
-
- assert result == "Error: submit_wiki_bundle takes no arguments for a Resource checkout."
- assert tool.bundle is None
-
-
-async def test_submit_tool_rejects_artifact_reused_as_wiki_body():
- tool = SubmitWikiBundleTool(
- source_ids={"src_1"},
- catalog_uris=set(),
- target_uri="viking://resources/wiki",
- limits=CompileLimits(),
- require_workspace_pages=True,
- )
- page = _page(1, "Overview")
- page.pop("body_markdown")
- page["body_workspace_path"] = "./ara-output/PAPER.md"
-
- result = await tool.execute(
- ToolContext(),
- pages=[page],
- files=[
- {
- "path": "PAPER.md",
- "workspace_path": "ara-output/PAPER.md",
- }
- ],
- )
-
- assert result.startswith("Error: Invalid Wiki bundle:")
- assert "must be editable files under __compile_staging__/wiki_pages/" in result
- assert tool.bundle is None
-
-
-async def test_submit_tool_preserves_generated_skill_artifacts_alongside_staged_wiki_pages():
- files = {
- "skills/compiler/SKILL.md": b"input skill",
- "ara-output/PAPER.md": b"---\ntitle: ARA\nauthors: [Ada]\nyear: 2026\n---\n\n# ARA",
- "ara-output/logic/problem.md": b"# Problem",
- "__compile_staging__/wiki_pages/overview.md": b"# Reader overview",
- "__compile_staging__/tmp/notes.md": b"scratch",
- }
-
- class Sandbox:
- async def list_dir(self, path):
- directories = {
- ".": [("ara-output", True), ("__compile_staging__", True), ("skills", True)],
- "ara-output": [("PAPER.md", False), ("logic", True)],
- "ara-output/logic": [("problem.md", False)],
- "skills": [("compiler", True)],
- "skills/compiler": [("SKILL.md", False)],
- }
- return directories[path]
-
- async def read_file_bytes(self, path):
- return files[path]
+ return self.payload
class Manager:
async def get_sandbox(self, session_key):
- assert session_key is not None
+ del session_key
return Sandbox()
- context = ToolContext(
- session_key=SessionKey(type="compile", channel_id="cmp", chat_id="cmp"),
- sandbox_manager=Manager(),
- )
- tool = SubmitWikiBundleTool(
- source_ids={"src_1"},
- catalog_uris=set(),
+ tool = SubmitTargetCheckoutTool(
target_uri="viking://resources/wiki",
+ source_roots={},
limits=CompileLimits(),
- require_workspace_files=True,
- require_workspace_pages=True,
- workspace_baseline={"skills/compiler/SKILL.md"},
)
- page = _page(1, "Overview")
- page.pop("body_markdown")
- page["body_workspace_path"] = "__compile_staging__/wiki_pages/overview.md"
-
- rejected = await tool.execute(
- context,
- pages=[page],
- files=[],
+ context = ToolContext(
+ session_key=SessionKey(type="compile", channel_id="cmp", chat_id="cmp"),
+ sandbox_manager=Manager(),
)
- assert "generated Skill artifacts are missing from files" in rejected
- assert "ara-output/PAPER.md" in rejected
- assert "ara-output/logic/problem.md" in rejected
- accepted = await tool.execute(
- context,
- pages=[page],
- files=[
- {
- "path": "PAPER.md",
- "workspace_path": "ara-output/PAPER.md",
- },
- {
- "path": "logic/problem.md",
- "workspace_path": "ara-output/logic/problem.md",
- },
- ],
- )
- assert accepted == "Wiki bundle accepted with 1 page(s) and 2 file(s)."
- assert tool.bundle is not None
- assert [file.path for file in tool.bundle.files] == [
- "PAPER.md",
- "logic/problem.md",
- ]
- assert tool.file_payloads == [
- files["ara-output/PAPER.md"],
- files["ara-output/logic/problem.md"],
- ]
+ incomplete = await tool.execute(context)
+ assert "must have non-empty YAML frontmatter fields: title, description" in incomplete
+ assert tool.bundle is None
class _EchoTool(Tool):
@@ -2680,7 +1332,7 @@ async def test_scoped_tool_requires_and_bounds_openviking_uri():
roots=("viking://resources/source",),
limits=CompileLimits(),
result_budget={"bytes": 0},
- budget_lock=__import__("asyncio").Lock(),
+ budget_lock=asyncio.Lock(),
)
context = ToolContext()
assert (await wrapped.execute(context)).startswith("Error:")
@@ -2697,60 +1349,30 @@ async def test_scoped_tool_requires_and_bounds_openviking_uri():
@pytest.mark.asyncio
async def test_scoped_tool_enforces_per_call_and_total_result_budgets():
+ class ResultTool(_EchoTool):
+ async def execute(self, tool_context, **kwargs):
+ return kwargs["result"]
+
limits = CompileLimits(tool_result_bytes=8, tool_total_result_bytes=12)
budget = {"bytes": 0}
wrapped = CompileScopedTool(
- _EchoTool(),
+ ResultTool(),
roots=("viking://resources/source",),
limits=limits,
result_budget=budget,
- budget_lock=__import__("asyncio").Lock(),
+ budget_lock=asyncio.Lock(),
)
context = ToolContext()
- oversized = await wrapped.execute(context, uri="viking://resources/source/child")
- assert oversized.startswith("Error:")
+ kwargs = {"uri": "viking://resources/source/child"}
+ oversized = await wrapped.execute(context, **kwargs, result="x" * 9)
+ assert "per-call size limit" in oversized
assert budget["bytes"] == 0
-
-
-async def test_structured_wrapper_delegates_to_only_existing_loop_without_fallback():
- registry = ToolRegistry()
- submit = SubmitWikiBundleTool(
- source_ids={"src_1"},
- catalog_uris=set(),
- target_uri="viking://resources/wiki",
- limits=CompileLimits(),
- )
- registry.register(submit)
- expected = WikiBundleDraft.model_validate({"pages": []})
-
- class FakeLoop:
- async def _run_agent_loop(self, **kwargs):
- assert kwargs["tool_registry"] is registry
- assert kwargs["stop_tool_names"] == ["submit_wiki_bundle"]
- assert kwargs["allow_final_fallback"] is False
- assert kwargs["inject_write_experience"] is False
- assert kwargs["messages"] == [
- {"role": "system", "content": "system"},
- {"role": "user", "content": "user"},
- ]
- submit.bundle = expected
- return None, None, [], {"total_tokens": 1}, 1
-
- bundle, tools, usage, iterations = await AgentLoop.run_structured_task(
- FakeLoop(),
- system_prompt="system",
- user_prompt="user",
- session_key=SessionKey(type="compile", channel_id="cmp", chat_id="cmp"),
- tool_registry=registry,
- openviking_tool_names=set(),
- stop_tool_names=["submit_wiki_bundle"],
- openviking_connection={"api_key": "secret"},
- )
- assert bundle is expected
- assert tools == []
- assert usage == {"total_tokens": 1}
- assert iterations == 1
+ assert await wrapped.execute(context, **kwargs, result="x" * 8) == "x" * 8
+ assert await wrapped.execute(context, **kwargs, result="y" * 4) == "y" * 4
+ assert budget["bytes"] == 12
+ exhausted = await wrapped.execute(context, **kwargs, result="z")
+ assert "task tool-result budget exceeded" in exhausted
+ assert budget["bytes"] == 12
@pytest.mark.asyncio
@@ -2789,29 +1411,6 @@ async def test_structured_wrapper_distinguishes_iteration_exhaustion(iteration,
)
-def test_budget_reminder_text_escalates_with_remaining_rounds():
- assert render_budget_reminder(70) is None
- assert render_budget_reminder(16) is None
-
- heads_up = render_budget_reminder(15)
- warn = render_budget_reminder(8)
- critical = render_budget_reminder(3)
-
- assert "还剩 15 轮" in heads_up
- assert "停止对已读文件的全量重扫" in heads_up
- assert "还剩 8 轮" in warn
- assert "必须开始提交" in warn
- assert "未覆盖/待确认" in warn
- assert "还剩 3 轮" in critical
- assert "立即提交当前最好结果" in critical
- assert "禁止再开启新的探索/读取" in critical
- # The consequence sentence ties the reminder to the real salvage failure mode.
- for note in (heads_up, warn, critical):
- assert "submit_wiki_bundle" in note
- assert "不经校验" in note
- assert "还剩 0 轮" in render_budget_reminder(0)
-
-
@pytest.mark.asyncio
async def test_readlist_tracker_records_and_summarizes_without_duplicates():
class Sandbox:
@@ -2957,63 +1556,6 @@ async def test_read_tracking_tool_records_read_file_edit_and_explicit_exec():
}
- ("iteration", "with_note"),
- [
- (42, True), # max_iterations=50, remaining == 8 -> budget reminder fires
- (1, False), # no thresholds/readlist -> provider stays quiet by default
- ],
-)
-async def test_structured_task_injects_status_note_provider(iteration, with_note):
- registry = ToolRegistry()
- submit = SubmitWikiBundleTool(
- source_ids={"src_1"},
- catalog_uris=set(),
- target_uri="viking://resources/wiki",
- limits=CompileLimits(),
- )
- registry.register(submit)
-
- class FakeReadlist:
- async def summary(self):
- return "已读 1/2 个源文件,未读 1 个。"
-
- captured: dict = {}
-
- class FakeLoop:
- max_iterations = 50
-
- async def _run_agent_loop(self, **kwargs):
- captured["provider"] = kwargs.get("status_note_provider")
- captured["note"] = await captured["provider"](iteration)
- submit.bundle = WikiBundleDraft.model_validate({"pages": []})
- return None, None, [], {}, iteration
-
- kwargs: dict = {
- "system_prompt": "system",
- "user_prompt": "user",
- "session_key": SessionKey(type="compile", channel_id="cmp", chat_id="cmp"),
- "tool_registry": registry,
- "openviking_tool_names": set(),
- "stop_tool_names": ["submit_wiki_bundle"],
- "openviking_connection": None,
- }
- if with_note:
- kwargs["budget_reminder_thresholds"] = (15, 8, 3)
- kwargs["readlist_provider"] = FakeReadlist()
-
- await AgentLoop.run_structured_task(FakeLoop(), **kwargs)
-
- note = captured["note"]
- if not with_note:
- assert note is None
- return
- assert "必须开始提交" in note
- assert "已读 1/2 个源文件" in note
- assert note.index("必须开始提交") < note.index("已读 1/2") # budget reminder first
-
-
@pytest.mark.asyncio
async def test_request_normalization_uses_default_instruction_and_canonical_skill(monkeypatch):
class Client:
@@ -3084,22 +1626,6 @@ async def test_request_normalization_uses_default_instruction_and_canonical_skil
assert raised.value.code == "SKILL_INVALID"
assert Client.created == set()
-def test_compile_target_accepts_only_exact_skill_namespaces():
- directory = {"isDir": True}
-
- BotCompileService._validate_target_directory("viking://agent/skills", directory)
- BotCompileService._validate_target_directory("viking://user/alice/skills", directory)
- BotCompileService._validate_target_directory("viking://user/skills", directory)
-
- with pytest.raises(CompileFailure, match="supported skills namespace"):
- BotCompileService._validate_target_directory(
- "viking://agent/skills/existing-skill", directory
- )
- with pytest.raises(CompileFailure, match="supported skills namespace"):
- BotCompileService._validate_target_directory(
- "viking://agent/legacy-agent/skills", directory
- )
-
@pytest.mark.asyncio
@pytest.mark.parametrize("existing", [False, True])
@@ -3692,37 +2218,6 @@ async def test_target_catalog_search_failure_keeps_collision_inventory():
assert set(inventory) == {"viking://resources/wiki/existing.md"}
-async def test_load_target_wiki_raw_reads_every_okf_page_but_skips_markdown_artifacts():
- wiki_uri = "viking://resources/wiki/entity/topic.md"
- artifact_uri = "viking://resources/wiki/README.md"
- contents = {
- wiki_uri: "---\ntype: entity\ntitle: Topic\n---\n\n# Topic\n",
- artifact_uri: "# README\n",
- }
-
- class Client:
- async def read_raw(self, uri, *, offset=0, limit=-1):
- assert offset == 0
- content = contents[uri]
- if limit == -1:
- return content
- return "".join(content.splitlines(keepends=True)[:limit])
-
- service = object.__new__(BotCompileService)
- service.limits = CompileLimits()
- loaded = await service._load_target_wiki_raw(
- Client(),
- {
- wiki_uri: {"size": len(contents[wiki_uri])},
- artifact_uri: {"size": len(contents[artifact_uri])},
- "viking://resources/wiki/image.png": {"size": 10},
- },
- )
-
- assert loaded == {wiki_uri: contents[wiki_uri]}
-
-
@pytest.mark.asyncio
async def test_memory_target_catalog_uses_memory_search_results():
target = "viking://user/alice/memories/preferences/wiki"
@@ -3799,24 +2294,6 @@ async def test_scoped_tool_redirects_skill_workspace_reads(uri, expected_path):
assert expected_path in result
-async def test_scoped_tool_keeps_normal_scope_errors_and_valid_reads():
- wrapped = CompileScopedTool(
- _NamedTool("openviking_multi_read"),
- roots=("viking://resources/source",),
- limits=CompileLimits(),
- result_budget={"bytes": 0},
- budget_lock=asyncio.Lock(),
- )
-
- rejected = await wrapped.execute(ToolContext(), uris=["viking://resources/other/file.md"])
- accepted = await wrapped.execute(ToolContext(), uris=["viking://resources/source/file.md"])
-
- assert "outside the Compile task scope" in rejected
- assert "read_file" not in rejected
- assert "viking://resources/source/file.md" in accepted
-
-
@pytest.mark.parametrize("exec_enabled", [True, False])
def test_compile_registry_has_a_fixed_ara_compatible_tool_set(exec_enabled):
available = ToolRegistry()
@@ -3874,11 +2351,8 @@ def test_compile_registry_has_a_fixed_ara_compatible_tool_set(exec_enabled):
"openviking_glob",
"openviking_multi_read",
}
- assert registry.tool_names[-1] == "submit_wiki_bundle"
assert all(isinstance(registry.get(name), CompileScopedTool) for name in ov_names)
submit = registry.get("submit_wiki_bundle")
- assert submit.require_workspace_files is False
- assert submit.require_workspace_pages is False
assert submit.exec_enabled is exec_enabled
checkout_registry, _ = service._build_compile_registry(
@@ -3889,7 +2363,6 @@ def test_compile_registry_has_a_fixed_ara_compatible_tool_set(exec_enabled):
)
checkout_submit = checkout_registry.get("submit_wiki_bundle")
assert isinstance(checkout_submit, SubmitTargetCheckoutTool)
- assert checkout_submit.parameters["properties"] == {}
def test_compile_registry_keeps_source_fallback_tools_only_when_needed():
@@ -3951,206 +2424,6 @@ def test_compile_registry_keeps_source_fallback_tools_only_when_needed():
assert "openviking_export" in eager_ov
-def test_compile_prompt_uses_materialized_workflow_when_manifest_available():
- request = SanitizedCompileRequest.model_validate(
- {
- "from": ["viking://resources/source"],
- "to": "viking://resources/wiki",
- "skill": "viking://agent/skills/wiki",
- "instruction": "Compile the research",
- }
- )
- common = {
- "request": request,
- "skill_name": "wiki",
- "skill_content": "Write Wiki pages.",
- "catalog": [],
- "capabilities": CompileCapabilities(exec_enabled=True),
- }
-
- materialized_system, _ = BotCompileService._build_prompts(
- **common,
- materialized_manifest="compile_resources/_manifest.tsv",
- )
- assert "read `compile_resources/_manifest.tsv`" in materialized_system
- assert "Do NOT use openviking_list" in materialized_system
-
- truncated_system, _ = BotCompileService._build_prompts(
- **common,
- materialized_manifest="compile_resources/_manifest.tsv",
- catalog_truncated=True,
- )
- assert "source catalog was truncated" in truncated_system
- assert "openviking_list/openviking_glob/openviking_multi_read" in truncated_system
-
- eager_system, _ = BotCompileService._build_prompts(**common)
- assert "Map the corpus first: run openviking_list" in eager_system
- assert "Do NOT use openviking_list" not in eager_system
-
-
-def test_compile_prompt_includes_per_source_inventory():
- request = SanitizedCompileRequest.model_validate(
- {
- "from": [
- "viking://resources/dream-memory-store",
- "viking://resources/dream-sessions",
- ],
- "to": "viking://resources/wiki",
- "skill": "viking://agent/skills/wiki",
- "instruction": "Compile",
- }
- )
- sources = [
- {
- "source_id": "src_1",
- "directory_uri": "viking://resources/dream-memory-store",
- "file_count": 46,
- "total_bytes": 180_000,
- "entries": [
- {"name": "a.md", "is_dir": False},
- {"name": "b.md", "is_dir": False},
- {"name": "MEMORY", "is_dir": True},
- ],
- },
- {
- "source_id": "src_2",
- "directory_uri": "viking://resources/dream-sessions",
- "file_count": 480,
- "total_bytes": 22_000_000,
- "entries": [{"name": "rollout.jsonl", "is_dir": False}],
- },
- ]
-
- _system, user = BotCompileService._build_prompts(
- request=request,
- skill_name="wiki",
- skill_content="Write pages.",
- catalog=[],
- capabilities=CompileCapabilities(exec_enabled=True),
- sources=sources,
- )
-
- assert "Source inventory (data):" in user
- assert "src_1" in user and "src_2" in user
- assert "46 files" in user
- assert "480 files" in user
- assert "md:2" in user
- assert "jsonl:1" in user
-
- # Without sources (e.g. older callers), the inventory section is omitted entirely.
- _system, user_without = BotCompileService._build_prompts(
- request=request,
- skill_name="wiki",
- skill_content="Write pages.",
- catalog=[],
- capabilities=CompileCapabilities(exec_enabled=True),
- )
- assert "Source inventory (data):" not in user_without
-
-
-def test_compile_prompt_routes_skill_cli_commands_through_exec():
- request = SanitizedCompileRequest.model_validate(
- {
- "from": ["viking://resources/source"],
- "to": "viking://resources/wiki",
- "skill": "viking://agent/skills/ara",
- "instruction": "Compile the research",
- }
- )
-
- system, user = BotCompileService._build_prompts(
- request=request,
- skill_name="ara",
- skill_content="Follow the ARA method.",
- catalog=[],
- capabilities=CompileCapabilities(exec_enabled=True),
- )
-
- assert "When the Skill asks to run Bash, shell commands, or a CLI, use the exec tool." in system
- assert "`skills/ara/`" in system
- assert "resolve its relative paths there and use read_file" in system
- assert (
- "Submit Wiki page bodies and artifact file content inline in submit_wiki_bundle" in system
- )
- assert "workspace_path (files) or body_workspace_path (pages)" in system
- assert "Compile host capability notice" not in system
- assert "Preserve every required output type, path, and format" in system
- assert "preserve Skill-prescribed artifact file trees as exact files" in system
- assert "bundle.links" not in system
- assert "match_text" not in system
- assert "pages=[]" not in system
- assert "body_workspace_path" in system
- assert "use its URI as an ordinary Markdown link" in system
- assert "unavailable" not in user
- assert "verify every output path and format explicitly required by the Skill" in user
- assert "Inspect the source directories" in system
- for implementation_name in (
- "submit_wiki_bundle",
- "source_id",
- "update_uri",
- "workspace_path",
- ):
- assert implementation_name not in user
-
-
-def test_compile_prompt_omits_exec_when_capability_is_disabled():
- request = SanitizedCompileRequest.model_validate(
- {
- "from": ["viking://resources/source"],
- "to": "viking://resources/wiki",
- "skill": "viking://agent/skills/wiki",
- "instruction": "Compile the research",
- }
- )
-
- system, _user = BotCompileService._build_prompts(
- request=request,
- skill_name="wiki",
- skill_content="Write two Wiki pages.",
- catalog=[],
- capabilities=CompileCapabilities(exec_enabled=False),
- )
-
- assert "Command execution is unavailable." in system
- assert "Do not attempt Bash, shell commands, or CLI commands" in system
- assert "use write_file or edit_file" in system
- assert (
- "Submit Wiki page bodies and artifact file content inline in submit_wiki_bundle" in system
- )
- assert "write_file or exec" not in system
- assert "use the exec tool" not in system
-
-
-def test_compile_prompt_requires_one_complete_skill_package_without_exec():
- request = SanitizedCompileRequest.model_validate(
- {
- "from": ["viking://resources/weekly"],
- "to": "viking://agent/skills",
- "skill": "viking://agent/skills/skill-creator",
- "instruction": "Create a weekly report Skill",
- }
- )
-
- system, user = BotCompileService._build_prompts(
- request=request,
- skill_name="skill-creator",
- skill_content="Create a standards-compliant Skill.",
- catalog=[],
- capabilities=CompileCapabilities(exec_enabled=False),
- )
-
- assert "Command execution is unavailable." in system
- assert "write_file or exec" not in system
- assert "workspace_path (files) or body_workspace_path (pages)" in system
- assert "exactly one complete Skill package as artifact files" in system
- assert "<skill-name>/SKILL.md" in system
- assert "Do not produce Wiki pages, links" in system
- assert "one complete Skill package" in user
- assert "on demand" in user
- assert "existing auxiliary files not included in the submission are preserved" in user
- assert "Existing target files" not in user
-
-
def _compile_service(
tmp_path: Path,
*,
@@ -4618,88 +2891,6 @@ async def test_salvage_copies_workspace_and_repairs_links(tmp_path: Path):
assert any("Skipped" in warning for warning in result.warnings)
- "content",
- [
- '[Missing](missing.md "title")',
- "[Missing](missing(1).md)",
- ],
-)
-def test_salvage_removes_unresolved_complex_markdown_links(content: str):
- assert (
- BotCompileService._repair_salvaged_markdown(
- content,
- source_path="guide/topic.md",
- known_paths={"guide/topic.md"},
- )
- == "Missing"
- )
-
-
-def test_salvage_preserves_existing_escaped_parenthesis_link():
- content = r"[Paren](../meta/foo\(1\).md)"
-
- assert (
- BotCompileService._repair_salvaged_markdown(
- content,
- source_path="guide/topic.md",
- known_paths={"meta/foo(1).md"},
- )
- == content
- )
-
-
-async def test_salvage_ignores_preexisting_srt_settings(tmp_path: Path):
- service = _compile_service(
- tmp_path,
- auth_mode="api_key",
- backend=SandboxBackend.SRT,
- )
- settings_path = "sandboxes/cmp-srt-settings.json"
-
- class Client:
- async def tree(self, uri, *, node_limit):
- raise AssertionError(f"target must not be read: {uri}, {node_limit}")
-
- result = await service._salvage_workspace(
- client=Client(),
- request=_sanitized_compile_request(),
- sandbox=_FakeWorkspaceSandbox({}, sizes={settings_path: 128}),
- workspace_baseline={settings_path},
- )
-
- assert result is None
-
-
-async def test_srt_settings_created_by_manager_are_in_the_workspace_baseline(
- monkeypatch, tmp_path: Path
-):
- async def start_without_process(self):
- self.workspace.mkdir(parents=True, exist_ok=True)
-
- monkeypatch.setattr(SrtBackend, "start", start_without_process)
- config = SimpleNamespace(
- sandbox=SandboxConfig(backend=SandboxBackend.SRT, mode=SandboxMode.PER_SESSION),
- skills=[],
- )
- manager = SandboxManager(config, tmp_path / "workspaces", tmp_path / "source")
- session_key = SessionKey(type="compile", channel_id="cmp", chat_id="cmp:windows")
-
- sandbox = await manager.get_sandbox(session_key)
- baseline = {
- entry.path
- for entry in await sandbox.list_files(max_entries=CompileLimits().target_inventory_entries)
- }
-
- workspace_id = manager.to_workspace_id(session_key)
- settings_name = portable_path_component(workspace_id)
- assert ":" in workspace_id
- assert ":" not in settings_name
- assert baseline == {f"sandboxes/{settings_name}-srt-settings.json"}
-
-
@pytest.mark.asyncio
async def test_salvage_skips_oversized_file_before_reading(tmp_path: Path):
limits = CompileLimits(output_total_bytes=4)
@@ -4759,9 +2950,7 @@ async def test_salvage_skips_oversized_file_before_reading(tmp_path: Path):
@pytest.mark.asyncio
-async def test_salvage_grace_returns_when_cancellation_is_suppressed(
- monkeypatch, tmp_path: Path
-):
+async def test_salvage_grace_returns_when_cancellation_is_suppressed(monkeypatch, tmp_path: Path):
service = _compile_service(
tmp_path,
auth_mode="api_key",
@@ -4818,9 +3007,7 @@ async def test_salvage_grace_returns_when_cancellation_is_suppressed(
@pytest.mark.asyncio
-async def test_salvage_keeps_its_grace_period_when_parent_is_cancelled(
- monkeypatch, tmp_path: Path
-):
+async def test_salvage_keeps_its_grace_period_when_parent_is_cancelled(monkeypatch, tmp_path: Path):
service = _compile_service(
tmp_path,
auth_mode="api_key",
@@ -5249,7 +3436,7 @@ async def test_task_store_prunes_expired_and_excess_terminal_records(tmp_path: P
@pytest.mark.asyncio
-async def test_task_owner_isolation_and_skill_snapshot_sync(tmp_path: Path):
+async def test_task_owner_isolation(tmp_path: Path):
store = CompileTaskStore(tmp_path)
now = utc_now()
task = CompileTask(
@@ -5272,10 +3459,13 @@ async def test_task_owner_isolation_and_skill_snapshot_sync(tmp_path: Path):
service = object.__new__(BotCompileService)
service.store = store
service._started = True
- service._start_lock = __import__("asyncio").Lock()
+ service._start_lock = asyncio.Lock()
assert await service.get_task("cmp_owner", principal_scope="other") is None
assert (await service.get_task("cmp_owner", principal_scope="owner"))["task_id"] == "cmp_owner"
+
+async def test_compile_syncs_skill_package_files(tmp_path: Path):
workspace = tmp_path / "workspace"
skill_dir = workspace / "skills" / "wiki" / "references"
skill_dir.mkdir(parents=True)
diff --git a/bot/tests/test_compile_readtrace.py b/bot/tests/test_compile_readtrace.py
index 223bfde71..c08342b58 100644
--- a/bot/tests/test_compile_readtrace.py
+++ b/bot/tests/test_compile_readtrace.py
@@ -1,9 +1,4 @@
-"""Tests for Compile read tracing (audit hook, minimal version).
-
-Verifies that files actually opened by ``exec`` subprocesses via Python land in
-the readlist, while pure enumeration (``glob``) does not, and that the change
-never breaks the exec pipeline (non-Compile sessions untouched).
-"""
+"""Compile source read tracking through Python subprocesses and the sandbox."""
from __future__ import annotations
@@ -13,12 +8,11 @@ import sys
from pathlib import Path
import pytest
-
import vikingbot.compile.readtrace as readtrace
from vikingbot.compile.readlist import READLIST_PATH, ReadlistTracker
+from vikingbot.config.schema import SandboxBackend, SandboxConfig, SandboxMode
from vikingbot.sandbox.backends.direct import DirectBackend
from vikingbot.sandbox.base import SandboxFileInfo
-from vikingbot.config.schema import SandboxBackend, SandboxConfig, SandboxMode
TRACE_DIR = readtrace.TRACE_DIR
READLIST_ENV = "READLIST_FILE"
@@ -50,13 +44,14 @@ def _make_source(workspace: Path, rel: str, content: str = "hello world\n") -> P
def test_audit_hook_records_files_opened_by_python(workspace):
src = _make_source(workspace, "compile_resources/src_2/a.jsonl")
proc = subprocess.run(
- [sys.executable, "-c", f"open({str(src)!r}, 'a').close()"],
+ [sys.executable, "-c", f"print(open({str(src)!r}).read(), end='')"],
cwd=workspace,
env=_read_env(workspace),
capture_output=True,
text=True,
)
assert proc.returncode == 0, proc.stderr
+ assert proc.stdout == "hello world\n"
assert "compile_resources/src_2/a.jsonl" in _readlist_lines(workspace)
@@ -75,6 +70,7 @@ def test_audit_hook_ignores_glob_enumeration(workspace):
text=True,
)
assert proc.returncode == 0, proc.stderr
+ assert proc.stdout.strip() == "2"
assert _readlist_lines(workspace) == [] # glob only enumerates, does not read
@@ -84,7 +80,7 @@ def test_audit_hook_records_relative_open_after_chdir(workspace):
[
sys.executable,
"-c",
- f"import os; os.chdir({str(src.parent)!r}); open('x.jsonl', 'a').close()",
+ f"import os; os.chdir({str(src.parent)!r}); print(open('x.jsonl').read(), end='')",
],
cwd=workspace,
env=_read_env(workspace),
@@ -92,25 +88,8 @@ def test_audit_hook_records_relative_open_after_chdir(workspace):
text=True,
)
assert proc.returncode == 0, proc.stderr
- assert (
- "compile_resources/src_2/dream-sessions/07/02/x.jsonl" in _readlist_lines(workspace)
- )
-
-
-def test_audit_hook_noops_without_readlist_env(workspace):
- _make_source(workspace, "compile_resources/src_2/a.jsonl", "x\n")
- env = dict(os.environ)
- env["PYTHONPATH"] = str(TRACE_DIR) + os.pathsep + env.get("PYTHONPATH", "")
- env.pop(READLIST_ENV, None) # no READLIST_FILE: hook must not install
- proc = subprocess.run(
- [sys.executable, "-c", "open('compile_resources/src_2/a.jsonl', 'a').close()"],
- cwd=workspace,
- env=env,
- capture_output=True,
- text=True,
- )
- assert proc.returncode == 0, proc.stderr
- assert _readlist_lines(workspace) == []
+ assert proc.stdout == "hello world\n"
+ assert "compile_resources/src_2/dream-sessions/07/02/x.jsonl" in _readlist_lines(workspace)
@pytest.mark.asyncio
@@ -162,27 +141,17 @@ async def test_direct_backend_injects_read_env_for_compile_session(tmp_path):
(workspace / "compile_resources/src_2").mkdir(parents=True, exist_ok=True)
(workspace / "compile_resources/src_2/a.jsonl").write_text("x\n", encoding="utf-8")
backend = await _run_direct("compile__cmp_task__cmp_task", workspace)
- result = await backend.execute(
- "python3 -c \"open('compile_resources/src_2/a.jsonl', 'a').close()\""
- )
- assert "Error" not in result
+ try:
+ result = await backend.execute(
+ "python3 -c \"print(open('compile_resources/src_2/a.jsonl').read(), end='')\""
+ )
+ finally:
+ await backend.stop()
+ assert result.strip() == "x"
lines = _readlist_lines(workspace)
assert "compile_resources/src_2/a.jsonl" in lines
-async def test_direct_backend_does_not_inject_for_non_compile_session(tmp_path):
- workspace = tmp_path / "ws"
- (workspace / "compile_resources/src_2").mkdir(parents=True, exist_ok=True)
- (workspace / "compile_resources/src_2/a.jsonl").write_text("x\n", encoding="utf-8")
- backend = await _run_direct("cli__default__chat", workspace)
- result = await backend.execute(
- "python3 -c \"open('compile_resources/src_2/a.jsonl', 'a').close()\""
- )
- assert "Error" not in result
- assert _readlist_lines(workspace) == []
-
-
@pytest.fixture
def workspace(tmp_path):
ws = tmp_path / "workspace"
diff --git a/bot/tests/test_cron_config.py b/bot/tests/test_cron_config.py
index 230202794..2c65a766e 100644
--- a/bot/tests/test_cron_config.py
+++ b/bot/tests/test_cron_config.py
@@ -2,32 +2,15 @@
# SPDX-License-Identifier: AGPL-3.0
"""Cron is opt-in for both tool registration and scheduler startup."""
-import json
from types import SimpleNamespace
from unittest.mock import AsyncMock, Mock
import pytest
from vikingbot.agent.tools.factory import register_default_tools, register_subagent_tools
from vikingbot.agent.tools.registry import ToolRegistry
-from vikingbot.bus.queue import MessageBus
from vikingbot.cli import commands
-from vikingbot.config import loader
-from vikingbot.config.schema import Config, SessionKey
+from vikingbot.config.schema import Config
from vikingbot.cron.service import CronService
-from vikingbot.cron.types import CronSchedule
-
-
[email protected]("enabled", [None, False, True])
-def test_cron_config_load_and_save(tmp_path, monkeypatch, enabled):
- path = tmp_path / "ov.conf"
- tools = {} if enabled is None else {"cron": {"enabled": enabled}}
- path.write_text(json.dumps({"bot": {"tools": tools}}))
- monkeypatch.setattr(loader, "CONFIG_PATH", path)
-
- config = loader.load_config()
- assert config.tools.cron.enabled is (enabled is True)
- loader.save_config(config, path)
- assert loader.load_config().tools.cron.enabled is (enabled is True)
@pytest.mark.parametrize("enabled", [False, True])
@@ -37,8 +20,6 @@ def test_cron_tool_registration(tmp_path, enabled):
service = CronService(tmp_path / "jobs.json")
register_default_tools(registry, config, cron_service=service)
assert registry.has("cron") is enabled
- assert registry.has("exec")
- assert registry.has("read_file")
names = {tool["function"]["name"] for tool in registry.get_definitions()}
assert ("cron" in names) is enabled
@@ -51,25 +32,6 @@ def test_cron_tool_registration(tmp_path, enabled):
assert not without_service.has("cron")
-def test_disabled_cron_preserves_existing_jobs(tmp_path, monkeypatch):
- monkeypatch.setattr(commands, "get_data_dir", lambda: tmp_path)
- path = tmp_path / "cron" / "jobs.json"
- service = CronService(path)
- service.add_job(
- "reminder",
- CronSchedule(kind="every", every_ms=60_000),
- "hello",
- SessionKey(type="cli", channel_id="test", chat_id="test"),
- )
- before = path.read_bytes()
- constructor = Mock(side_effect=AssertionError("Disabled cron must not be constructed"))
- monkeypatch.setattr(commands, "CronService", constructor)
-
- assert commands.prepare_cron(Config(), MessageBus()) is None
- constructor.assert_not_called()
- assert path.read_bytes() == before
-
-
@pytest.mark.parametrize("enabled", [False, True])
@pytest.mark.parametrize("mode", ["gateway", "interactive", "single_turn", "eval"])
def test_startup_respects_cron_config(tmp_path, monkeypatch, enabled, mode):
@@ -119,4 +81,3 @@ def test_startup_respects_cron_config(tmp_path, monkeypatch, enabled, mode):
assert constructor.call_count == int(should_start)
assert cron.start.await_count == int(should_start)
assert prepare_agent.call_args.args[3] is (cron if should_start else None)
- agent.close_mcp.assert_awaited_once()
diff --git a/bot/tests/test_cron_datetime_parsing.py b/bot/tests/test_cron_datetime_parsing.py
index 933404fc6..e1d12a72b 100644
--- a/bot/tests/test_cron_datetime_parsing.py
+++ b/bot/tests/test_cron_datetime_parsing.py
@@ -2,94 +2,52 @@
# SPDX-License-Identifier: AGPL-3.0
"""Regression tests for cron ISO datetime parsing."""
-import sys
from datetime import datetime, timezone
-from pathlib import Path
from types import SimpleNamespace
import pytest
-import typer
-
-sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
-
-from vikingbot.agent.tools.cron import CronTool # noqa: E402
-from vikingbot.cli.commands import cron_add # noqa: E402
-from vikingbot.config.schema import SessionKey # noqa: E402
-from vikingbot.cron.service import CronService, _compute_next_run # noqa: E402
-from vikingbot.cron.types import CronSchedule # noqa: E402
-
-
-def test_cron_cli_accepts_z_datetime(tmp_path, monkeypatch):
- monkeypatch.setattr("vikingbot.config.loader.get_data_dir", lambda: tmp_path)
-
- cron_add(
- name="release",
- message="ship it",
- every=None,
- cron_expr=None,
- at="2099-08-17T10:00:00Z",
- deliver=False,
- timezone=None,
- )
-
- jobs = CronService(tmp_path / "cron" / "jobs.json").list_jobs()
- assert len(jobs) == 1
- assert jobs[0].schedule.at_ms == 4090644000000
-
-
-def test_cron_cli_reports_invalid_datetime(tmp_path, monkeypatch):
- monkeypatch.setattr("vikingbot.config.loader.get_data_dir", lambda: tmp_path)
-
- with pytest.raises(typer.Exit):
- cron_add(
- name="release",
- message="ship it",
- every=None,
- cron_expr=None,
- at="not-a-date",
- deliver=False,
- timezone=None,
+from vikingbot.agent.tools.cron import CronTool
+from vikingbot.cli.commands import cron_add
+from vikingbot.config.schema import SessionKey
+from vikingbot.cron.service import CronService, _compute_next_run
+from vikingbot.cron.types import CronSchedule
+
+
[email protected]("entrypoint", ["cli", "tool"])
+ "options, expected_kind",
+ [
+ pytest.param({"at": "2099-08-17T10:00:00Z"}, "at", id="one-shot-utc"),
+ pytest.param(
+ {"cron_expr": "0 9 * * *", "timezone": "Asia/Shanghai"}, "cron", id="recurring-timezone"
+ ),
+ ],
+)
+async def test_cron_creation_persists_schedule(
+ tmp_path, monkeypatch, entrypoint, options, expected_kind
+):
+ store_path = tmp_path / "cron" / "jobs.json"
+ if entrypoint == "cli":
+ monkeypatch.setattr("vikingbot.config.loader.get_data_dir", lambda: tmp_path)
+ params = {"every": None, "cron_expr": None, "at": None, "timezone": None, **options}
+ cron_add(name="release", message="ship it", deliver=False, **params)
+ else:
+ tool = CronTool(CronService(store_path))
+ context = SimpleNamespace(
+ session_key=SessionKey(type="cli", channel_id="default", chat_id="default"),
+ channel_metadata=None,
)
-
-
-async def test_cron_tool_accepts_z_datetime(tmp_path):
- service = CronService(tmp_path / "jobs.json")
- tool = CronTool(service)
- context = SimpleNamespace(
- session_key=SessionKey(type="cli", channel_id="default", chat_id="default"),
- channel_metadata=None,
- )
-
- result = await tool.execute(
- context,
- action="add",
- name="release",
- message="ship it",
- at="2099-08-17T10:00:00Z",
- )
-
- assert result.startswith("Created job")
- assert service.list_jobs()[0].schedule.at_ms == 4090644000000
-
-
-async def test_cron_tool_reports_invalid_datetime(tmp_path):
- tool = CronTool(CronService(tmp_path / "jobs.json"))
- context = SimpleNamespace(
- session_key=SessionKey(type="cli", channel_id="default", chat_id="default"),
- channel_metadata=None,
- )
-
- result = await tool.execute(
- context,
- action="add",
- name="release",
- message="ship it",
- at="not-a-date",
- )
-
- assert result.startswith("Error: invalid at datetime:")
+ result = await tool.execute(
+ context, action="add", name="release", message="ship it", **options
+ )
+ assert result.startswith("Created job")
+ jobs = CronService(store_path).list_jobs()
+ assert len(jobs) == 1
+ assert jobs[0].schedule.kind == expected_kind
+ if expected_kind == "at":
+ assert jobs[0].schedule.at_ms == 4090644000000
+ else:
+ assert jobs[0].schedule.tz == "Asia/Shanghai"
def test_cron_schedule_uses_explicit_timezone():
@@ -106,55 +64,3 @@ def test_cron_schedule_uses_explicit_timezone():
assert shanghai == int(datetime(2026, 1, 1, 1, tzinfo=timezone.utc).timestamp() * 1000)
assert los_angeles == int(datetime(2026, 1, 1, 17, tzinfo=timezone.utc).timestamp() * 1000)
-
-
-async def test_cron_tool_persists_timezone(tmp_path):
- service = CronService(tmp_path / "jobs.json")
- tool = CronTool(service)
- context = SimpleNamespace(
- session_key=SessionKey(type="cli", channel_id="default", chat_id="default"),
- channel_metadata=None,
- )
-
- result = await tool.execute(
- context,
- action="add",
- name="morning",
- message="good morning",
- cron_expr="0 9 * * *",
- timezone="Asia/Shanghai",
- )
-
- assert result.startswith("Created job")
- persisted = CronService(tmp_path / "jobs.json").list_jobs()
- assert persisted[0].schedule.tz == "Asia/Shanghai"
-
-
-def test_cron_service_rejects_invalid_timezone(tmp_path):
- service = CronService(tmp_path / "jobs.json")
-
- with pytest.raises(ValueError, match="Invalid cron timezone: Mars/Olympus"):
- service.add_job(
- name="invalid timezone",
- schedule=CronSchedule(kind="cron", expr="0 9 * * *", tz="Mars/Olympus"),
- message="hello",
- session_key=SessionKey(type="cli", channel_id="default", chat_id="default"),
- )
-
-
-def test_cron_cli_persists_timezone(tmp_path, monkeypatch):
- monkeypatch.setattr("vikingbot.config.loader.get_data_dir", lambda: tmp_path)
-
- cron_add(
- name="morning",
- message="good morning",
- every=None,
- cron_expr="0 9 * * *",
- at=None,
- deliver=False,
- timezone="Asia/Shanghai",
- )
-
- jobs = CronService(tmp_path / "cron" / "jobs.json").list_jobs()
- assert jobs[0].schedule.tz == "Asia/Shanghai"
diff --git a/bot/vikingbot/tests/unit/test_cron_service.py b/bot/tests/test_cron_service.py
similarity index 99%
rename from bot/vikingbot/tests/unit/test_cron_service.py
rename to bot/tests/test_cron_service.py
index 08ba2c0f9..899c50651 100644
--- a/bot/vikingbot/tests/unit/test_cron_service.py
+++ b/bot/tests/test_cron_service.py
@@ -2,7 +2,6 @@ import json
from pathlib import Path
import pytest
-
from vikingbot.cron.service import CronService
from vikingbot.cron.types import CronJob, CronStore
diff --git a/bot/tests/test_exec_tool.py b/bot/tests/test_exec_tool.py
index ba6408c10..2991b4982 100644
--- a/bot/tests/test_exec_tool.py
+++ b/bot/tests/test_exec_tool.py
@@ -19,56 +19,13 @@ class _SandboxManager:
@pytest.mark.asyncio
-async def test_exec_tool_applies_working_dir_for_all_sandbox_backends():
- calls = []
-
- class Sandbox:
- sandbox_cwd = "/workspace"
-
- async def execute(self, command, timeout):
- calls.append((command, timeout))
- return "ok"
-
- context = SimpleNamespace(
- sandbox_manager=_SandboxManager(Sandbox()),
- session_key="session",
- )
-
- result = await ExecTool(timeout=7).execute(
- context,
- command="pwd",
- working_dir="directory with spaces",
- )
-
- assert result == "ok"
- assert calls == [("cd 'directory with spaces' && pwd", 7)]
-
-
-async def test_exec_tool_pwd_without_working_dir_uses_sandbox_root():
- class Sandbox:
- sandbox_cwd = "/workspace"
-
- async def execute(self, command, timeout):
- raise AssertionError((command, timeout))
-
- context = SimpleNamespace(
- sandbox_manager=_SandboxManager(Sandbox()),
- session_key="session",
- )
-
- result = await ExecTool().execute(context, command="pwd")
-
- assert result == "/workspace"
-
-
-async def test_exec_tool_working_dir_changes_real_command_cwd(tmp_path):
[email protected]("relative_dir", [None, "directory with spaces"])
+async def test_exec_tool_runs_in_selected_directory(tmp_path, relative_dir):
class Config:
restrict_workspaces = True
workspace = tmp_path / "workspace"
- working_dir = workspace / "directory with spaces"
+ working_dir = workspace / relative_dir if relative_dir else workspace
working_dir.mkdir(parents=True)
session_key = SessionKey(type="cli", channel_id="default", chat_id="exec-test")
sandbox = DirectBackend(Config(), session_key, workspace)
@@ -80,8 +37,8 @@ async def test_exec_tool_working_dir_changes_real_command_cwd(tmp_path):
)
result = await ExecTool().execute(
context,
- command="pwd",
- working_dir="directory with spaces",
+ command="python3 -c 'import os; print(os.getcwd())'",
+ working_dir=relative_dir,
)
finally:
await sandbox.stop()
diff --git a/bot/tests/test_feedback_stats.py b/bot/tests/test_feedback_stats.py
index 5a31ace7d..b234894c9 100644
--- a/bot/tests/test_feedback_stats.py
+++ b/bot/tests/test_feedback_stats.py
@@ -1,108 +1,69 @@
import json
-import pytest
-
from vikingbot.observability.feedback_stats import (
- FEEDBACK_STATS_SORT_FIELDS,
build_feedback_stats_display,
compute_feedback_stats,
- format_feedback_stats_markdown,
- format_feedback_stats_table,
select_feedback_stats,
- validate_feedback_stats_sort_by,
)
-def test_compute_feedback_stats_aggregates_minimal_metrics(temp_dir):
- sessions_dir = temp_dir / "bot" / "sessions"
- sessions_dir.mkdir(parents=True)
-
- (sessions_dir / "cli__default__session-1.jsonl").write_text(
- "\n".join(
- [
- json.dumps(
- {
- "_type": "metadata",
- "session_key": "cli__default__session-1",
- "metadata": {
- "feedback_events": [
- {"response_id": "resp-1", "feedback_type": "thumb_up"}
- ],
- "response_outcomes": {
- "resp-1": {"outcome_label": "positive_feedback"},
- "resp-2": {"outcome_label": "reasked"},
- },
- },
- }
- ),
- json.dumps(
- {
- "role": "assistant",
- "content": "answer 1",
- "response_id": "resp-1",
- "timestamp": "2026-05-01T10:00:00",
- }
- ),
- json.dumps(
- {
- "role": "assistant",
- "content": "answer 2",
- "response_id": "resp-2",
- "timestamp": "2026-05-01T10:01:00",
- }
- ),
- ]
- ),
+def _write_session(bot_dir, key, responses, *, feedback, outcomes, **timestamps):
+ directory = bot_dir / "sessions"
+ directory.mkdir(parents=True, exist_ok=True)
+ records = [
+ {
+ "_type": "metadata",
+ "session_key": key,
+ **timestamps,
+ "metadata": {
+ "feedback_events": feedback,
+ "response_outcomes": {
+ response_id: {"outcome_label": label} for response_id, label in outcomes.items()
+ },
+ },
+ }
+ ]
+ records.extend(
+ {
+ "role": "assistant",
+ "content": "answer",
+ "response_id": response_id,
+ "timestamp": timestamp,
+ }
+ for response_id, timestamp in responses
+ )
+ (directory / f"{key}.jsonl").write_text(
+ "\n".join(json.dumps(record) for record in records) + "\n",
encoding="utf-8",
)
- (sessions_dir / "bot_api__demo__session-2.jsonl").write_text(
- "\n".join(
- [
- json.dumps(
- {
- "_type": "metadata",
- "session_key": "bot_api__demo__session-2",
- "metadata": {
- "feedback_events": [
- {"response_id": "resp-3", "feedback_type": "thumb_down"},
- {"response_id": "resp-3", "feedback_type": "thumb_down"},
- ],
- "response_outcomes": {
- "resp-3": {"outcome_label": "negative_feedback"},
- "resp-4": {"outcome_label": "resolved"},
- "resp-5": {"outcome_label": "follow_up_without_feedback"},
- },
- },
- }
- ),
- json.dumps(
- {
- "role": "assistant",
- "content": "answer 3",
- "response_id": "resp-3",
- "timestamp": "2026-05-03T10:00:00",
- }
- ),
- json.dumps(
- {
- "role": "assistant",
- "content": "answer 4",
- "response_id": "resp-4",
- "timestamp": "2026-05-03T10:01:00",
- }
- ),
- json.dumps(
- {
- "role": "assistant",
- "content": "answer 5",
- "response_id": "resp-5",
- "timestamp": "2026-05-03T10:02:00",
- }
- ),
- ]
- ),
- encoding="utf-8",
+
+def test_compute_feedback_stats_aggregates_minimal_metrics(temp_dir):
+ _write_session(
+ temp_dir / "bot",
+ "cli__default__session-1",
+ [("resp-1", "2026-05-01T10:00:00"), ("resp-2", "2026-05-01T10:01:00")],
+ feedback=[{"response_id": "resp-1", "feedback_type": "thumb_up"}],
+ outcomes={"resp-1": "positive_feedback", "resp-2": "reasked"},
+ )
+
+ _write_session(
+ temp_dir / "bot",
+ "bot_api__demo__session-2",
+ [
+ ("resp-3", "2026-05-03T10:00:00"),
+ ("resp-4", "2026-05-03T10:01:00"),
+ ("resp-5", "2026-05-03T10:02:00"),
+ ],
+ feedback=[
+ {"response_id": "resp-3", "feedback_type": "thumb_down"},
+ {"response_id": "resp-3", "feedback_type": "thumb_down"},
+ ],
+ outcomes={
+ "resp-3": "negative_feedback",
+ "resp-4": "resolved",
+ "resp-5": "follow_up_without_feedback",
+ },
)
stats = compute_feedback_stats(temp_dir / "bot")
@@ -144,78 +105,29 @@ def test_compute_feedback_stats_ignores_missing_sessions_dir(temp_dir):
def test_compute_feedback_stats_supports_filters(temp_dir):
- sessions_dir = temp_dir / "bot" / "sessions"
- sessions_dir.mkdir(parents=True)
-
- (sessions_dir / "cli__default__session-1.jsonl").write_text(
- "\n".join(
- [
- json.dumps(
- {
- "_type": "metadata",
- "session_key": "cli__default__session-1",
- "created_at": "2026-05-01T10:00:00",
- "updated_at": "2026-05-01T10:00:00",
- "metadata": {
- "feedback_events": [
- {"response_id": "resp-1", "feedback_type": "thumb_up"}
- ],
- "response_outcomes": {
- "resp-1": {"outcome_label": "positive_feedback"}
- },
- },
- }
- ),
- json.dumps(
- {
- "role": "assistant",
- "content": "answer 1",
- "response_id": "resp-1",
- "timestamp": "2026-05-01T10:00:00",
- }
- ),
- ]
- )
- + "\n",
- encoding="utf-8",
+ _write_session(
+ temp_dir / "bot",
+ "cli__default__session-1",
+ [("resp-1", "2026-05-01T10:00:00")],
+ feedback=[{"response_id": "resp-1", "feedback_type": "thumb_up"}],
+ outcomes={"resp-1": "positive_feedback"},
+ created_at="2026-05-01T10:00:00",
+ updated_at="2026-05-01T10:00:00",
)
- (sessions_dir / "bot_api__demo__session-2.jsonl").write_text(
- "\n".join(
- [
- json.dumps(
- {
- "_type": "metadata",
- "session_key": "bot_api__demo__session-2",
- "created_at": "2026-05-03T10:00:00",
- "updated_at": "2026-05-03T10:00:00",
- "metadata": {
- "feedback_events": [
- {"response_id": "resp-2", "feedback_type": "thumb_down"}
- ],
- "response_outcomes": {
- "resp-2": {"outcome_label": "negative_feedback"}
- },
- },
- }
- ),
- json.dumps(
- {
- "role": "assistant",
- "content": "answer 2",
- "response_id": "resp-2",
- "timestamp": "2026-05-03T10:00:00",
- }
- ),
- ]
- )
- + "\n",
- encoding="utf-8",
+ _write_session(
+ temp_dir / "bot",
+ "bot_api__demo__session-2",
+ [("resp-2", "2026-05-03T10:00:00")],
+ feedback=[{"response_id": "resp-2", "feedback_type": "thumb_down"}],
+ outcomes={"resp-2": "negative_feedback"},
+ created_at="2026-05-03T10:00:00",
+ updated_at="2026-05-03T10:00:00",
)
channel_stats = compute_feedback_stats(temp_dir / "bot", channel="cli__default")
assert channel_stats["summary"]["sessions_scanned"] == 1
assert channel_stats["summary"]["thumb_up_total"] == 1
- assert channel_stats["channels"] == {"cli__default": channel_stats["channels"]["cli__default"]}
+ assert list(channel_stats["channels"]) == ["cli__default"]
session_stats = compute_feedback_stats(temp_dir / "bot", session_key="bot_api__demo__session-2")
assert session_stats["summary"]["sessions_scanned"] == 1
@@ -232,59 +144,6 @@ def test_compute_feedback_stats_supports_filters(temp_dir):
assert list(time_filtered_stats["channels"].keys()) == ["bot_api__demo"]
-def test_compute_feedback_stats_includes_session_breakdown_when_requested(temp_dir):
- sessions_dir = temp_dir / "bot" / "sessions"
- sessions_dir.mkdir(parents=True)
-
- (sessions_dir / "cli__default__session-1.jsonl").write_text(
- "\n".join(
- [
- json.dumps(
- {
- "_type": "metadata",
- "session_key": "cli__default__session-1",
- "updated_at": "2026-05-01T10:00:00",
- "metadata": {
- "feedback_events": [
- {"response_id": "resp-1", "feedback_type": "thumb_up"}
- ],
- "response_outcomes": {
- "resp-1": {"outcome_label": "positive_feedback"},
- "resp-2": {"outcome_label": "resolved"},
- },
- },
- }
- ),
- json.dumps(
- {
- "role": "assistant",
- "content": "answer 1",
- "response_id": "resp-1",
- "timestamp": "2026-05-01T10:00:00",
- }
- ),
- json.dumps(
- {
- "role": "assistant",
- "content": "answer 2",
- "response_id": "resp-2",
- "timestamp": "2026-05-01T10:01:00",
- }
- ),
- ]
- )
- + "\n",
- encoding="utf-8",
- )
-
- stats = compute_feedback_stats(temp_dir / "bot", include_sessions=True)
-
- assert len(stats["sessions"]) == 1
- assert stats["sessions"][0]["session_key"] == "cli__default__session-1"
- assert stats["sessions"][0]["feedback_total"] == 1
- assert stats["sessions"][0]["one_turn_resolution_rate"] == 1.0
-
-
def test_select_feedback_stats_sorts_and_limits_channels_and_sessions():
stats = {
"summary": {"sessions_scanned": 3},
@@ -311,252 +170,25 @@ def test_select_feedback_stats_sorts_and_limits_channels_and_sessions():
assert [session["session_key"] for session in selected["sessions"]] == ["s2", "s3"]
-def test_select_feedback_stats_rejects_unknown_sort_field():
- with pytest.raises(ValueError, match="sort_by must be one of"):
- select_feedback_stats({"summary": {}, "channels": {}, "sessions": []}, sort_by="bad_metric")
-
-
-def test_validate_feedback_stats_sort_by_accepts_known_field():
- assert validate_feedback_stats_sort_by("responses_total") == "responses_total"
- assert "negative_feedback_rate" in FEEDBACK_STATS_SORT_FIELDS
-
-
-def test_format_feedback_stats_table_returns_summary_and_channel_rows():
- stats = {
- "summary": {
- "sessions_scanned": 1,
- "responses_total": 2,
- "responses_with_feedback": 1,
- "feedback_total": 1,
- "thumb_up_total": 1,
- "thumb_down_total": 0,
- "outcomes_total": 2,
- "positive_feedback_total": 1,
- "negative_feedback_total": 0,
- "reasked_total": 0,
- "resolved_total": 1,
- "follow_up_without_feedback_total": 0,
- "feedback_coverage": 0.5,
- "thumbs_up_rate": 1.0,
- "thumbs_down_rate": 0.0,
- "positive_feedback_rate": 0.5,
- "negative_feedback_rate": 0.0,
- "reask_rate": 0.0,
- "one_turn_resolution_rate": 1.0,
- },
- "channels": {
- "cli__default": {
- "responses_total": 2,
- "responses_with_feedback": 1,
- "feedback_total": 1,
- "thumb_up_total": 1,
- "thumb_down_total": 0,
- "outcomes_total": 2,
- "positive_feedback_total": 1,
- "negative_feedback_total": 0,
- "reasked_total": 0,
- "resolved_total": 1,
- "follow_up_without_feedback_total": 0,
- "feedback_coverage": 0.5,
- "thumbs_up_rate": 1.0,
- "thumbs_down_rate": 0.0,
- "positive_feedback_rate": 0.5,
- "negative_feedback_rate": 0.0,
- "reask_rate": 0.0,
- "one_turn_resolution_rate": 1.0,
- }
- },
- "sessions": [
- {
- "session_key": "cli__default__session-1",
- "channel": "cli__default",
- "updated_at": "2026-05-01T10:00:00",
- "responses_total": 2,
- "feedback_total": 1,
- "negative_feedback_total": 0,
- "reasked_total": 0,
- "one_turn_resolution_rate": 1.0,
- }
- ],
- }
-
- table_data = format_feedback_stats_table(stats)
-
- assert ("Sessions Scanned", "1") in table_data["summary_rows"]
- assert ("One-turn Resolution Rate", "100.0%") in table_data["summary_rows"]
- assert table_data["channel_rows"] == [
- ("cli__default", "2", "1", "50.0%", "100.0%", "0.0%", "100.0%")
- ]
- assert table_data["session_rows"] == [
- (
- "cli__default__session-1",
- "cli__default",
- "2026-05-01T10:00:00",
- "2",
- "1",
- "0",
- "0",
- "100.0%",
- )
- ]
-
-
-def test_format_feedback_stats_markdown_returns_summary_and_tables():
- stats = {
- "summary": {
- "sessions_scanned": 1,
- "responses_total": 2,
- "responses_with_feedback": 1,
- "feedback_total": 1,
- "thumb_up_total": 1,
- "thumb_down_total": 0,
- "outcomes_total": 2,
- "positive_feedback_total": 1,
- "negative_feedback_total": 0,
- "reasked_total": 0,
- "resolved_total": 1,
- "follow_up_without_feedback_total": 0,
- "feedback_coverage": 0.5,
- "thumbs_up_rate": 1.0,
- "thumbs_down_rate": 0.0,
- "positive_feedback_rate": 0.5,
- "negative_feedback_rate": 0.0,
- "reask_rate": 0.0,
- "one_turn_resolution_rate": 1.0,
- },
- "channels": {
- "cli__default": {
- "responses_total": 2,
- "responses_with_feedback": 1,
- "feedback_total": 1,
- "thumb_up_total": 1,
- "thumb_down_total": 0,
- "outcomes_total": 2,
- "positive_feedback_total": 1,
- "negative_feedback_total": 0,
- "reasked_total": 0,
- "resolved_total": 1,
- "follow_up_without_feedback_total": 0,
- "feedback_coverage": 0.5,
- "thumbs_up_rate": 1.0,
- "thumbs_down_rate": 0.0,
- "positive_feedback_rate": 0.5,
- "negative_feedback_rate": 0.0,
- "reask_rate": 0.0,
- "one_turn_resolution_rate": 1.0,
- }
- },
- "sessions": [
- {
- "session_key": "cli__default__session-1",
- "channel": "cli__default",
- "updated_at": "2026-05-01T10:00:00",
- "responses_total": 2,
- "feedback_total": 1,
- "negative_feedback_total": 0,
- "reasked_total": 0,
- "one_turn_resolution_rate": 1.0,
- }
- ],
- }
-
- markdown_data = format_feedback_stats_markdown(stats)
-
- assert "**Sessions Scanned:** 1" in markdown_data["summary_markdown"]
- assert "**One-turn Resolution Rate:** 100.0%" in markdown_data["summary_markdown"]
- assert (
- "| Channel | Responses | Feedback | Coverage | Thumbs Up | Thumbs Down | Resolution |"
- in markdown_data["channels_markdown"]
- )
- assert (
- "| cli__default | 2 | 1 | 50.0% | 100.0% | 0.0% | 100.0% |"
- in markdown_data["channels_markdown"]
- )
- assert (
- "| Session | Channel | Updated At | Responses | Feedback | Negative | Reasked | Resolution |"
- in markdown_data["sessions_markdown"]
- )
- assert (
- "| cli__default__session-1 | cli__default | 2026-05-01T10:00:00 | 2 | 1 | 0 | 0 | 100.0% |"
- in markdown_data["sessions_markdown"]
- )
-
-
def test_build_feedback_stats_display_applies_filters_and_sorting(temp_dir):
- sessions_dir = temp_dir / "bot" / "sessions"
- sessions_dir.mkdir(parents=True)
-
- (sessions_dir / "cli__default__session-1.jsonl").write_text(
- "\n".join(
- [
- json.dumps(
- {
- "_type": "metadata",
- "session_key": "cli__default__session-1",
- "updated_at": "2026-05-01T10:00:00",
- "metadata": {
- "feedback_events": [
- {"response_id": "resp-1", "feedback_type": "thumb_up"}
- ],
- "response_outcomes": {
- "resp-1": {"outcome_label": "positive_feedback"},
- "resp-2": {"outcome_label": "resolved"},
- },
- },
- }
- ),
- json.dumps(
- {
- "role": "assistant",
- "content": "answer 1",
- "response_id": "resp-1",
- "timestamp": "2026-05-01T10:00:00",
- }
- ),
- json.dumps(
- {
- "role": "assistant",
- "content": "answer 2",
- "response_id": "resp-2",
- "timestamp": "2026-05-01T10:01:00",
- }
- ),
- ]
- )
- + "\n",
- encoding="utf-8",
+ _write_session(
+ temp_dir / "bot",
+ "cli__default__session-1",
+ [("resp-1", "2026-05-01T10:00:00"), ("resp-2", "2026-05-01T10:01:00")],
+ feedback=[{"response_id": "resp-1", "feedback_type": "thumb_up"}],
+ outcomes={"resp-1": "positive_feedback", "resp-2": "resolved"},
+ updated_at="2026-05-01T10:00:00",
)
- (sessions_dir / "bot_api__demo__session-2.jsonl").write_text(
- "\n".join(
- [
- json.dumps(
- {
- "_type": "metadata",
- "session_key": "bot_api__demo__session-2",
- "updated_at": "2026-05-03T10:00:00",
- "metadata": {
- "feedback_events": [
- {"response_id": "resp-2", "feedback_type": "thumb_down"},
- {"response_id": "resp-2", "feedback_type": "thumb_down"},
- ],
- "response_outcomes": {
- "resp-2": {"outcome_label": "negative_feedback"},
- },
- },
- }
- ),
- json.dumps(
- {
- "role": "assistant",
- "content": "answer 3",
- "response_id": "resp-2",
- "timestamp": "2026-05-03T10:00:00",
- }
- ),
- ]
- )
- + "\n",
- encoding="utf-8",
+ _write_session(
+ temp_dir / "bot",
+ "bot_api__demo__session-2",
+ [("resp-2", "2026-05-03T10:00:00")],
+ feedback=[
+ {"response_id": "resp-2", "feedback_type": "thumb_down"},
+ {"response_id": "resp-2", "feedback_type": "thumb_down"},
+ ],
+ outcomes={"resp-2": "negative_feedback"},
+ updated_at="2026-05-03T10:00:00",
)
display = build_feedback_stats_display(
@@ -575,114 +207,14 @@ def test_build_feedback_stats_display_applies_filters_and_sorting(temp_dir):
assert "cli__default__session-1" not in display["sessions_markdown"]
-def test_compute_feedback_stats_preserves_all_assistant_responses_total_but_uses_tracked_denominators(temp_dir):
- sessions_dir = temp_dir / "bot" / "sessions"
- sessions_dir.mkdir(parents=True)
-
- (sessions_dir / "cli__default__session-1.jsonl").write_text(
- "\n".join(
- [
- json.dumps(
- {
- "_type": "metadata",
- "session_key": "cli__default__session-1",
- "updated_at": "2026-05-01T10:00:00",
- "metadata": {
- "feedback_events": [
- {"response_id": "resp-1", "feedback_type": "thumb_up"}
- ],
- "response_outcomes": {
- "resp-1": {"outcome_label": "positive_feedback"}
- },
- },
- }
- ),
- json.dumps(
- {
- "role": "assistant",
- "content": "answer 1",
- "response_id": "resp-1",
- "timestamp": "2026-05-01T10:00:00",
- }
- ),
- json.dumps(
- {
- "role": "assistant",
- "content": "answer 2",
- "response_id": "resp-2",
- "timestamp": "2026-05-01T10:01:00",
- }
- ),
- ]
- )
- + "\n",
- encoding="utf-8",
- )
-
- stats = compute_feedback_stats(temp_dir / "bot", include_sessions=True)
-
- assert stats["summary"]["responses_total"] == 2
- assert stats["summary"]["tracked_responses_total"] == 1
- assert stats["summary"]["outcomes_total"] == 1
- assert stats["summary"]["responses_with_feedback"] == 1
- assert stats["summary"]["feedback_coverage"] == 1.0
- assert stats["summary"]["positive_feedback_rate"] == 1.0
- assert stats["summary"]["one_turn_resolution_rate"] == 1.0
- assert stats["channels"]["cli__default"]["responses_total"] == 2
- assert stats["channels"]["cli__default"]["tracked_responses_total"] == 1
- assert stats["channels"]["cli__default"]["positive_feedback_rate"] == 1.0
- assert stats["sessions"][0]["responses_total"] == 2
- assert stats["sessions"][0]["tracked_responses_total"] == 1
- assert stats["sessions"][0]["positive_feedback_rate"] == 1.0
-
-
def test_compute_feedback_stats_counts_rating_feedback_via_outcomes(temp_dir):
- sessions_dir = temp_dir / "bot" / "sessions"
- sessions_dir.mkdir(parents=True)
-
- (sessions_dir / "cli__default__session-1.jsonl").write_text(
- "\n".join(
- [
- json.dumps(
- {
- "_type": "metadata",
- "session_key": "cli__default__session-1",
- "updated_at": "2026-05-01T10:00:00",
- "metadata": {
- "feedback_events": [
- {
- "response_id": "resp-1",
- "feedback_type": "rating",
- "feedback_score": 1,
- }
- ],
- "response_outcomes": {
- "resp-1": {"outcome_label": "positive_feedback"},
- "resp-2": {"outcome_label": "resolved"},
- },
- },
- }
- ),
- json.dumps(
- {
- "role": "assistant",
- "content": "answer 1",
- "response_id": "resp-1",
- "timestamp": "2026-05-01T10:00:00",
- }
- ),
- json.dumps(
- {
- "role": "assistant",
- "content": "answer 2",
- "response_id": "resp-2",
- "timestamp": "2026-05-01T10:01:00",
- }
- ),
- ]
- )
- + "\n",
- encoding="utf-8",
+ _write_session(
+ temp_dir / "bot",
+ "cli__default__session-1",
+ [("resp-1", "2026-05-01T10:00:00"), ("resp-2", "2026-05-01T10:01:00")],
+ feedback=[{"response_id": "resp-1", "feedback_type": "rating", "feedback_score": 1}],
+ outcomes={"resp-1": "positive_feedback", "resp-2": "resolved"},
+ updated_at="2026-05-01T10:00:00",
)
stats = compute_feedback_stats(temp_dir / "bot", include_sessions=True)
@@ -702,56 +234,17 @@ def test_compute_feedback_stats_counts_rating_feedback_via_outcomes(temp_dir):
def test_compute_feedback_stats_uses_tracked_responses_as_rate_denominator(temp_dir):
- sessions_dir = temp_dir / "bot" / "sessions"
- sessions_dir.mkdir(parents=True)
-
- (sessions_dir / "cli__default__session-1.jsonl").write_text(
- "\n".join(
- [
- json.dumps(
- {
- "_type": "metadata",
- "session_key": "cli__default__session-1",
- "updated_at": "2026-05-01T10:00:00",
- "metadata": {
- "feedback_events": [
- {"response_id": "resp-1", "feedback_type": "thumb_up"}
- ],
- "response_outcomes": {
- "resp-1": {"outcome_label": "positive_feedback"},
- "resp-2": {"outcome_label": "resolved"},
- },
- },
- }
- ),
- json.dumps(
- {
- "role": "assistant",
- "content": "answer 1",
- "response_id": "resp-1",
- "timestamp": "2026-05-01T10:00:00",
- }
- ),
- json.dumps(
- {
- "role": "assistant",
- "content": "legacy answer 2",
- "response_id": "resp-legacy-2",
- "timestamp": "2026-05-01T10:01:00",
- }
- ),
- json.dumps(
- {
- "role": "assistant",
- "content": "legacy answer 3",
- "response_id": "resp-legacy-3",
- "timestamp": "2026-05-01T10:02:00",
- }
- ),
- ]
- )
- + "\n",
- encoding="utf-8",
+ _write_session(
+ temp_dir / "bot",
+ "cli__default__session-1",
+ [
+ ("resp-1", "2026-05-01T10:00:00"),
+ ("resp-legacy-2", "2026-05-01T10:01:00"),
+ ("resp-legacy-3", "2026-05-01T10:02:00"),
+ ],
+ feedback=[{"response_id": "resp-1", "feedback_type": "thumb_up"}],
+ outcomes={"resp-1": "positive_feedback", "resp-2": "resolved"},
+ updated_at="2026-05-01T10:00:00",
)
stats = compute_feedback_stats(temp_dir / "bot", include_sessions=True)
diff --git a/bot/tests/test_feishu_outbound_images.py b/bot/tests/test_feishu_outbound_images.py
index 0425bf052..100589932 100644
--- a/bot/tests/test_feishu_outbound_images.py
+++ b/bot/tests/test_feishu_outbound_images.py
@@ -3,15 +3,10 @@
"""Regression coverage for knowledge-base images in Feishu cards."""
import json
-import sys
-from pathlib import Path
from types import SimpleNamespace
from unittest.mock import AsyncMock, Mock
import pytest
-
-sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
-
from vikingbot.bus.events import OutboundMessage
from vikingbot.bus.queue import MessageBus
from vikingbot.channels.feishu import FeishuChannel
@@ -138,48 +133,6 @@ async def test_explicit_image_after_code_block_still_sends(delivery):
channel._upload_image_to_feishu.assert_awaited_once_with(PNG)
-async def test_image_listing_card_preserves_uri_without_attaching_image(delivery):
- pytest.importorskip("lark_oapi")
- channel, client, create, msg, config = delivery
- msg.content = (
- f"目前 OpenViking 中有 **1 张图片**:\n\n- `{URI}`\n\n"
- "如果你愿意,我还可以继续帮你查看这张图片。"
- )
- send = Mock(return_value=SimpleNamespace(success=lambda: True))
- channel._client = SimpleNamespace(
- im=SimpleNamespace(v1=SimpleNamespace(message=SimpleNamespace(create=send)))
- )
-
- await channel.send(msg)
-
- send.assert_called_once()
- card = json.loads(send.call_args.args[0].request_body.content)
- assert card["elements"] == [{"tag": "markdown", "content": msg.content}]
- create.assert_not_awaited()
- channel._upload_image_to_feishu.assert_not_awaited()
-
-
[email protected]("channel_type", ["feishu", "telegram"])
-async def test_image_delivery_instructions_are_scoped_to_feishu(tmp_path, channel_type):
- from vikingbot.agent.context import ContextBuilder
-
- context = ContextBuilder(tmp_path)
- context._templates_ensured = True
- context._skills = SimpleNamespace(get_always_skills=lambda: [], build_skills_summary=lambda: "")
- session_key = SessionKey(type=channel_type, channel_id="app", chat_id="chat")
-
- prompt = await context.build_system_prompt(session_key, ov_tools_enable=False)
-
- if channel_type == "feishu":
- assert "![description](viking://...) outside code" in prompt
- assert "To cite an image URI without displaying it, use inline code" in prompt
- assert "Reading an image does not send it to the user" in prompt
- else:
- assert "## Feishu images" not in prompt
-
-
@pytest.mark.asyncio
@pytest.mark.parametrize("stage", ["stat", "download_bytes", "upload"])
async def test_image_failure_keeps_body_and_removes_invalid_markdown(delivery, stage):
diff --git a/bot/tests/test_image_format.py b/bot/tests/test_image_format.py
deleted file mode 100644
index 71a3e8337..000000000
--- a/bot/tests/test_image_format.py
+++ /dev/null
@@ -1,28 +0,0 @@
-# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.
-# SPDX-License-Identifier: AGPL-3.0
-"""Regression tests for image format detection."""
-
-import sys
-from pathlib import Path
-
-sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
-
-from vikingbot.utils.image_format import detect_image_format, sniff_image_format # noqa: E402
-
-
-def test_detect_image_format_identifies_jpeg():
- image_format = detect_image_format(b"\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01")
-
- assert image_format.extension == "jpg"
- assert image_format.mime_type == "image/jpeg"
-
-
-def test_detect_image_format_uses_mime_hint_for_unknown_bytes():
- image_format = detect_image_format(b"unknown", fallback_mime="image/webp; charset=binary")
-
- assert image_format.extension == "webp"
- assert image_format.mime_type == "image/webp"
-
-
-def test_sniff_image_format_rejects_unknown_bytes():
- assert sniff_image_format(b"not an image") is None
diff --git a/bot/tests/test_image_tool_sandbox.py b/bot/tests/test_image_tool_sandbox.py
index bdbf6e331..47c1b8c87 100644
--- a/bot/tests/test_image_tool_sandbox.py
+++ b/bot/tests/test_image_tool_sandbox.py
@@ -3,13 +3,11 @@
"""Regression tests for image tool sandbox file handling."""
import base64
-import importlib.util
-import sys
-import types
-from pathlib import Path
from types import SimpleNamespace
import pytest
+from vikingbot.agent.tools import image as image_module
+from vikingbot.agent.tools.image import ImageGenerationTool
class _FakeSandbox:
@@ -33,70 +31,13 @@ class _SandboxManager:
return self._sandbox
-def _load_image_module(monkeypatch):
- """Load the image tool module directly so this regression stays isolated."""
- repo_root = Path(__file__).resolve().parents[2]
- image_path = repo_root / "bot" / "vikingbot" / "agent" / "tools" / "image.py"
- image_format_path = repo_root / "bot" / "vikingbot" / "utils" / "image_format.py"
-
- base_mod = types.ModuleType("vikingbot.agent.tools.base")
-
- class Tool:
- pass
-
- class ToolContext:
- pass
-
- base_mod.Tool = Tool
- base_mod.ToolContext = ToolContext
-
- events_mod = types.ModuleType("vikingbot.bus.events")
-
- class OutboundMessage:
- def __init__(self, **kwargs):
- self.__dict__.update(kwargs)
-
- events_mod.OutboundMessage = OutboundMessage
-
- utils_mod = types.ModuleType("vikingbot.utils")
- utils_mod.get_data_path = lambda: repo_root
- image_format_spec = importlib.util.spec_from_file_location(
- "image_format_under_test", image_format_path
- )
- image_format_module = importlib.util.module_from_spec(image_format_spec)
- assert image_format_spec.loader is not None
- image_format_spec.loader.exec_module(image_format_module)
- utils_mod.detect_image_format = image_format_module.detect_image_format
-
- monkeypatch.setitem(sys.modules, "vikingbot", types.ModuleType("vikingbot"))
- monkeypatch.setitem(sys.modules, "vikingbot.agent", types.ModuleType("vikingbot.agent"))
- monkeypatch.setitem(
- sys.modules, "vikingbot.agent.tools", types.ModuleType("vikingbot.agent.tools")
- )
- monkeypatch.setitem(sys.modules, "vikingbot.agent.tools.base", base_mod)
- monkeypatch.setitem(sys.modules, "vikingbot.bus", types.ModuleType("vikingbot.bus"))
- monkeypatch.setitem(sys.modules, "vikingbot.bus.events", events_mod)
- monkeypatch.setitem(sys.modules, "vikingbot.utils", utils_mod)
-
- spec = importlib.util.spec_from_file_location("image_tool_under_test", image_path)
- module = importlib.util.module_from_spec(spec)
- assert spec.loader is not None
- spec.loader.exec_module(module)
- return module
-
-
-def _load_image_tool(monkeypatch):
- return _load_image_module(monkeypatch).ImageGenerationTool
-
-
@pytest.mark.asyncio
-async def test_image_tool_uses_sandbox_for_local_paths(monkeypatch, tmp_path):
+async def test_image_tool_uses_sandbox_for_local_paths(tmp_path):
secret = tmp_path / "host-secret.png"
secret.write_bytes(b"OPENVIKING_HOST_SECRET_MARKER")
sandbox = _FakeSandbox(error=PermissionError("outside sandbox"))
context = SimpleNamespace(session_key="session", sandbox_manager=_SandboxManager(sandbox))
- ImageGenerationTool = _load_image_tool(monkeypatch)
tool = ImageGenerationTool()
with pytest.raises(PermissionError):
@@ -106,11 +47,10 @@ async def test_image_tool_uses_sandbox_for_local_paths(monkeypatch, tmp_path):
@pytest.mark.asyncio
-async def test_image_tool_reads_sandbox_local_file_paths(monkeypatch):
+async def test_image_tool_reads_sandbox_local_file_paths():
sandbox = _FakeSandbox(files={"image.png": b"SANDBOX_IMAGE_BYTES"})
context = SimpleNamespace(session_key="session", sandbox_manager=_SandboxManager(sandbox))
- ImageGenerationTool = _load_image_tool(monkeypatch)
tool = ImageGenerationTool()
data_uri, format_type = await tool._parse_image_data("image.png", context)
@@ -120,42 +60,16 @@ async def test_image_tool_reads_sandbox_local_file_paths(monkeypatch):
assert base64.b64decode(data_uri.split(",", 1)[1]) == b"SANDBOX_IMAGE_BYTES"
-async def test_image_tool_keeps_data_uri_support_without_sandbox_context(monkeypatch):
- ImageGenerationTool = _load_image_tool(monkeypatch)
- tool = ImageGenerationTool()
- data_uri = "data:image/png;base64,UE5H"
-
- parsed, format_type = await tool._parse_image_data(data_uri)
-
- assert parsed == data_uri
- assert format_type == "data"
-
-
-def test_image_tool_documents_mask_sandbox_local_paths(monkeypatch):
- ImageGenerationTool = _load_image_tool(monkeypatch)
- tool = ImageGenerationTool()
-
- mask_description = tool.parameters["properties"]["mask"]["description"]
-
- assert "sandbox-local" in mask_description
+ "source, expected_type",
+ [("data:image/png;base64,UE5H", "data"), ("https://example.com/image.png", "url")],
+)
+async def test_image_tool_accepts_remote_images_without_sandbox(source, expected_type):
+ assert await ImageGenerationTool()._parse_image_data(source) == (source, expected_type)
@pytest.mark.asyncio
-async def test_image_tool_keeps_url_support_without_sandbox_context(monkeypatch):
- ImageGenerationTool = _load_image_tool(monkeypatch)
- tool = ImageGenerationTool()
- image_url = "https://example.com/image.png"
-
- parsed, format_type = await tool._parse_image_data(image_url)
-
- assert parsed == image_url
- assert format_type == "url"
-
-
-async def test_image_tool_rejects_local_paths_without_sandbox_context(monkeypatch):
- ImageGenerationTool = _load_image_tool(monkeypatch)
+async def test_image_tool_rejects_local_paths_without_sandbox_context():
tool = ImageGenerationTool()
with pytest.raises(ValueError, match="sandbox context"):
@@ -171,8 +85,7 @@ async def test_edit_mode_reads_base_image_and_mask_from_sandbox(monkeypatch, tmp
}
)
context = SimpleNamespace(session_key="session", sandbox_manager=_SandboxManager(sandbox))
- module = _load_image_module(monkeypatch)
- monkeypatch.setattr(module, "get_data_path", lambda: tmp_path)
+ monkeypatch.setattr(image_module, "get_data_path", lambda: tmp_path)
captured_kwargs = {}
async def fake_image_edit(**kwargs):
@@ -181,8 +94,8 @@ async def test_edit_mode_reads_base_image_and_mask_from_sandbox(monkeypatch, tmp
data=[SimpleNamespace(b64_json=base64.b64encode(b"OUTPUT").decode())]
)
- monkeypatch.setattr(module.litellm, "aimage_edit", fake_image_edit)
- tool = module.ImageGenerationTool(gen_image_model="openai/dall-e-2")
+ monkeypatch.setattr(image_module.litellm, "aimage_edit", fake_image_edit)
+ tool = ImageGenerationTool(gen_image_model="openai/dall-e-2")
result = await tool.execute(
context,
@@ -205,16 +118,17 @@ async def test_edit_mode_reads_base_image_and_mask_from_sandbox(monkeypatch, tmp
@pytest.mark.asyncio
async def test_image_tool_saves_generated_jpeg_with_jpg_extension(monkeypatch, tmp_path):
- module = _load_image_module(monkeypatch)
- monkeypatch.setattr(module, "get_data_path", lambda: tmp_path)
+ monkeypatch.setattr(image_module, "get_data_path", lambda: tmp_path)
jpeg_bytes = b"\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01fake-jpeg"
async def fake_image_generation(**kwargs):
- return SimpleNamespace(data=[SimpleNamespace(b64_json=base64.b64encode(jpeg_bytes).decode())])
+ return SimpleNamespace(
+ data=[SimpleNamespace(b64_json=base64.b64encode(jpeg_bytes).decode())]
+ )
- monkeypatch.setattr(module.litellm, "aimage_generation", fake_image_generation)
+ monkeypatch.setattr(image_module.litellm, "aimage_generation", fake_image_generation)
context = SimpleNamespace(session_key="session", channel_metadata={})
- tool = module.ImageGenerationTool()
+ tool = ImageGenerationTool()
result = await tool.execute(context, mode="generate", prompt="make a jpeg", send_to_user=False)
diff --git a/bot/tests/test_langfuse_outcome_metadata.py b/bot/tests/test_langfuse_outcome_metadata.py
index 5d2ad690c..f04eb10b0 100644
--- a/bot/tests/test_langfuse_outcome_metadata.py
+++ b/bot/tests/test_langfuse_outcome_metadata.py
@@ -12,16 +12,6 @@ class _FakeGeneration:
self.metadata = kwargs["metadata"]
-class _FakeGenerationWithoutMetadata:
- def __init__(self):
- self.updated_metadata = None
- self.trace_id = "trace-456"
- self.id = "obs-456"
-
- def update(self, **kwargs):
- self.updated_metadata = kwargs.get("metadata")
-
-
class _FakeClient:
def __init__(self):
self.flush_calls = 0
@@ -68,65 +58,3 @@ def test_langfuse_client_creates_event_and_score_for_outcome():
assert client._client.scores[0]["observation_id"] == "obs-123"
assert client._client.scores[0]["metadata"]["response_id"] == "resp-123"
assert client._client.flush_calls == 1
-
-
-def test_langfuse_client_records_outcome_without_generation_metadata_attribute():
- client = LangfuseClient.__new__(LangfuseClient)
- client.enabled = True
- client._client = _FakeClient()
- client._observations_by_response_id = {}
- client._metadata_by_response_id = {}
- client._trace_context_by_response_id = {}
-
- generation = _FakeGenerationWithoutMetadata()
- client.register_generation(
- "resp-456",
- generation,
- metadata={"response_id": "resp-456", "has_tools": False},
- )
- client.update_generation_metadata("resp-456", {"finish_reason": "stop"})
- client.update_response_outcome(
- "resp-456",
- "reasked",
- {
- "response_id": "resp-456",
- "outcome_label": "reasked",
- "reask_within_10m": True,
- },
- )
-
- assert client._client.events[0]["trace_context"] == {"trace_id": "trace-456"}
- assert client._client.events[0]["metadata"]["response_id"] == "resp-456"
- assert client._client.events[0]["metadata"]["has_tools"] is False
- assert client._client.events[0]["metadata"]["finish_reason"] == "stop"
- assert client._client.events[0]["metadata"]["outcome_label"] == "reasked"
- assert client._client.scores[0]["observation_id"] == "obs-456"
- assert client._client.scores[0]["metadata"]["reask_within_10m"] is True
- assert client._client.flush_calls == 1
-
-
-def test_langfuse_client_merges_generation_metadata_into_tracked_generation():
- client = LangfuseClient.__new__(LangfuseClient)
- client.enabled = True
- client._client = _FakeClient()
- client._observations_by_response_id = {}
- client._metadata_by_response_id = {}
- client._trace_context_by_response_id = {}
-
- generation = _FakeGeneration(metadata={"response_id": "resp-789"})
- client.register_generation(
- "resp-789",
- generation,
- metadata={"response_id": "resp-789", "has_tools": False},
- )
-
- merged = client.update_generation_metadata(
- "resp-789",
- {"channel": "cli__default", "tool_count": 0},
- )
-
- assert merged["response_id"] == "resp-789"
- assert merged["has_tools"] is False
- assert merged["channel"] == "cli__default"
- assert merged["tool_count"] == 0
- assert generation.metadata == merged
diff --git a/bot/tests/test_minimax_provider.py b/bot/tests/test_minimax_provider.py
index 43f650a6f..68ac8b664 100644
--- a/bot/tests/test_minimax_provider.py
+++ b/bot/tests/test_minimax_provider.py
@@ -4,72 +4,13 @@
from copy import deepcopy
from types import SimpleNamespace
-from urllib.parse import urlparse
import pytest
-from vikingbot.providers.registry import ProviderSpec, find_by_model, find_by_name
from vikingbot.providers.vlm_adapter import VLMProviderAdapter
from openviking.models.vlm.backends.litellm_vlm import LiteLLMVLMProvider
-class TestMiniMaxRegistry:
- """Tests for MiniMax provider registry entries."""
-
- def test_minimax_spec_exists(self):
- """MiniMax must be registered in the PROVIDERS tuple."""
- spec = find_by_name("minimax")
- assert spec is not None, "MiniMax provider not found in registry"
- assert isinstance(spec, ProviderSpec)
-
- def test_minimax_spec_fields(self):
- """Verify MiniMax ProviderSpec has correct field values."""
- spec = find_by_name("minimax")
- assert spec.name == "minimax"
- assert spec.env_key == "MINIMAX_API_KEY"
- assert spec.display_name == "MiniMax"
- assert spec.litellm_prefix == "minimax"
- assert "minimax/" in spec.skip_prefixes
- assert spec.default_api_base == "https://api.minimax.io/v1"
- assert not spec.is_gateway
- assert not spec.is_local
-
- def test_minimax_m3_matched_by_keyword(self):
- """MiniMax-M3 should be matched to the minimax ProviderSpec."""
- spec = find_by_model("MiniMax-M3")
- assert spec is not None, "MiniMax-M3 not matched to any provider"
- assert spec.name == "minimax"
-
- def test_minimax_m2_7_matched_by_keyword(self):
- """MiniMax-M2.7 should be matched to the minimax ProviderSpec."""
- spec = find_by_model("MiniMax-M2.7")
- assert spec is not None, "MiniMax-M2.7 not matched to any provider"
- assert spec.name == "minimax"
-
- def test_minimax_m2_7_highspeed_matched_by_keyword(self):
- """MiniMax-M2.7-highspeed should be matched to the minimax ProviderSpec."""
- spec = find_by_model("MiniMax-M2.7-highspeed")
- assert spec is not None, "MiniMax-M2.7-highspeed not matched to any provider"
- assert spec.name == "minimax"
-
- def test_minimax_keyword_is_case_insensitive(self):
- """Model name matching must be case-insensitive."""
- for model in ("minimax-m2.7", "MINIMAX-M2.7", "MiniMax-M2.7", "MiniMax-m3"):
- spec = find_by_model(model)
- assert spec is not None, f"{model!r} not matched"
- assert spec.name == "minimax"
-
- def test_minimax_api_base_uses_international_domain(self):
- """Default API base must point to the international endpoint."""
- spec = find_by_name("minimax")
- 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"
- )
-
-
@pytest.mark.asyncio
@pytest.mark.parametrize(
"model",
@@ -105,4 +46,3 @@ async def test_minimax_chat_uses_core_litellm_backend(monkeypatch, model):
assert response.content == "Hello!"
assert captured["model"] == f"minimax/{model.removeprefix('minimax/')}"
assert captured["messages"] == original
- assert messages == original
diff --git a/bot/tests/test_openapi_auth.py b/bot/tests/test_openapi_auth.py
index f404337b8..15fc50c48 100644
--- a/bot/tests/test_openapi_auth.py
+++ b/bot/tests/test_openapi_auth.py
@@ -15,7 +15,7 @@ from fastapi.testclient import TestClient
from vikingbot.bus.events import OutboundEventType, OutboundMessage
from vikingbot.bus.queue import MessageBus
from vikingbot.channels.openapi import OpenAPIChannel, OpenAPIChannelConfig, PendingResponse
-from vikingbot.channels.openapi_models import ChatRequest, ChatResponse
+from vikingbot.channels.openapi_models import ChatResponse
from vikingbot.compile.models import CompileAccepted
from vikingbot.config.schema import BotChannelConfig, SessionKey
from vikingbot.session.manager import Session
@@ -236,13 +236,6 @@ class TestOpenAPIAuth:
assert service.connection is None
assert runtime_probes == [{}, {}]
- def test_chat_request_rejects_unsupported_context(self):
- with pytest.raises(ValueError, match="context is not supported"):
- ChatRequest(message="hello", context=[{"role": "user", "content": "prior"}])
- assert ChatRequest(message="hello", context=[]).context == []
- description = ChatRequest.model_json_schema()["properties"]["context"]["description"]
- assert "not supported" in description
-
def test_chat_returns_422_for_unsupported_context(self, message_bus, temp_workspace):
channel = OpenAPIChannel(OpenAPIChannelConfig(), message_bus, temp_workspace)
@@ -1465,53 +1458,23 @@ class TestOpenAPIAuth:
assert pending.events[0]["type"] == "response"
assert pending.events[0]["data"] == {"content": "hello", "response_id": "resp-123"}
- @pytest.mark.asyncio
- async def test_send_forwards_iteration_to_bot_stream(self, message_bus, temp_workspace):
- channel = OpenAPIChannel(
- OpenAPIChannelConfig(),
- message_bus,
- workspace_path=temp_workspace,
- )
+ @pytest.mark.parametrize("channel_type", ["bot_api", "cli"])
+ async def test_send_forwards_iteration(self, message_bus, temp_workspace, channel_type):
+ channel = OpenAPIChannel(OpenAPIChannelConfig(), message_bus, workspace_path=temp_workspace)
pending = PendingResponse()
- channel._bot_pending["default"] = {"session-1": pending}
-
+ if channel_type == "bot_api":
+ channel._bot_pending["default"] = {"session-1": pending}
+ else:
+ channel._pending["session-1"] = pending
await channel.send(
OutboundMessage(
session_key=SessionKey(
- type="bot_api",
- channel_id="default",
- chat_id="session-1",
+ type=channel_type, channel_id="default", chat_id="session-1"
),
content="Iteration 2/10",
event_type=OutboundEventType.ITERATION,
)
)
-
- assert pending.events[0]["type"] == "iteration"
- assert pending.events[0]["data"] == "Iteration 2/10"
-
- @pytest.mark.asyncio
- async def test_send_forwards_iteration_to_openapi_stream(self, message_bus, temp_workspace):
- channel = OpenAPIChannel(
- OpenAPIChannelConfig(),
- message_bus,
- workspace_path=temp_workspace,
- )
- pending = PendingResponse()
- channel._pending["session-1"] = pending
-
- await channel.send(
- OutboundMessage(
- session_key=SessionKey(
- type="cli",
- channel_id="default",
- chat_id="session-1",
- ),
- content="Iteration 2/10",
- event_type=OutboundEventType.ITERATION,
- )
- )
-
assert pending.events[0]["type"] == "iteration"
assert pending.events[0]["data"] == "Iteration 2/10"
@@ -1535,26 +1498,6 @@ class TestOpenAPIAuth:
assert response.status_code == 404
assert response.json()["detail"] == "Response not found"
- def test_rating_feedback_requires_feedback_score(self, message_bus, temp_workspace):
- channel = OpenAPIChannel(
- OpenAPIChannelConfig(),
- message_bus,
- workspace_path=temp_workspace,
- )
- client = _make_client(channel)
-
- response = client.post(
- "/bot/v1/feedback",
- json={
- "session_id": "session-1",
- "response_id": "resp-123",
- "feedback_type": "rating",
- },
- )
-
- assert response.status_code == 422
- assert "feedback_score is required when feedback_type is rating" in response.text
-
def test_feedback_reloads_session_after_stale_cached_miss(self, message_bus, temp_workspace):
channel = OpenAPIChannel(
OpenAPIChannelConfig(),
diff --git a/bot/tests/test_openviking_add_resource.py b/bot/tests/test_openviking_add_resource.py
index 826776717..5e98ea34f 100644
--- a/bot/tests/test_openviking_add_resource.py
+++ b/bot/tests/test_openviking_add_resource.py
@@ -6,67 +6,25 @@ from vikingbot.agent.tools.ov_file import VikingAddResourceTool
from vikingbot.openviking_mount.ov_server import VikingClient
-class _FakeClient:
- def __init__(self, root_uri):
- self.root_uri = root_uri
- self.calls = []
-
- async def add_resource(self, path, description, to=None):
- self.calls.append((path, description, to))
- return {"root_uri": self.root_uri}
-
-
-def test_add_resource_schema_exposes_optional_target_uri():
- tool = VikingAddResourceTool()
-
- assert tool.parameters["properties"]["to"] == {
- "type": "string",
- "description": "Optional exact target URI under viking://resources/. When omitted, OpenViking chooses the resource URI.",
- }
- assert "to" not in tool.parameters["required"]
- assert tool.validate_params({"path": "https://example.com/doc", "description": "doc"}) == []
-
-
- ("target_uri", "root_uri"),
- [
- (None, "viking://resources/doc"),
- ("viking://resources/feishu/doc/config", "viking://resources/feishu/doc/config"),
- ],
-)
-async def test_add_resource_tool_forwards_optional_target(monkeypatch, target_uri, root_uri):
- tool = VikingAddResourceTool()
- client = _FakeClient(root_uri)
-
- async def _fake_get_client(_tool_context):
- return client
-
- monkeypatch.setattr(tool, "_get_client", _fake_get_client)
-
- kwargs = {"path": "https://example.com/doc", "description": "conversation export"}
- if target_uri is not None:
- kwargs["to"] = target_uri
- result = await tool.execute(ToolContext(), **kwargs)
-
- assert client.calls == [("https://example.com/doc", "conversation export", target_uri)]
- assert result == f"Successfully added resource: {root_uri}"
-
-
@pytest.mark.asyncio
@pytest.mark.parametrize("target_uri", [None, "viking://resources/feishu/doc/config"])
-async def test_viking_client_add_resource_forwards_optional_target(target_uri):
+async def test_add_resource_tool_submits_to_sdk(monkeypatch, target_uri):
expected_uri = target_uri or "viking://resources/doc"
sdk_client = AsyncMock()
sdk_client.add_resource.return_value = {"root_uri": expected_uri}
client = object.__new__(VikingClient)
client.client = sdk_client
+ tool = VikingAddResourceTool()
+ monkeypatch.setattr(tool, "_get_client", AsyncMock(return_value=client))
- result = await client.add_resource("/tmp/doc.md", "conversation export", to=target_uri)
+ kwargs = {"path": "https://example.com/doc", "description": "conversation export"}
+ if target_uri is not None:
+ kwargs["to"] = target_uri
+ result = await tool.execute(ToolContext(), **kwargs)
sdk_client.add_resource.assert_awaited_once_with(
- path="/tmp/doc.md",
+ path="https://example.com/doc",
to=target_uri,
options={"reason": "conversation export"},
)
- assert result["root_uri"] == expected_uri
+ assert result == f"Successfully added resource: {expected_uri}"
diff --git a/bot/tests/test_openviking_api_key_type.py b/bot/tests/test_openviking_api_key_type.py
index bcfb07549..5c179b454 100644
--- a/bot/tests/test_openviking_api_key_type.py
+++ b/bot/tests/test_openviking_api_key_type.py
@@ -6,10 +6,8 @@ from unittest.mock import AsyncMock
import pytest
from vikingbot.agent import memory as memory_module
from vikingbot.agent.context import ContextBuilder
-from vikingbot.agent.loop import _is_tool_result_success
from vikingbot.agent.memory import MemoryStore
from vikingbot.agent.tools import ov_file as ov_file_module
-from vikingbot.agent.tools.base import ToolContext
from vikingbot.agent.tools.ov_file import (
VikingGlobTool,
VikingGrepTool,
@@ -17,9 +15,8 @@ from vikingbot.agent.tools.ov_file import (
VikingMemoryCommitTool,
VikingSearchTool,
)
-from vikingbot.cli import commands as commands_module
from vikingbot.config import loader as config_loader_module
-from vikingbot.config.schema import OpenVikingConfig, SessionKey
+from vikingbot.config.schema import SessionKey
from vikingbot.hooks.base import HookContext
from vikingbot.hooks.builtins import openviking_hooks as openviking_hooks_module
from vikingbot.hooks.builtins.openviking_hooks import OpenVikingCompactHook
@@ -33,14 +30,6 @@ from vikingbot.openviking_mount.session_state import (
from vikingbot.session.manager import SessionManager
-class _DummySession:
- async def add_message(self, role, parts, created_at=None):
- return None
-
- async def commit_async(self):
- return {"status": "committed"}
-
-
class _DummyHTTPClient:
instances = []
@@ -79,12 +68,6 @@ class _DummyHTTPClient:
"keep_recent_count": keep_recent_count,
}
- def session(self, _session_id):
- return _DummySession()
-
- async def admin_list_accounts(self):
- return []
-
async def find(self, *_args, **_kwargs):
self.find_calls.append((_args, _kwargs))
return []
@@ -124,12 +107,56 @@ class _DummyHTTPClient:
return None
+class _SessionContextClient:
+ """Record synchronization and simulate a retryable session commit failure."""
+
+ def __init__(self, *, pending_tokens, fail_commit=False, fail_append=False):
+ self.pending_tokens = pending_tokens
+ self.fail_session_commit = fail_commit
+ self.fail_append = fail_append
+ self.append_calls = []
+ self.commit_calls = []
+
+ def session_owner_user_id(self):
+ return "admin"
+
+ async def append_messages(
+ self, session_id, messages, default_user_peer_id=None, session_user_id=None
+ ):
+ self.append_calls.append((session_id, [message["content"] for message in messages]))
+ if self.fail_append:
+ raise RuntimeError("session append failed")
+ return {"session_id": session_id, "added": len(messages)}
+
+ async def get_session(self, session_id, user_id=None):
+ return {"session_id": session_id, "pending_tokens": self.pending_tokens}
+
+ async def commit_session(
+ self,
+ session_id,
+ keep_recent_count=0,
+ user_id=None,
+ *,
+ retention_mode,
+ keep_recent_turn_count,
+ retained_message_token_budget,
+ min_raw_tail_steps,
+ ):
+ assert retention_mode == "turn_budget"
+ assert keep_recent_turn_count == 2
+ self.commit_calls.append((session_id, keep_recent_count, user_id))
+ if self.fail_session_commit:
+ raise RuntimeError("session commit failed")
+ return {"session_id": session_id, "status": "accepted"}
+
+
def _make_config(api_key_type: str, mode: str = "remote", **ov_overrides):
agent_defaults = {
"session_context_enabled": False,
"session_context_token_budget": 12000,
"commit_token_threshold": 6000,
"commit_keep_recent_count": 10,
+ "commit_keep_recent_turn_count": 3,
}
agent_overrides = {}
for key in tuple(agent_defaults):
@@ -152,6 +179,20 @@ def _make_config(api_key_type: str, mode: str = "remote", **ov_overrides):
)
+def compact_hook(monkeypatch):
+ """Bind the real commit hook to an explicit client and session configuration."""
+
+ def create(client, **agent_options):
+ config = _make_config("root", session_context_enabled=True, **agent_options)
+ monkeypatch.setattr(openviking_hooks_module, "load_config", lambda: config)
+ hook = OpenVikingCompactHook()
+ monkeypatch.setattr(hook, "_get_client", AsyncMock(return_value=(client, False)))
+ return hook
+
+ return create
+
+
@pytest.fixture(autouse=True)
def _patch_http_client(monkeypatch):
_DummyHTTPClient.instances.clear()
@@ -172,27 +213,6 @@ def test_viking_client_init_root_mode_sets_account_and_user(monkeypatch):
assert "agent_id" not in first.kwargs
-def test_viking_client_uses_injected_config_without_reloading(monkeypatch):
- config = _make_config("root")
-
- def _unexpected_load():
- raise AssertionError("VikingClient must not reload Bot config")
-
- monkeypatch.setattr(ov_server_module, "load_config", _unexpected_load)
-
- client = VikingClient(config=config)
-
- assert client.config is config
- assert client.openviking_config is config.ov_server
- assert _DummyHTTPClient.instances[0].kwargs["api_key"] == "root-key"
-
-
-def test_tool_result_success_only_treats_standard_error_prefix_as_failure():
- assert _is_tool_result_success("errorCode = 0") is True
- assert _is_tool_result_success("Error budget: 5%") is True
- assert _is_tool_result_success("Error: failed") is False
-
-
def test_viking_client_init_user_mode_does_not_set_user_or_account(monkeypatch):
monkeypatch.setattr(ov_server_module, "load_config", lambda: _make_config("user"))
@@ -229,14 +249,6 @@ def test_viking_client_uses_effective_auth_mode_for_dev(monkeypatch):
assert first.kwargs == {"url": "http://ov.local"}
-def test_openviking_config_api_key_type_empty_values_are_inferred():
- assert OpenVikingConfig(api_key_type=None, api_key="user-key").api_key_type == "user"
- assert OpenVikingConfig(api_key_type="", api_key="user-key").api_key_type == "user"
- config = OpenVikingConfig(api_key_type="root", api_key="root-key")
- assert config.api_key_type == "root"
- assert config.api_key == "root-key"
-
-
def test_user_key_current_memory_targets_use_home_alias(monkeypatch):
monkeypatch.setattr(ov_server_module, "load_config", lambda: _make_config("user"))
@@ -314,47 +326,28 @@ def test_ov_server_api_key_mode_does_not_read_ovcli_user_key():
assert bot_data["api_key_type"] == "user"
-def test_ov_server_trusted_mode_fills_api_key_from_top_level_root_key():
- bot_data = {}
- ov_data = {"auth_mode": "trusted", "root_api_key": "server-root-key"}
-
- config_loader_module._merge_ov_server_config(bot_data, ov_data, server_section_present=True)
-
- assert bot_data["mode"] == "remote"
- assert bot_data["api_key"] == "server-root-key"
- assert "root_api_key" not in bot_data
- assert bot_data["api_key_type"] == "root"
-
-
-def test_ov_server_current_trusted_prefers_top_level_root_key():
- bot_data = {"api_key": "stale-bot-key"}
- ov_data = {"auth_mode": "trusted", "root_api_key": "server-root-key"}
-
- config_loader_module._merge_ov_server_config(bot_data, ov_data, server_section_present=True)
-
[email protected]("bot_key", [None, "stale-bot-key"])
+def test_trusted_server_uses_root_key(bot_key):
+ bot_data = {"api_key": bot_key} if bot_key is not None else {}
+ config_loader_module._merge_ov_server_config(
+ bot_data,
+ {"auth_mode": "trusted", "root_api_key": "server-root-key"},
+ server_section_present=True,
+ )
assert bot_data["mode"] == "remote"
assert bot_data["api_key"] == "server-root-key"
assert bot_data["api_key_type"] == "root"
-
-
-def test_ov_server_external_url_does_not_inherit_trusted_root_key():
- bot_data = {"server_url": "https://external.example"}
- ov_data = {"auth_mode": "trusted", "root_api_key": "server-root-key"}
-
- config_loader_module._merge_ov_server_config(bot_data, ov_data, server_section_present=True)
-
- assert bot_data["mode"] == "remote"
- assert "api_key" not in bot_data
- assert bot_data["api_key_type"] == "user"
assert "root_api_key" not in bot_data
-def test_ov_server_explicit_url_is_external_even_if_it_matches_local_url():
- bot_data = {"server_url": "http://localhost:1933"}
- ov_data = {"auth_mode": "trusted", "root_api_key": "server-root-key"}
-
- config_loader_module._merge_ov_server_config(bot_data, ov_data, server_section_present=True)
-
[email protected]("url", ["https://external.example", "http://localhost:1933"])
+def test_explicit_upstream_does_not_inherit_root_key(url):
+ bot_data = {"server_url": url}
+ config_loader_module._merge_ov_server_config(
+ bot_data,
+ {"auth_mode": "trusted", "root_api_key": "server-root-key"},
+ server_section_present=True,
+ )
assert bot_data["mode"] == "remote"
assert "api_key" not in bot_data
assert bot_data["api_key_type"] == "user"
@@ -515,7 +508,6 @@ def test_vikingbot_load_config_does_not_read_ovcli_conf(monkeypatch, tmp_path):
config = config_loader_module.load_config()
- assert not hasattr(config_loader_module, "load_ovcli_config")
assert config.ov_server.api_key == "bot-user-key"
assert config.ov_server.get_api_key_source() == "bot.ov_server.api_key"
@@ -911,32 +903,6 @@ def test_validate_openviking_auth_allows_trusted_root(monkeypatch, capsys):
assert captured.err == ""
-def test_memory_user_cli_option_warns_at_runtime(capsys):
- commands_module._warn_deprecated_memory_user(["legacy-user"])
-
- captured = capsys.readouterr()
- assert "--memory-user is deprecated" in captured.err
- assert "--memory-peer" in captured.err
-
-
[email protected]("api_key_type", ["root", "user"])
-async def test_viking_client_initializes_without_admin_account_lookup(monkeypatch, api_key_type):
- monkeypatch.setattr(ov_server_module, "load_config", lambda: _make_config(api_key_type))
-
- lookup = AsyncMock(return_value=[])
- monkeypatch.setattr(_DummyHTTPClient, "admin_list_accounts", lookup)
- initialize = AsyncMock()
- monkeypatch.setattr(_DummyHTTPClient, "initialize", initialize)
-
- client = await VikingClient.create(agent_id="workspace")
-
- initialize.assert_awaited_once()
- lookup.assert_not_called()
- expected = "viking://user/alice/memories/" if api_key_type == "root" else "viking://~/memories/"
- assert client._memory_target_uri("alice") == expected
-
-
def test_viking_client_request_connection_uses_active_identity(monkeypatch):
monkeypatch.setattr(ov_server_module, "load_config", lambda: _make_config("root"))
@@ -1057,6 +1023,7 @@ async def test_commit_user_mode_uses_user_key_client_only(monkeypatch):
)
assert result["success"] is True
+ assert [instance.kwargs["api_key"] for instance in _DummyHTTPClient.instances] == ["user-key"]
@pytest.mark.asyncio
@@ -1185,20 +1152,7 @@ async def test_compact_hook_user_mode_commits_once(monkeypatch):
@pytest.mark.asyncio
-async def test_compact_hook_session_context_commits_single_session_with_peer_messages(monkeypatch):
- from vikingbot.hooks.builtins import openviking_hooks as hooks_module
-
- monkeypatch.setattr(
- hooks_module,
- "load_config",
- lambda: _make_config(
- "root",
- session_context_enabled=True,
- commit_token_threshold=100,
- commit_keep_recent_count=2,
- ),
- )
-
+async def test_compact_hook_session_context_commits_single_session_with_peer_messages(compact_hook):
class _FakeClient:
def __init__(self):
self.pending_tokens = [120, 0]
@@ -1242,12 +1196,7 @@ async def test_compact_hook_session_context_commits_single_session_with_peer_mes
return {"session_id": session_id, "status": "accepted"}
fake_client = _FakeClient()
- hook = OpenVikingCompactHook()
-
- async def _fake_get_client(_workspace_id):
- return fake_client
-
- monkeypatch.setattr(hook, "_get_client", _fake_get_client)
+ hook = compact_hook(fake_client, commit_token_threshold=100, commit_keep_recent_count=2)
session_key = SessionKey(
type="cli",
@@ -1336,52 +1285,9 @@ async def test_reset_openviking_state_replaces_persisted_sender_cursors(temp_dir
@pytest.mark.asyncio
-async def test_compact_hook_force_commit_does_not_resync_already_synced_messages(monkeypatch):
- from vikingbot.hooks.builtins import openviking_hooks as hooks_module
-
- monkeypatch.setattr(
- hooks_module,
- "load_config",
- lambda: _make_config(
- "root",
- session_context_enabled=True,
- commit_token_threshold=100,
- commit_keep_recent_count=2,
- ),
- )
-
- class _FakeClient:
- def __init__(self):
- self.append_calls = []
- self.commit_calls = []
-
- def session_owner_user_id(self):
- return "admin"
-
- async def append_messages(
- self,
- session_id,
- messages,
- default_user_peer_id=None,
- session_user_id=None,
- ):
- self.append_calls.append((session_id, [message["content"] for message in messages]))
- return {"session_id": session_id, "added": len(messages)}
-
- async def get_session(self, session_id, user_id=None):
- return {"session_id": session_id, "pending_tokens": 120}
-
- async def commit_session(self, session_id, keep_recent_count=0, user_id=None):
- self.commit_calls.append((session_id, keep_recent_count, user_id))
- return {"session_id": session_id, "status": "accepted"}
-
- fake_client = _FakeClient()
- hook = OpenVikingCompactHook()
-
- async def _fake_get_client(_workspace_id):
- return fake_client
-
- monkeypatch.setattr(hook, "_get_client", _fake_get_client)
+async def test_compact_hook_force_commit_does_not_resync_already_synced_messages(compact_hook):
+ fake_client = _SessionContextClient(pending_tokens=120, fail_commit=False)
+ hook = compact_hook(fake_client, commit_token_threshold=100, commit_keep_recent_turn_count=2)
context = HookContext(
event_type="message.compact",
@@ -1406,60 +1312,17 @@ async def test_compact_hook_force_commit_does_not_resync_already_synced_messages
assert result["success"] is True
assert fake_client.append_calls == [("cli__default__chat-1", ["new reply"])]
- assert fake_client.commit_calls == [("cli__default__chat-1", 2, "admin")]
+ assert fake_client.commit_calls == [("cli__default__chat-1", 0, "admin")]
assert session.metadata["openviking"]["last_synced_local_index"] == 1
assert session.metadata["openviking"]["last_commit_local_index"] == 1
@pytest.mark.asyncio
async def test_compact_hook_force_commit_commits_current_session_without_unsynced_messages(
- monkeypatch,
+ compact_hook,
):
- from vikingbot.hooks.builtins import openviking_hooks as hooks_module
-
- monkeypatch.setattr(
- hooks_module,
- "load_config",
- lambda: _make_config(
- "root",
- session_context_enabled=True,
- commit_token_threshold=1000,
- commit_keep_recent_count=2,
- ),
- )
-
- class _FakeClient:
- def __init__(self):
- self.append_calls = []
- self.commit_calls = []
-
- def session_owner_user_id(self):
- return "admin"
-
- async def append_messages(
- self,
- session_id,
- messages,
- default_user_peer_id=None,
- session_user_id=None,
- ):
- self.append_calls.append((session_id, [message["content"] for message in messages]))
- return {"session_id": session_id, "added": len(messages)}
-
- async def get_session(self, session_id, user_id=None):
- return {"session_id": session_id, "pending_tokens": 120}
-
- async def commit_session(self, session_id, keep_recent_count=0, user_id=None):
- self.commit_calls.append((session_id, keep_recent_count, user_id))
- return {"session_id": session_id, "status": "accepted"}
-
- fake_client = _FakeClient()
- hook = OpenVikingCompactHook()
-
- async def _fake_get_client(_workspace_id):
- return fake_client
-
- monkeypatch.setattr(hook, "_get_client", _fake_get_client)
+ fake_client = _SessionContextClient(pending_tokens=120, fail_commit=False)
+ hook = compact_hook(fake_client, commit_token_threshold=1000, commit_keep_recent_turn_count=2)
context = HookContext(
event_type="message.compact",
@@ -1484,61 +1347,16 @@ async def test_compact_hook_force_commit_commits_current_session_without_unsynce
assert result["success"] is True
assert fake_client.append_calls == []
- assert fake_client.commit_calls == [("cli__default__chat-1", 2, "admin")]
+ assert fake_client.commit_calls == [("cli__default__chat-1", 0, "admin")]
assert session.metadata["openviking"]["last_commit_performed"] is True
@pytest.mark.asyncio
async def test_compact_hook_session_context_append_failure_does_not_advance_sync_cursor(
- monkeypatch,
+ compact_hook,
):
- from vikingbot.hooks.builtins import openviking_hooks as hooks_module
-
- monkeypatch.setattr(
- hooks_module,
- "load_config",
- lambda: _make_config(
- "root",
- session_context_enabled=True,
- commit_token_threshold=100,
- commit_keep_recent_count=2,
- ),
- )
-
- class _FakeClient:
- def __init__(self):
- self.append_calls = []
- self.commit_calls = []
-
- def session_owner_user_id(self):
- return "admin"
-
- async def append_messages(
- self,
- session_id,
- messages,
- default_user_peer_id=None,
- session_user_id=None,
- ):
- self.append_calls.append((session_id, [message["content"] for message in messages]))
- if session_id == "cli__default__chat-1":
- raise RuntimeError("session append failed")
- return {"session_id": session_id, "added": len(messages)}
-
- async def get_session(self, session_id, user_id=None):
- return {"session_id": session_id, "pending_tokens": 120}
-
- async def commit_session(self, session_id, keep_recent_count=0, user_id=None):
- self.commit_calls.append((session_id, keep_recent_count, user_id))
- return {"session_id": session_id, "status": "accepted"}
-
- fake_client = _FakeClient()
- hook = OpenVikingCompactHook()
-
- async def _fake_get_client(_workspace_id):
- return fake_client
-
- monkeypatch.setattr(hook, "_get_client", _fake_get_client)
+ fake_client = _SessionContextClient(pending_tokens=120, fail_append=True)
+ hook = compact_hook(fake_client, commit_token_threshold=100, commit_keep_recent_count=2)
context = HookContext(
event_type="message.compact",
@@ -1570,53 +1388,10 @@ async def test_compact_hook_session_context_append_failure_does_not_advance_sync
@pytest.mark.asyncio
async def test_compact_hook_session_context_commits_when_message_threshold_reached(
- monkeypatch,
+ compact_hook,
):
- from vikingbot.hooks.builtins import openviking_hooks as hooks_module
-
- monkeypatch.setattr(
- hooks_module,
- "load_config",
- lambda: _make_config(
- "root",
- session_context_enabled=True,
- commit_token_threshold=1000,
- commit_keep_recent_count=2,
- ),
- )
-
- class _FakeClient:
- def __init__(self):
- self.append_calls = []
- self.commit_calls = []
-
- def session_owner_user_id(self):
- return "admin"
-
- async def append_messages(
- self,
- session_id,
- messages,
- default_user_peer_id=None,
- session_user_id=None,
- ):
- self.append_calls.append((session_id, [message["content"] for message in messages]))
- return {"session_id": session_id, "added": len(messages)}
-
- async def get_session(self, session_id, user_id=None):
- return {"session_id": session_id, "pending_tokens": 0}
-
- async def commit_session(self, session_id, keep_recent_count=0, user_id=None):
- self.commit_calls.append((session_id, keep_recent_count, user_id))
- return {"session_id": session_id, "status": "accepted"}
-
- fake_client = _FakeClient()
- hook = OpenVikingCompactHook()
-
- async def _fake_get_client(_workspace_id):
- return fake_client
-
- monkeypatch.setattr(hook, "_get_client", _fake_get_client)
+ fake_client = _SessionContextClient(pending_tokens=0, fail_commit=False)
+ hook = compact_hook(fake_client, commit_token_threshold=1000, commit_keep_recent_turn_count=2)
context = HookContext(
event_type="message.compact",
@@ -1644,62 +1419,16 @@ async def test_compact_hook_session_context_commits_when_message_threshold_reach
assert result["success"] is True
assert result["admin_result"]["committed"] is True
assert fake_client.append_calls == [("cli__default__chat-1", ["m3"])]
- assert fake_client.commit_calls == [("cli__default__chat-1", 2, "admin")]
+ assert fake_client.commit_calls == [("cli__default__chat-1", 0, "admin")]
assert session.metadata["openviking"]["last_commit_local_index"] == 2
@pytest.mark.asyncio
async def test_compact_hook_session_commit_failure_retries_without_resyncing_messages(
- monkeypatch,
+ compact_hook,
):
- from vikingbot.hooks.builtins import openviking_hooks as hooks_module
-
- monkeypatch.setattr(
- hooks_module,
- "load_config",
- lambda: _make_config(
- "root",
- session_context_enabled=True,
- commit_token_threshold=100,
- commit_keep_recent_count=2,
- ),
- )
-
- class _FakeClient:
- def __init__(self):
- self.append_calls = []
- self.commit_calls = []
- self.fail_session_commit = True
-
- def session_owner_user_id(self):
- return "admin"
-
- async def append_messages(
- self,
- session_id,
- messages,
- default_user_peer_id=None,
- session_user_id=None,
- ):
- self.append_calls.append((session_id, [message["content"] for message in messages]))
- return {"session_id": session_id, "added": len(messages)}
-
- async def get_session(self, session_id, user_id=None):
- return {"session_id": session_id, "pending_tokens": 120}
-
- async def commit_session(self, session_id, keep_recent_count=0, user_id=None):
- self.commit_calls.append((session_id, keep_recent_count, user_id))
- if session_id == "cli__default__chat-1" and self.fail_session_commit:
- raise RuntimeError("session commit failed")
- return {"session_id": session_id, "status": "accepted"}
-
- fake_client = _FakeClient()
- hook = OpenVikingCompactHook()
-
- async def _fake_get_client(_workspace_id):
- return fake_client
-
- monkeypatch.setattr(hook, "_get_client", _fake_get_client)
+ fake_client = _SessionContextClient(pending_tokens=120, fail_commit=True)
+ hook = compact_hook(fake_client, commit_token_threshold=100, commit_keep_recent_turn_count=2)
context = HookContext(
event_type="message.compact",
@@ -1719,7 +1448,7 @@ async def test_compact_hook_session_commit_failure_retries_without_resyncing_mes
assert first["success"] is False
assert "session commit failed" in first["error"]
assert fake_client.append_calls == [("cli__default__chat-1", ["u1 asks", "u1 reply"])]
- assert fake_client.commit_calls == [("cli__default__chat-1", 2, "admin")]
+ assert fake_client.commit_calls == [("cli__default__chat-1", 0, "admin")]
state = session.metadata["openviking"]
assert state["last_sync_status"] == "error"
assert state["last_synced_local_index"] == 1
@@ -1733,59 +1462,16 @@ async def test_compact_hook_session_commit_failure_retries_without_resyncing_mes
assert second["success"] is True
assert fake_client.append_calls == []
- assert fake_client.commit_calls == [("cli__default__chat-1", 2, "admin")]
+ assert fake_client.commit_calls == [("cli__default__chat-1", 0, "admin")]
assert session.metadata["openviking"]["last_commit_performed"] is True
@pytest.mark.asyncio
async def test_compact_hook_session_context_skips_message_threshold_after_recent_commit(
- monkeypatch,
+ compact_hook,
):
- from vikingbot.hooks.builtins import openviking_hooks as hooks_module
-
- monkeypatch.setattr(
- hooks_module,
- "load_config",
- lambda: _make_config(
- "root",
- session_context_enabled=True,
- commit_token_threshold=1000,
- commit_keep_recent_count=2,
- ),
- )
-
- class _FakeClient:
- def __init__(self):
- self.append_calls = []
- self.commit_calls = []
-
- def session_owner_user_id(self):
- return "admin"
-
- async def append_messages(
- self,
- session_id,
- messages,
- default_user_peer_id=None,
- session_user_id=None,
- ):
- self.append_calls.append((session_id, [message["content"] for message in messages]))
- return {"session_id": session_id, "added": len(messages)}
-
- async def get_session(self, session_id, user_id=None):
- return {"session_id": session_id, "pending_tokens": 0}
-
- async def commit_session(self, session_id, keep_recent_count=0, user_id=None):
- self.commit_calls.append((session_id, keep_recent_count, user_id))
- return {"session_id": session_id, "status": "accepted"}
-
- fake_client = _FakeClient()
- hook = OpenVikingCompactHook()
-
- async def _fake_get_client(_workspace_id):
- return fake_client
-
- monkeypatch.setattr(hook, "_get_client", _fake_get_client)
+ fake_client = _SessionContextClient(pending_tokens=0)
+ hook = compact_hook(fake_client, commit_token_threshold=1000, commit_keep_recent_count=2)
context = HookContext(
event_type="message.compact",
@@ -2058,23 +1744,6 @@ async def test_search_memory_peer_ids_use_explicit_peer_uris(monkeypatch):
]
-async def test_skill_memory_uri_uses_user_memory_namespace(monkeypatch):
- monkeypatch.setattr(ov_server_module, "load_config", lambda: _make_config("root"))
- client = VikingClient()
-
- assert (
- client._skill_memory_uri("planner", "admin")
- == "viking://user/admin/memories/skills/planner.md"
- )
-
-
-def test_openviking_grep_schema_requires_single_string_pattern():
- tool = VikingGrepTool()
-
- assert tool.parameters["properties"]["pattern"]["type"] == "string"
-
-
@pytest.mark.asyncio
async def test_openviking_grep_keeps_explicit_resource_uri(monkeypatch):
tool = VikingGrepTool()
@@ -2112,28 +1781,6 @@ async def test_openviking_grep_keeps_explicit_resource_uri(monkeypatch):
assert "viking://resources/doc.md" in result
-def test_openviking_tool_memory_peer_ids_exclude_legacy_memory_users():
- tool = VikingSearchTool()
-
- peer_ids = tool._memory_peer_ids(
- SimpleNamespace(
- sender_id="sender-1",
- memory_peer_ids=["speaker-a"],
- memory_user_ids=["legacy-user"],
- )
- )
-
- assert peer_ids == ["sender-1", "speaker-a"]
-
-
-def test_tool_context_syncs_legacy_memory_user_alias():
- from_legacy = ToolContext(memory_user_ids=["legacy-user"])
- from_owner = ToolContext(memory_owner_user_ids=["owner-user"])
-
- assert from_legacy.memory_owner_user_ids == ["legacy-user"]
- assert from_owner.memory_user_ids == ["owner-user"]
-
-
@pytest.mark.asyncio
async def test_viking_memory_context_keeps_legacy_users_separate_from_peers(monkeypatch, tmp_path):
calls = []
@@ -2285,7 +1932,7 @@ async def test_viking_peer_profiles_use_target_peer_actor_clients(monkeypatch, t
monkeypatch.setattr("vikingbot.agent.memory.VikingClient.create", _fake_create)
- store = MemoryStore(tmp_path)
+ store = MemoryStore(tmp_path, config=_make_config("user"))
result = await store.get_viking_peer_profiles(
workspace_id="workspace",
peer_ids=["speaker-a", "speaker-b"],
@@ -2909,97 +2556,6 @@ async def test_openviking_glob_root_uses_namespaced_self_targets_for_root_key(mo
]
-def test_openviking_search_description_allows_follow_up_memory_queries():
- description = VikingSearchTool().description
-
- assert "follow-up" in description
- assert "different remembered fact" in description
- assert "before concluding no relevant record exists" in description
- assert "avoid repeated calls with similar queries" not in description.lower()
-
-
-async def test_context_reminds_agent_to_search_current_memory_question(tmp_path):
- class _EmptyMemory:
- async def get_viking_experience_context(self, **_kwargs):
- return ""
-
- async def get_viking_memory_context(self, **_kwargs):
- return ""
-
- context = ContextBuilder(workspace=tmp_path, sender_id="sender-1")
- context._memory = _EmptyMemory()
-
- user_info = await context._build_user_memory(
- session_key=SessionKey(type="cli", channel_id="default", chat_id="chat-1"),
- current_message="我会哪些语言",
- sender_id="sender-1",
- ov_tools_enable=True,
- )
-
- assert "OpenViking Memory Retrieval" in user_info
- assert "use openviking_search for the current question" in user_info
- assert "search again when the requested fact changes" in user_info
- assert "grouped by memory_type" in user_info
- assert "events contain atomic time-based facts" in user_info
- assert "entities contain stable topic/entity facts" in user_info
- assert "preferences contain likes, habits, and recurring tendencies" in user_info
- assert "full means the full memory content is already shown" in user_info
- assert "summary means only a summary is shown" in user_info
- assert "uri means only the URI is shown" in user_info
- assert "openviking_multi_read" in user_info
-
-
-async def test_context_memory_prefix_tells_agent_to_read_summary_and_uri_details(tmp_path):
- calls = []
-
- class _Memory:
- async def get_viking_experience_context(self, **_kwargs):
- return ""
-
- async def get_viking_memory_context(self, **kwargs):
- calls.append(kwargs)
- return (
- "### user memories:\n"
- '<memory index="1" type="summary">\n'
- " <uri>viking://user/default/peers/sender-1/memories/events/e.md</uri>\n"
- " <summary>important clue</summary>\n"
- "</memory>"
- )
-
- context = ContextBuilder(workspace=tmp_path, sender_id="sender-1")
- context._memory = _Memory()
-
- user_info = await context._build_user_memory(
- session_key=SessionKey(type="cli", channel_id="default", chat_id="chat-1"),
- current_message="问题",
- sender_id="sender-1",
- ov_tools_enable=True,
- memory_peer_ids=["peer-1"],
- memory_owner_user_ids=["owner-1"],
- )
-
- assert calls == [
- {
- "current_message": "问题",
- "workspace_id": "cli__default__chat-1",
- "sender_id": "sender-1",
- "peer_ids": ["peer-1"],
- "user_ids": ["owner-1"],
- "openviking_connection": None,
- }
- ]
- assert context.latest_relevant_memories
- assert "## openviking_search(query=[user_query])" in user_info
- assert "grouped by memory_type" in user_info
- assert "full means the full memory content is already shown" in user_info
- assert "summary means only a summary is shown" in user_info
- assert "uri means only the URI is shown" in user_info
- assert "openviking_multi_read" in user_info
- assert "important clue" in user_info
-
-
@pytest.mark.asyncio
async def test_context_loads_profiles_for_memory_peers(tmp_path):
calls = {"sender": [], "peers": []}
@@ -3253,226 +2809,3 @@ async def test_experience_reminder_follows_direct_case_experience_links(monkeypa
assert "direct exp content" not in content
assert exp_uri in uris
assert direct_exp_uri not in uris
-
-
-async def test_tau2_experience_reminder_loads_case_by_exact_task_not_query(monkeypatch, tmp_path):
- from openviking.session.memory.dataclass import MemoryFile, StoredLink
- from openviking.session.memory.utils.memory_file_utils import MemoryFileUtils
-
- matched_case_uri = "viking://user/admin/memories/cases/tau2_airline_train_1.md"
- wrong_case_uri = "viking://user/admin/memories/cases/tau2_airline_train_9.md"
- matched_exp_uri = "viking://user/admin/memories/experiences/matched.md"
- wrong_exp_uri = "viking://user/admin/memories/experiences/wrong.md"
-
- def _case_content(case_uri, exp_uri, *, task_no, task_id):
- return MemoryFileUtils.write(
- MemoryFile(
- uri=case_uri,
- content="",
- memory_type="cases",
- extra_fields={
- "case_name": case_uri.rsplit("/", 1)[-1].removesuffix(".md"),
- "task_signature": f"tau2:airline:train:{task_id}",
- "input": json.dumps(
- {
- "domain": "airline",
- "split": "train",
- "data_split": "airline_train",
- "task_no": task_no,
- "task_id": str(task_id),
- }
- ),
- },
- links=[
- StoredLink(
- from_uri=case_uri,
- to_uri=exp_uri,
- link_type="related_to",
- weight=1.0,
- match_text=None,
- description="",
- ).model_dump()
- ],
- ),
- content_template=(
- "# {{ case_name }}\n\n"
- "## Task Signature\n{{ task_signature }}\n\n"
- "## Input\n{{ input }}\n\n"
- "## Linked Experiences\n"
- "{% for link in links or [] %}"
- "- [{{ uri_basename(link.to_uri) }}]({{ link_target(link.to_uri) }})\n"
- "{% endfor %}"
- ),
- )
-
- raw_cases = {
- matched_case_uri: _case_content(matched_case_uri, matched_exp_uri, task_no=1, task_id=7),
- wrong_case_uri: _case_content(wrong_case_uri, wrong_exp_uri, task_no=9, task_id=99),
- }
- visible_cases = {
- uri: raw.split("<!-- MEMORY_FIELDS", 1)[0].strip() for uri, raw in raw_cases.items()
- }
-
- class _FakeClient:
- admin_user_id = "admin"
-
- def __init__(self):
- self.search_calls = []
- self.read_calls = []
-
- async def search(self, *, query, target_uri, limit):
- self.search_calls.append((query, target_uri, limit))
- return {"memories": [{"uri": wrong_case_uri, "score": 0.99}]}
-
- async def read_content(self, uri, level="read"):
- self.read_calls.append((uri, level))
- if uri in visible_cases:
- return visible_cases[uri]
- if uri == matched_exp_uri:
- return "matched exp content"
- if uri == wrong_exp_uri:
- return "wrong exp content"
- return ""
-
- async def close(self):
- return None
-
- fake_client = _FakeClient()
-
- async def _fake_create(**_kwargs):
- return fake_client
-
- monkeypatch.setattr(
- "vikingbot.agent.memory.load_config",
- lambda: _make_config("root", case_recall_limit=3, exp_recall_max_chars=4000),
- )
- monkeypatch.setattr("vikingbot.agent.memory.VikingClient.create", _fake_create)
-
- content, uris = await MemoryStore(tmp_path).get_viking_experience_reminder(
- query="same customer cancellation text could match wrong case",
- workspace_id="workspace",
- case_lookup={
- "benchmark": "tau2",
- "domain": "airline",
- "split": "train",
- "data_split": "airline_train",
- "task_no": 1,
- "task_id": "7",
- "case_name": "tau2_airline_train_1",
- "task_signature": "tau2:airline:train:7",
- "strict": True,
- },
- )
-
- assert fake_client.search_calls == []
- assert (matched_case_uri, "read") in fake_client.read_calls
- assert (wrong_case_uri, "read") not in fake_client.read_calls
- assert "matched exp content" in content
- assert "wrong exp content" not in content
- assert uris == [matched_exp_uri]
-
-
-async def test_tau2_experience_reminder_returns_empty_when_exact_case_mismatches(
- monkeypatch, tmp_path
-):
- from openviking.session.memory.dataclass import MemoryFile, StoredLink
- from openviking.session.memory.utils.memory_file_utils import MemoryFileUtils
-
- case_uri = "viking://user/admin/memories/cases/tau2_airline_train_1.md"
- exp_uri = "viking://user/admin/memories/experiences/wrong.md"
- raw_case = MemoryFileUtils.write(
- MemoryFile(
- uri=case_uri,
- content="",
- memory_type="cases",
- extra_fields={
- "case_name": "tau2_airline_train_1",
- "task_signature": "tau2:airline:train:99",
- "input": json.dumps(
- {
- "domain": "airline",
- "split": "train",
- "data_split": "airline_train",
- "task_no": 1,
- "task_id": "99",
- }
- ),
- },
- links=[
- StoredLink(
- from_uri=case_uri,
- to_uri=exp_uri,
- link_type="related_to",
- weight=1.0,
- match_text=None,
- description="",
- ).model_dump()
- ],
- ),
- content_template=(
- "# {{ case_name }}\n\n"
- "## Task Signature\n{{ task_signature }}\n\n"
- "## Input\n{{ input }}\n\n"
- "## Linked Experiences\n"
- "{% for link in links or [] %}"
- "- [{{ uri_basename(link.to_uri) }}]({{ link_target(link.to_uri) }})\n"
- "{% endfor %}"
- ),
- )
- visible_case = raw_case.split("<!-- MEMORY_FIELDS", 1)[0].strip()
-
- class _FakeClient:
- admin_user_id = "admin"
-
- def __init__(self):
- self.search_calls = []
- self.read_calls = []
-
- async def search(self, *, query, target_uri, limit):
- self.search_calls.append((query, target_uri, limit))
- return {"memories": [{"uri": case_uri, "score": 0.99}]}
-
- async def read_content(self, uri, level="read"):
- self.read_calls.append((uri, level))
- if uri == case_uri:
- return visible_case
- if uri == exp_uri:
- return "wrong exp content"
- return ""
-
- async def close(self):
- return None
-
- fake_client = _FakeClient()
-
- async def _fake_create(**_kwargs):
- return fake_client
-
- monkeypatch.setattr(
- "vikingbot.agent.memory.load_config",
- lambda: _make_config("root", case_recall_limit=3, exp_recall_max_chars=4000),
- )
- monkeypatch.setattr("vikingbot.agent.memory.VikingClient.create", _fake_create)
-
- content, uris = await MemoryStore(tmp_path).get_viking_experience_reminder(
- query="query would retrieve wrong case",
- workspace_id="workspace",
- case_lookup={
- "benchmark": "tau2",
- "domain": "airline",
- "split": "train",
- "data_split": "airline_train",
- "task_no": 1,
- "task_id": "7",
- "case_name": "tau2_airline_train_1",
- "task_signature": "tau2:airline:train:7",
- "strict": True,
- },
- )
-
- assert fake_client.search_calls == []
- assert (case_uri, "read") in fake_client.read_calls
- assert content == ""
- assert uris == []
diff --git a/bot/vikingbot/tests/unit/test_openviking_peer_identity.py b/bot/tests/test_openviking_peer_identity.py
similarity index 96%
rename from bot/vikingbot/tests/unit/test_openviking_peer_identity.py
rename to bot/tests/test_openviking_peer_identity.py
index 458e79683..e53106f1b 100644
--- a/bot/vikingbot/tests/unit/test_openviking_peer_identity.py
+++ b/bot/tests/test_openviking_peer_identity.py
@@ -1,22 +1,5 @@
-import sys
-import types
-from pathlib import Path
-
import pytest
-
-sys.path.insert(0, str(Path(__file__).resolve().parents[4]))
-
-try:
- import vikingbot.config.loader # noqa: F401
-except Exception:
- config_module = types.ModuleType("vikingbot.config")
- loader_module = types.ModuleType("vikingbot.config.loader")
- loader_module.load_config = lambda: None
- config_module.load_config = loader_module.load_config
- sys.modules.setdefault("vikingbot.config", config_module)
- sys.modules.setdefault("vikingbot.config.loader", loader_module)
-
-from vikingbot.openviking_mount.ov_server import VikingClient # noqa: E402
+from vikingbot.openviking_mount.ov_server import VikingClient
TELEGRAM_ALICE_PEER_ID = "ext-dGVsZWdyYW06YWxpY2U"
diff --git a/bot/vikingbot/tests/unit/test_openviking_session_retention.py b/bot/tests/test_openviking_session_retention.py
similarity index 94%
rename from bot/vikingbot/tests/unit/test_openviking_session_retention.py
rename to bot/tests/test_openviking_session_retention.py
index 733ee225f..3310e8030 100644
--- a/bot/vikingbot/tests/unit/test_openviking_session_retention.py
+++ b/bot/tests/test_openviking_session_retention.py
@@ -1,7 +1,6 @@
from types import SimpleNamespace
import pytest
-
from vikingbot.agent.loop import AgentLoop
from vikingbot.config.schema import SessionKey
from vikingbot.hooks.base import HookContext
@@ -115,9 +114,5 @@ def test_prompt_history_consumes_overview_checkpoint_and_raw_tail_in_order():
"[Earlier conversation summary]\nEarlier turns were compacted."
)
assert history[1]["content"] == "Investigate the outage"
- assert history[2]["content"] == (
- "Checked early signals and confirmed pool saturation."
- )
- assert history[3]["content"] == (
- "I will verify the recovery setting.\nrecovery_timeout=30"
- )
+ assert history[2]["content"] == ("Checked early signals and confirmed pool saturation.")
+ assert history[3]["content"] == ("I will verify the recovery setting.\nrecovery_timeout=30")
diff --git a/bot/tests/test_outcome_evaluator.py b/bot/tests/test_outcome_evaluator.py
index a6332514d..4ab43fb8a 100644
--- a/bot/tests/test_outcome_evaluator.py
+++ b/bot/tests/test_outcome_evaluator.py
@@ -1,228 +1,80 @@
from datetime import datetime
+import pytest
from vikingbot.observability.outcome import evaluate_response_outcome
-def test_evaluate_response_outcome_marks_resolved_without_follow_up():
- evaluation = evaluate_response_outcome(
- [
- {
- "role": "assistant",
- "content": "hello",
- "response_id": "resp-1",
- "timestamp": "2026-04-30T00:00:00",
- }
- ],
- "resp-1",
- now=datetime.fromisoformat("2026-04-30T00:05:00"),
- )
-
- assert evaluation is not None
- assert evaluation.outcome_label == "resolved"
- assert evaluation.resolved_in_one_turn is True
- assert evaluation.reask_within_10m is False
- assert evaluation.clarification_turns == 0
- assert evaluation.follow_up_without_feedback is False
-
-
-def test_evaluate_response_outcome_prefers_positive_feedback():
- evaluation = evaluate_response_outcome(
- [
- {
- "role": "assistant",
- "content": "hello",
- "response_id": "resp-1",
- "timestamp": "2026-04-30T00:00:00",
- },
+ "feedback, follow_up_minute, expected",
+ [
+ pytest.param(None, None, ("resolved", True, False, 0, False), id="resolved"),
+ pytest.param(
+ {"feedback_type": "thumb_up"},
+ 1,
+ ("positive_feedback", True, False, 0, False),
+ id="positive-feedback",
+ ),
+ pytest.param(
+ {"feedback_type": "thumb_down"},
+ None,
+ ("negative_feedback", False, False, 0, False),
+ id="negative-feedback",
+ ),
+ pytest.param(
+ {"feedback_type": "rating", "feedback_score": 1},
+ 1,
+ ("positive_feedback", True, False, 0, False),
+ id="positive-rating",
+ ),
+ pytest.param(
+ {"feedback_type": "rating", "feedback_score": -1},
+ None,
+ ("negative_feedback", False, False, 0, False),
+ id="negative-rating",
+ ),
+ pytest.param(
+ {"feedback_type": "rating", "feedback_score": 0},
+ None,
+ ("resolved", True, False, 0, False),
+ id="neutral-rating",
+ ),
+ pytest.param(None, 5, ("reasked", False, True, 1, False), id="reasked"),
+ pytest.param(
+ None, 20, ("follow_up_without_feedback", False, False, 1, True), id="later-follow-up"
+ ),
+ ],
+)
+def test_response_outcome(feedback, follow_up_minute, expected):
+ messages = [
+ {
+ "role": "assistant",
+ "content": "hello",
+ "response_id": "resp-1",
+ "timestamp": "2026-04-30T00:00:00",
+ }
+ ]
+ if follow_up_minute is not None:
+ messages.append(
{
"role": "user",
- "content": "thanks",
- "timestamp": "2026-04-30T00:01:00",
- },
- ],
- "resp-1",
- feedback_events=[
- {
- "response_id": "resp-1",
- "feedback_type": "thumb_up",
+ "content": "follow-up",
+ "timestamp": f"2026-04-30T00:{follow_up_minute:02d}:00",
}
- ],
- now=datetime.fromisoformat("2026-04-30T00:02:00"),
- )
-
- assert evaluation is not None
- assert evaluation.outcome_label == "positive_feedback"
- assert evaluation.resolved_in_one_turn is True
- assert evaluation.reask_within_10m is False
- assert evaluation.clarification_turns == 0
- assert evaluation.follow_up_without_feedback is False
-
-
-def test_evaluate_response_outcome_marks_negative_feedback():
+ )
+ events = [{"response_id": "resp-1", **feedback}] if feedback is not None else []
evaluation = evaluate_response_outcome(
- [
- {
- "role": "assistant",
- "content": "hello",
- "response_id": "resp-1",
- "timestamp": "2026-04-30T00:00:00",
- }
- ],
+ messages,
"resp-1",
- feedback_events=[
- {
- "response_id": "resp-1",
- "feedback_type": "thumb_down",
- }
- ],
- now=datetime.fromisoformat("2026-04-30T00:02:00"),
+ feedback_events=events,
+ now=datetime(2026, 4, 30, 0, (follow_up_minute or 1) + 1),
)
-
- assert evaluation is not None
- assert evaluation.outcome_label == "negative_feedback"
- assert evaluation.resolved_in_one_turn is False
- assert evaluation.follow_up_without_feedback is False
-
-
-def test_evaluate_response_outcome_maps_positive_rating_to_positive_feedback():
- evaluation = evaluate_response_outcome(
- [
- {
- "role": "assistant",
- "content": "hello",
- "response_id": "resp-1",
- "timestamp": "2026-04-30T00:00:00",
- },
- {
- "role": "user",
- "content": "thanks",
- "timestamp": "2026-04-30T00:01:00",
- },
- ],
- "resp-1",
- feedback_events=[
- {
- "response_id": "resp-1",
- "feedback_type": "rating",
- "feedback_score": 1,
- }
- ],
- now=datetime.fromisoformat("2026-04-30T00:02:00"),
- )
-
- assert evaluation is not None
- assert evaluation.outcome_label == "positive_feedback"
- assert evaluation.resolved_in_one_turn is True
- assert evaluation.reask_within_10m is False
- assert evaluation.clarification_turns == 0
- assert evaluation.follow_up_without_feedback is False
- assert evaluation.evidence["feedback_score"] == 1.0
-
-
-def test_evaluate_response_outcome_maps_negative_rating_to_negative_feedback():
- evaluation = evaluate_response_outcome(
- [
- {
- "role": "assistant",
- "content": "hello",
- "response_id": "resp-1",
- "timestamp": "2026-04-30T00:00:00",
- }
- ],
- "resp-1",
- feedback_events=[
- {
- "response_id": "resp-1",
- "feedback_type": "rating",
- "feedback_score": -1,
- }
- ],
- now=datetime.fromisoformat("2026-04-30T00:02:00"),
- )
-
- assert evaluation is not None
- assert evaluation.outcome_label == "negative_feedback"
- assert evaluation.resolved_in_one_turn is False
- assert evaluation.follow_up_without_feedback is False
- assert evaluation.evidence["feedback_score"] == -1.0
-
-
-def test_evaluate_response_outcome_keeps_heuristic_outcome_for_neutral_rating():
- evaluation = evaluate_response_outcome(
- [
- {
- "role": "assistant",
- "content": "hello",
- "response_id": "resp-1",
- "timestamp": "2026-04-30T00:00:00",
- }
- ],
- "resp-1",
- feedback_events=[
- {
- "response_id": "resp-1",
- "feedback_type": "rating",
- "feedback_score": 0,
- }
- ],
- now=datetime.fromisoformat("2026-04-30T00:02:00"),
- )
-
- assert evaluation is not None
- assert evaluation.outcome_label == "resolved"
- assert evaluation.resolved_in_one_turn is True
- assert evaluation.evidence["feedback_score"] == 0.0
-
-
-def test_evaluate_response_outcome_marks_reasked_within_window():
- evaluation = evaluate_response_outcome(
- [
- {
- "role": "assistant",
- "content": "hello",
- "response_id": "resp-1",
- "timestamp": "2026-04-30T00:00:00",
- },
- {
- "role": "user",
- "content": "that did not help",
- "timestamp": "2026-04-30T00:05:00",
- },
- ],
- "resp-1",
- now=datetime.fromisoformat("2026-04-30T00:06:00"),
- )
-
- assert evaluation is not None
- assert evaluation.outcome_label == "reasked"
- assert evaluation.resolved_in_one_turn is False
- assert evaluation.reask_within_10m is True
- assert evaluation.clarification_turns == 1
- assert evaluation.follow_up_without_feedback is False
-
-
-def test_evaluate_response_outcome_marks_follow_up_without_feedback():
- evaluation = evaluate_response_outcome(
- [
- {
- "role": "assistant",
- "content": "hello",
- "response_id": "resp-1",
- "timestamp": "2026-04-30T00:00:00",
- },
- {
- "role": "user",
- "content": "another question later",
- "timestamp": "2026-04-30T00:20:00",
- },
- ],
- "resp-1",
- now=datetime.fromisoformat("2026-04-30T00:21:00"),
- )
-
assert evaluation is not None
- assert evaluation.outcome_label == "follow_up_without_feedback"
- assert evaluation.resolved_in_one_turn is False
- assert evaluation.reask_within_10m is False
- assert evaluation.clarification_turns == 1
- assert evaluation.follow_up_without_feedback is True
+ assert (
+ evaluation.outcome_label,
+ evaluation.resolved_in_one_turn,
+ evaluation.reask_within_10m,
+ evaluation.clarification_turns,
+ evaluation.follow_up_without_feedback,
+ ) == expected
+ if feedback is not None and "feedback_score" in feedback:
+ assert evaluation.evidence["feedback_score"] == feedback["feedback_score"]
diff --git a/bot/tests/test_sandbox_file_access.py b/bot/tests/test_sandbox_file_access.py
index 95157fdc6..31ed70522 100644
--- a/bot/tests/test_sandbox_file_access.py
+++ b/bot/tests/test_sandbox_file_access.py
@@ -3,9 +3,8 @@
"""Regression tests for bounded local and remote sandbox file access."""
import json
-import sys
from pathlib import Path
-from types import ModuleType, SimpleNamespace
+from types import SimpleNamespace
import pytest
from vikingbot.sandbox.backends.aiosandbox import AioSandboxBackend
@@ -48,23 +47,9 @@ async def test_local_workspace_listing_and_reads_are_bounded(tmp_path: Path):
class _AioFileClient:
def __init__(self, *, truncated=False):
- self.list_calls = []
self.glob_calls = []
self.download_calls = []
self.truncated = truncated
- self.entries = {
- "/home/gem": [
- SimpleNamespace(name="artifact.bin", is_directory=False, size=6),
- SimpleNamespace(name="nested", is_directory=True, size=0),
- ],
- "/home/gem/nested": [
- SimpleNamespace(name="page.md", is_directory=False, size=4),
- ],
- }
-
- async def list_path(self, **kwargs):
- self.list_calls.append(kwargs)
- return SimpleNamespace(data=SimpleNamespace(files=self.entries[kwargs["path"]]))
async def glob_files(self, **kwargs):
self.glob_calls.append(kwargs)
@@ -189,27 +174,11 @@ class _OpenSandboxCommands:
)
-class _RunCommandOpts(SimpleNamespace):
- pass
-
-
def _opensandbox_vke_backend(
tmp_path: Path,
file_client: _OpenSandboxFiles,
commands: _OpenSandboxCommands,
) -> OpenSandboxBackend:
- if "opensandbox.models.execd" not in sys.modules:
- opensandbox_module = sys.modules.setdefault("opensandbox", ModuleType("opensandbox"))
- models_module = sys.modules.setdefault(
- "opensandbox.models",
- ModuleType("opensandbox.models"),
- )
- execd_module = ModuleType("opensandbox.models.execd")
- execd_module.RunCommandOpts = _RunCommandOpts
- opensandbox_module.models = models_module
- models_module.execd = execd_module
- sys.modules["opensandbox.models.execd"] = execd_module
-
backend = object.__new__(OpenSandboxBackend)
backend._workspace = tmp_path
backend._is_vke = True
diff --git a/bot/tests/test_sandbox_startup.py b/bot/tests/test_sandbox_startup.py
index cf799902a..d6d5e57a5 100644
--- a/bot/tests/test_sandbox_startup.py
+++ b/bot/tests/test_sandbox_startup.py
@@ -3,17 +3,12 @@
"""Regression tests for sandbox construction and startup failures."""
import json
-import sys
-from pathlib import Path
import pytest
-
-sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
-
-from vikingbot.config.schema import Config, SessionKey # noqa: E402
-from vikingbot.sandbox.backends.srt import SrtBackend # noqa: E402
-from vikingbot.sandbox.manager import SandboxManager # noqa: E402
-from vikingbot.utils.session_paths import portable_path_component # noqa: E402
+from vikingbot.config.schema import Config, SessionKey
+from vikingbot.sandbox.backends.srt import SrtBackend
+from vikingbot.sandbox.manager import SandboxManager
+from vikingbot.utils.session_paths import portable_path_component
def test_srt_backend_uses_workspace_id_and_nested_config(tmp_path):
@@ -30,26 +25,37 @@ def test_srt_backend_uses_workspace_id_and_nested_config(tmp_path):
@pytest.mark.asyncio
async def test_startup_failure_is_not_cached_and_can_retry(tmp_path):
- class FailingBackend:
+ class RetryBackend:
instances = []
def __init__(self, config, workspace_id, workspace):
self.stopped = False
+ self.started = False
+ self.workspace = workspace
self.instances.append(self)
async def start(self):
- raise RuntimeError("startup failed")
+ if len(self.instances) == 1:
+ raise RuntimeError("startup failed")
+ self.workspace.mkdir(parents=True, exist_ok=True)
+ self.started = True
async def stop(self):
self.stopped = True
manager = SandboxManager(Config(), tmp_path / "sandboxes", tmp_path / "source")
- manager._backend_cls = FailingBackend
+ manager._backend_cls = RetryBackend
session_key = SessionKey(type="cli", channel_id="default", chat_id="test")
- for expected_attempts in (1, 2):
- with pytest.raises(RuntimeError, match="startup failed"):
- await manager.get_sandbox(session_key)
- assert manager._sandboxes == {}
- assert len(FailingBackend.instances) == expected_attempts
- assert FailingBackend.instances[-1].stopped
+ with pytest.raises(RuntimeError, match="startup failed"):
+ await manager.get_sandbox(session_key)
+ assert RetryBackend.instances[0].stopped
+
+ try:
+ recovered = await manager.get_sandbox(session_key)
+ assert recovered.started
+ assert len(RetryBackend.instances) == 2
+ assert await manager.get_sandbox(session_key) is recovered
+ finally:
+ await manager.cleanup_all()
+ assert recovered.stopped
diff --git a/bot/tests/test_send_uri_traversal.py b/bot/tests/test_send_uri_traversal.py
index ce5f7e380..3d4378e9d 100644
--- a/bot/tests/test_send_uri_traversal.py
+++ b/bot/tests/test_send_uri_traversal.py
@@ -10,7 +10,6 @@ files and channel image delivery would exfiltrate them.
from pathlib import Path
import pytest
-
from vikingbot.channels.base import BaseChannel
@@ -43,9 +42,7 @@ def images_dir(tmp_path: Path, monkeypatch) -> Path:
import vikingbot.channels.base as base_mod
- monkeypatch.setattr(
- base_mod, "get_data_path", lambda: (tmp_path / "data")
- )
+ monkeypatch.setattr(base_mod, "get_data_path", lambda: tmp_path / "data")
return images
diff --git a/bot/tests/test_session_portable_paths.py b/bot/tests/test_session_portable_paths.py
index d45f4ab39..c1b7d4dc4 100644
--- a/bot/tests/test_session_portable_paths.py
+++ b/bot/tests/test_session_portable_paths.py
@@ -1,9 +1,9 @@
"""Regression tests for portable Bot session persistence paths."""
-import hashlib
import json
import os
import unicodedata
+from datetime import datetime
from pathlib import Path
from types import SimpleNamespace
@@ -48,51 +48,6 @@ def _write_session(
)
- ("logical", "portable"),
- [
- ("order-123", "order-123"),
- ("订单😀", "订单😀"),
- ("order:123", "order%3A123"),
- ("folder/name", "folder%2Fname"),
- (r"folder\name", "folder%5Cname"),
- ('bad<>"|?*', "bad%3C%3E%22%7C%3F%2A"),
- ("literal%3A", "literal%253A"),
- ("line\nbreak", "line%0Abreak"),
- ("trailing.", "trailing%2E"),
- ("trailing ", "trailing%20"),
- ("CON", "%43ON"),
- ],
-)
-def test_portable_path_component_is_readable_and_windows_safe(logical, portable):
- result = portable_path_component(logical)
- digest = hashlib.sha256(logical.encode("utf-8", errors="surrogatepass")).hexdigest()[:16]
-
- assert result == f"{portable}~{digest}"
- assert not any(character in result for character in '<>:"/\\|?*')
- assert not result.endswith((".", " "))
-
-
-def test_portable_path_component_bounds_long_names():
- first = portable_path_component("订单" * 200)
- second = portable_path_component("订单" * 199 + "号")
-
- assert len(first.encode("utf-8")) <= 180
- assert len(second.encode("utf-8")) <= 180
- assert first != second
- assert "订单" in first
-
-
-def test_portable_path_components_resist_case_and_unicode_normalization_collisions():
- lower = portable_path_component("foo")
- upper = portable_path_component("FOO")
- composed = portable_path_component("é")
- decomposed = portable_path_component("e\u0301")
-
- assert lower.casefold() != upper.casefold()
- assert unicodedata.normalize("NFD", composed) != unicodedata.normalize("NFD", decomposed)
-
-
def test_openviking_storage_id_preserves_successful_legacy_namespace():
key = SessionKey(type="cli", channel_id="default", chat_id="scope:order:123")
logical_session_id = key.safe_name()
@@ -270,6 +225,9 @@ def test_session_files_do_not_collide_after_casefold_or_unicode_normalization(tm
assert len({path.name.casefold() for path in paths}) == len(paths)
assert len({unicodedata.normalize("NFD", path.name) for path in paths}) == len(paths)
assert all(path.exists() for path in paths)
+ restarted = SessionManager(tmp_path / "bot")
+ for key in keys:
+ assert restarted.get_or_create(key).messages[0]["content"] == key.chat_id
def test_legacy_lookup_does_not_reuse_a_case_folded_different_session(tmp_path):
@@ -281,23 +239,6 @@ def test_legacy_lookup_does_not_reuse_a_case_folded_different_session(tmp_path):
assert manager._find_session_path(upper) is None
- ("mode", "logical_name"),
- [
- ("per-session", "cli__alerts:primary__order:123"),
- ("per-channel", "cli__alerts:primary"),
- ],
-)
-def test_workspace_names_are_portable_for_every_isolation_mode(mode, logical_name):
- key = SessionKey(type="cli", channel_id="alerts:primary", chat_id="order:123")
-
- result = workspace_name(key, mode, portable=True)
-
- assert result == portable_path_component(logical_name)
- assert result.startswith(logical_name.replace(":", "%3A"))
- assert ":" not in result
-
-
def test_sandbox_and_heartbeat_reuse_existing_legacy_workspace(tmp_path):
key = SessionKey(type="cli", channel_id="default", chat_id="legacy%workspace")
legacy_name = workspace_name(key, "per-session", portable=False)
@@ -314,8 +255,8 @@ def test_sandbox_and_heartbeat_reuse_existing_legacy_workspace(tmp_path):
list_sessions=lambda: [
{
"key": key,
- "created_at": "2026-08-20T00:00:00",
- "updated_at": "2026-08-20T00:00:01",
+ "created_at": datetime.now().isoformat(),
+ "updated_at": datetime.now().isoformat(),
"metadata": {},
}
]
diff --git a/bot/tests/test_skills_metadata.py b/bot/tests/test_skills_metadata.py
deleted file mode 100644
index 3437bd4ff..000000000
--- a/bot/tests/test_skills_metadata.py
+++ /dev/null
@@ -1,24 +0,0 @@
-from vikingbot.agent.skills import SkillsLoader
-
-
-def test_skill_requirements_support_nested_yaml(tmp_path):
- skill_dir = tmp_path / "skills" / "lark-ov-compile-progress"
- skill_dir.mkdir(parents=True)
- (skill_dir / "SKILL.md").write_text(
- """---
-name: lark-ov-compile-progress
-description: Track OV Compile progress
-metadata:
- requires:
- bins:
- - lark-cli
----
-
-Use lark-cli.
-""",
- encoding="utf-8",
- )
-
- assert SkillsLoader(tmp_path)._get_skill_meta("lark-ov-compile-progress") == {
- "requires": {"bins": ["lark-cli"]}
- }
diff --git a/bot/vikingbot/tests/unit/test_stream_tool_calls.py b/bot/tests/test_stream_tool_calls.py
similarity index 61%
rename from bot/vikingbot/tests/unit/test_stream_tool_calls.py
rename to bot/tests/test_stream_tool_calls.py
index b3e8d069d..d904cd1b3 100644
--- a/bot/vikingbot/tests/unit/test_stream_tool_calls.py
+++ b/bot/tests/test_stream_tool_calls.py
@@ -1,8 +1,6 @@
from types import SimpleNamespace
import pytest
-
-import vikingbot.providers.base as provider_base
from vikingbot.providers.base import (
build_stream_response,
merge_stream_tool_call_delta,
@@ -18,45 +16,16 @@ def _tool_call(*, index=None, call_id=None, name=None, arguments=None):
)
-def test_tool_arguments_repair_malformed_json_object():
- raw = '{"pages": [], "files": [], "links": [],}'
-
- assert parse_tool_arguments(raw) == {"pages": [], "files": [], "links": []}
-
-
-def test_tool_arguments_preserve_unrepairable_non_object():
- raw = "not-json"
-
- assert parse_tool_arguments(raw) == {"raw": raw}
-
-
-def test_tool_arguments_do_not_repair_truncated_json_object(monkeypatch):
- def fail_if_called(_raw):
- raise AssertionError("truncated JSON must not be repaired")
-
- monkeypatch.setattr(provider_base.json_repair, "loads", fail_if_called)
- raw = '{"pages": [], "files": [{"path": "logic/related_work.md", "content": "'
-
- assert parse_tool_arguments(raw) == {"raw": raw}
-
-
-def test_tool_arguments_fall_back_when_repair_rejects_input(monkeypatch):
- def reject_input(_raw):
- raise ValueError("invalid input")
-
- monkeypatch.setattr(provider_base.json_repair, "loads", reject_input)
-
- assert parse_tool_arguments("{invalid}") == {"raw": "{invalid}"}
-
-
-def test_tool_arguments_do_not_hide_unexpected_repair_errors(monkeypatch):
- def fail_unexpectedly(_raw):
- raise RuntimeError("repair implementation failed")
-
- monkeypatch.setattr(provider_base.json_repair, "loads", fail_unexpectedly)
-
- with pytest.raises(RuntimeError, match="repair implementation failed"):
- parse_tool_arguments("{invalid}")
+ ("raw", "expected"),
+ [
+ pytest.param('{"query": "bot",}', {"query": "bot"}, id="repairable-object"),
+ pytest.param("not-json", {"raw": "not-json"}, id="unparseable"),
+ pytest.param('{"query": "bot', {"raw": '{"query": "bot'}, id="truncated-object"),
+ ],
+)
+def test_parse_tool_arguments(raw, expected):
+ assert parse_tool_arguments(raw) == expected
def test_missing_stream_tool_call_index_uses_chunk_local_order():
diff --git a/bot/tests/test_studio.py b/bot/tests/test_studio.py
index 6c25c0d3d..3cdf7f676 100644
--- a/bot/tests/test_studio.py
+++ b/bot/tests/test_studio.py
@@ -191,15 +191,6 @@ def test_public_config_never_returns_credentials_and_filters_accounts(tmp_path):
assert error.value.status_code == 404
-async def test_revision_conflict_does_not_mutate_config(tmp_path):
- service = StudioService(SimpleNamespace(bot_data_path=tmp_path), SimpleNamespace(channels={}))
- service.store.save(record())
- with pytest.raises(HTTPException) as error:
- await service.update("a", "connection", {"revision": 0, "action": "pause"})
- assert error.value.status_code == 409
- assert service.get("a", "connection")["enabled"]
-
-
async def test_private_gateway_rejects_loopback_without_secret(tmp_path, monkeypatch):
import httpx
from fastapi import FastAPI
@@ -252,14 +243,6 @@ async def test_failed_secret_rotation_preserves_old_connection(tmp_path, monkeyp
assert service.get("a", "connection")["enabled"]
-async def test_obsolete_setup_step_action_is_rejected(tmp_path):
- service = StudioService(SimpleNamespace(bot_data_path=tmp_path), SimpleNamespace(channels={}))
- service.store.save(record())
- with pytest.raises(HTTPException) as exc:
- await service.update("a", "connection", {"revision": 1, "action": "step", "step": 5})
- assert exc.value.status_code == 400
-
-
def test_managed_group_tools_deny_local_and_unregistered_capabilities():
from vikingbot.studio.policy import disabled_group_tools
@@ -268,14 +251,6 @@ def test_managed_group_tools_deny_local_and_unregistered_capabilities():
) == ["exec", "read_file", "mcp_admin", "spawn"]
-def test_provider_registry_preserves_legacy_and_rejects_unknown():
- assert get_provider({}).type == "feishu"
- assert get_provider({"type": "feishu"}).type == "feishu"
- with pytest.raises(HTTPException) as error:
- get_provider({"type": "slack"})
- assert error.value.status_code == 400
-
-
async def test_delete_connection_stops_runtime_and_cleans_owned_data(tmp_path):
service = StudioService(SimpleNamespace(bot_data_path=tmp_path), SimpleNamespace(channels={}))
item = record()
@@ -302,129 +277,6 @@ async def test_delete_connection_stops_runtime_and_cleans_owned_data(tmp_path):
assert service.store.history(item["id"]) == []
-def test_conversations_show_latest_preview_and_time(tmp_path):
- store = StudioStore(tmp_path / "preview.db")
- store.append("bot", "group", "1", {"title": "Team", "content": "first"})
- store.append(
- "bot",
- "group",
- "2",
- {
- "content": "latest reply",
- "time": "2026-09-16T12:00:00+00:00",
- },
- )
- item = store.conversations("bot")[0]
- assert item["title"] == "first"
- assert item["preview"] == "latest reply"
- assert item["time"] == "2026-09-16T12:00:00+00:00"
- assert store.conversations("another") == []
-
-
-def test_conversation_title_keeps_first_message_when_group_name_resolves(tmp_path):
- store = StudioStore(tmp_path / "titles.db")
- store.append("bot", "group", "1", {"title": "", "content": "介绍一下 OpenViking"})
- assert store.conversations("bot")[0]["title"] == "介绍一下 OpenViking"
- store.append("bot", "group", "2", {"title": "开发讨论", "content": "继续"})
- assert store.conversations("bot")[0]["title"] == "介绍一下 OpenViking"
-
-
-async def test_history_backfills_names_only_when_sender_identity_is_known(channel, monkeypatch):
- channel.store.append(
- "connection",
- "group",
- "known",
- {
- "role": "user",
- "sender": "",
- "sender_id": "ou_person",
- "content": "hello",
- },
- )
- channel.store.append(
- "connection",
- "group",
- "legacy",
- {
- "role": "user",
- "sender": "",
- "content": "old",
- },
- )
- lookup = AsyncMock(return_value="张三")
- monkeypatch.setattr(channel, "_get_group_member_name", lookup)
- messages = await channel.history_with_names("group")
- assert messages[1]["sender"] == "张三"
- assert messages[0]["sender"] == ""
- assert channel.store.history("connection", "group")[1]["sender"] == "张三"
- lookup.assert_awaited_once_with("group", "ou_person")
-
-
-async def test_history_name_lookup_failure_keeps_messages(channel, monkeypatch):
- channel.store.append(
- "connection",
- "group",
- "known",
- {
- "role": "user",
- "sender": "",
- "sender_id": "ou_person",
- "content": "hello",
- },
- )
- monkeypatch.setattr(channel, "_get_group_member_name", AsyncMock(side_effect=RuntimeError()))
- messages = await channel.history_with_names("group")
- assert messages[0]["content"] == "hello"
- assert messages[0]["sender"] == ""
-
-
-async def test_platform_provider_receives_generic_credentials(tmp_path, monkeypatch):
- from vikingbot.studio.providers.registry import PROVIDERS
-
- class FutureProvider:
- type = "future-platform"
-
- async def prepare(self, credentials):
- assert credentials == {"client_id": "app", "client_secret": "secret"}
- return {"client_id": credentials["client_id"]}
-
- def runtime_key(self, record):
- return self.type + record["client_id"]
-
- def install(self, service, record):
- channel = SimpleNamespace(start=AsyncMock())
- service.manager.channels[self.runtime_key(record)] = channel
- return channel
-
- def public_fields(self, record):
- return {"client_id": record["client_id"]}
-
- monkeypatch.setitem(PROVIDERS, "future-platform", FutureProvider())
- service = StudioService(SimpleNamespace(bot_data_path=tmp_path), SimpleNamespace(channels={}))
- result = await service.create(
- "a",
- {
- "type": "future-platform",
- "credentials": {"client_id": "app", "client_secret": "secret"},
- },
- {"user_id": "bot"},
- )
- await asyncio.gather(*service.tasks.values())
- assert result["type"] == "future-platform"
- assert result["client_id"] == "app"
- assert "secret" not in str(result)
- assert service.get("a", result["id"])["account"] == "a"
-
-
[email protected]("value", ["false", 0, None, {"unexpected": True}])
-def test_feishu_settings_reject_invalid_values(value):
- provider = get_provider({"type": "feishu"})
- with pytest.raises(HTTPException):
- provider.validate_settings(
- value if isinstance(value, dict) else {"thread_require_mention": value}
- )
-
-
async def test_reply_settings_persist_apply_immediately_and_keep_revision_guard(
tmp_path, channel, monkeypatch
):
@@ -473,25 +325,6 @@ async def test_reply_settings_persist_apply_immediately_and_keep_revision_guard(
assert captured == [False]
[email protected]("endpoint", ["token", "bot"])
[email protected]("status", [429, 503])
-async def test_feishu_upstream_failure_is_not_a_credentials_error(monkeypatch, endpoint, status):
- import httpx
- from vikingbot.studio.providers.feishu import provider
-
- def respond(request):
- if endpoint == "bot" and request.method == "POST":
- return httpx.Response(200, json={"code": 0, "tenant_access_token": "token"})
- return httpx.Response(status, json={"code": 99991400, "msg": "unavailable"})
-
- client = httpx.AsyncClient(transport=httpx.MockTransport(respond))
- monkeypatch.setattr(provider.httpx, "AsyncClient", lambda **kwargs: client)
- with pytest.raises(HTTPException) as error:
- await provider.FeishuProvider().validate_app("cli_test", "secret")
- assert error.value.status_code == 502
- assert error.value.detail == "Cannot reach Feishu; retry the connection check"
-
-
async def test_recreated_connection_isolates_sessions_and_rejects_old_replies(
tmp_path, monkeypatch
):
diff --git a/bot/tests/test_studio_onboarding.py b/bot/tests/test_studio_onboarding.py
index 44b805ce2..7e016cd8d 100644
--- a/bot/tests/test_studio_onboarding.py
+++ b/bot/tests/test_studio_onboarding.py
@@ -143,22 +143,6 @@ async def test_cannot_cancel_once_app_mutation_started(tmp_path):
assert error.value.status_code == 409
-def test_permission_mapping_does_not_take_user_scopes():
- entries = [{"scopeName": name, "id": name + "-tenant"} for name in console.SCOPES]
- result = console.scope_ids(
- {
- "data": {
- "appScopes": entries,
- "userScopes": [
- {"scopeName": name, "id": name + "-user"} for name in console.SCOPES
- ],
- }
- }
- )
- assert set(result) == console.SCOPES
- assert all(value.endswith("-tenant") for value in result.values())
-
-
async def test_event_write_success_requires_readback():
async def post(path, *args):
if "/scope/all/" in path:
@@ -341,8 +325,13 @@ async def test_retry_waits_for_previous_session_cleanup(tmp_path, monkeypatch):
assert result["state"] == "initializing"
-async def test_numeric_permission_ids_are_sent_as_strings():
- scopes = sorted(console.SCOPES)
+async def test_configure_app_requests_tenant_permissions_from_catalog():
+ scopes = [
+ "im:message.group_at_msg:readonly",
+ "im:message:send_as_bot",
+ "im:chat:read",
+ "im:chat.members:read",
+ ]
calls = []
async def post(path, body=None):
@@ -368,37 +357,6 @@ async def test_numeric_permission_ids_are_sent_as_strings():
assert update["userScopeIDs"] == []
-def test_invalid_permission_ids_are_not_accepted():
- for value in (True, False, None, {}, []):
- assert console.scope_ids({"name": next(iter(console.SCOPES)), "id": value}) == {}
-
-
-async def test_current_feishu_catalog_uses_chat_read_permission():
- calls = []
-
- async def post(path, body=None):
- calls.append((path, body))
- if "/scope/all/" in path:
- return {
- "data": {
- "appScopeList": [
- {"name": "im:message.group_at_msg:readonly", "id": 101},
- {"name": "im:message:send_as_bot", "id": 102},
- {"name": "im:chat:read", "id": 103},
- {"name": "im:chat.members:read", "id": 104},
- ]
- }
- }
- if path == "/developers/v1/event/cli_test":
- return {"data": {"eventMode": 4, "appEvents": ["im.message.receive_v1"]}}
- return {"code": 0}
-
- await console.configure_app(SimpleNamespace(post=post), "cli_test")
- body = next(body for path, body in calls if "/scope/update/" in path)
- assert set(body["appScopeIDs"]) == {"101", "102", "103", "104"}
- assert body["userScopeIDs"] == []
-
-
async def test_new_app_uses_agent_template_and_persists_request_identity():
run = run_record()
calls = []
@@ -445,43 +403,6 @@ async def test_rejected_agent_template_never_falls_back_to_ordinary_bot():
assert run["create_started"]
-async def test_onboarding_idempotency_is_scoped_to_platform(tmp_path, monkeypatch):
- from vikingbot.studio.providers.registry import PROVIDERS
-
- monkeypatch.setitem(
- PROVIDERS,
- "future-platform",
- SimpleNamespace(
- type="future-platform",
- run_onboarding=AsyncMock(),
- ),
- )
- jobs = make_jobs(tmp_path)
- monkeypatch.setattr(jobs, "launch", jobs.service.store.save_onboarding)
- request_id = str(uuid.uuid4())
- identity = {"user_id": "bot"}
- feishu = await jobs.start("a", {"type": "feishu", "request_id": request_id}, identity)
- other = await jobs.start("a", {"type": "future-platform", "request_id": request_id}, identity)
- assert feishu["id"] != other["id"]
- assert jobs.current("a", "feishu")["id"] == feishu["id"]
- assert jobs.current("a", "future-platform")["id"] == other["id"]
-
-
-async def test_setup_preserves_reply_settings(tmp_path, monkeypatch):
- jobs = make_jobs(tmp_path)
- monkeypatch.setattr(jobs, "launch", jobs.service.store.save_onboarding)
- run = await jobs.start(
- "a",
- {
- "type": "feishu",
- "request_id": str(uuid.uuid4()),
- "settings": {"thread_require_mention": False},
- },
- {"user_id": "bot"},
- )
- assert jobs.get("a", run["id"])["settings"] == {"thread_require_mention": False}
-
-
@pytest.mark.parametrize("require_mention", [True, False])
async def test_setup_requests_all_group_messages_only_when_selected(require_mention):
all_scopes = console.SCOPES | {"im:message.group_msg"}
diff --git a/bot/tests/test_subagent_skills_context.py b/bot/tests/test_subagent_skills_context.py
index 638619806..45dcc02e0 100644
--- a/bot/tests/test_subagent_skills_context.py
+++ b/bot/tests/test_subagent_skills_context.py
@@ -3,16 +3,12 @@
"""Regression tests for subagent prompt skill loading."""
import asyncio
-import sys
from pathlib import Path
from types import SimpleNamespace
import pytest
-
-sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
-
-from vikingbot.agent.subagent import SubagentManager # noqa: E402
-from vikingbot.bus.queue import MessageBus # noqa: E402
+from vikingbot.agent.subagent import SubagentManager
+from vikingbot.bus.queue import MessageBus
def _write_skill(workspace: Path, name: str, content: str) -> None:
@@ -58,7 +54,6 @@ Read this only when needed.
assert "# Active Skills" in prompt
assert "### Skill: always-skill" in prompt
assert "Always-loaded instruction." in prompt
- assert "description: Always active instructions" not in prompt
assert "# Skills" in prompt
assert "<name>normal-skill</name>" in prompt
assert "<description>Normal on-demand instructions</description>" in prompt
diff --git a/bot/tests/test_werewolf_server_security.py b/bot/tests/test_werewolf_server_security.py
index 025f10b10..b8fa2b6fb 100644
--- a/bot/tests/test_werewolf_server_security.py
+++ b/bot/tests/test_werewolf_server_security.py
@@ -3,54 +3,11 @@
from __future__ import annotations
-from pathlib import Path
-from unittest.mock import patch
-
from fastapi.testclient import TestClient
from bot.demo.werewolf.werewolf_server import GameState, create_fastapi_app
-def _make_client(tmp_path) -> TestClient:
- state = GameState(
- game_id="test-game",
- vikingbot_url="http://127.0.0.1:18790",
- config_path=tmp_path / "ov.conf",
- config={},
- storage_path=tmp_path,
- )
- return TestClient(create_fastapi_app(state))
-
-
-def test_start_endpoint_hides_value_error_details(tmp_path):
- client = _make_client(tmp_path)
-
- async def _raise_value_error(_state):
- raise ValueError("secret filesystem detail")
-
- with patch("bot.demo.werewolf.werewolf_server.start_current_game", new=_raise_value_error):
- response = client.post("/api/start", json={"game_mode": "all_agents"})
-
- assert response.status_code == 200
- assert response.json() == {"success": False, "error": "Failed to start game"}
- assert "secret filesystem detail" not in response.text
-
-
-def test_conversation_endpoint_hides_internal_exception_details(tmp_path):
- conversation_dir = tmp_path / "bot" / "workspace" / "werewolf"
- conversation_dir.mkdir(parents=True, exist_ok=True)
- (conversation_dir / "CONVERSATION_demo.md").write_text("demo", encoding="utf-8")
-
- client = _make_client(tmp_path)
-
- with patch.object(Path, "read_text", side_effect=RuntimeError("secret stack detail")):
- response = client.get("/api/conversation/demo")
-
- assert response.status_code == 500
- assert 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)
diff --git a/bot/vikingbot/channels/feishu_test.py b/bot/vikingbot/channels/feishu_test.py
deleted file mode 100644
index 13e994be7..000000000
--- a/bot/vikingbot/channels/feishu_test.py
+++ /dev/null
@@ -1,29 +0,0 @@
-import json
-
-import lark_oapi as lark
-from lark_oapi.api.contact.v3 import GetUserRequest
-
-
-def main():
- # 创建client
- client = lark.Client.builder().app_id("").app_secret("").log_level(lark.LogLevel.DEBUG).build()
-
- # 构造请求对象
- request: GetUserRequest = GetUserRequest.builder().user_id("").user_id_type("open_id").build()
-
- # 发起请求
- response = client.contact.v3.user.get(request)
-
- # 处理失败返回
- if not response.success():
- lark.logger.error(
- f"client.contact.v3.user.get failed, code: {response.code}, msg: {response.msg}, log_id: {response.get_log_id()}, resp: \n{json.dumps(json.loads(response.raw.content), indent=4, ensure_ascii=False)}"
- )
- return
-
- # 处理业务结果
- lark.logger.info(lark.JSON.marshal(response.data, indent=4))
-
-
-if __name__ == "__main__":
- main()
diff --git a/bot/vikingbot/tests/__init__.py b/bot/vikingbot/tests/__init__.py
deleted file mode 100644
index 1ed28a3ab..000000000
--- a/bot/vikingbot/tests/__init__.py
+++ /dev/null
@@ -1 +0,0 @@
-"""Vikingbot test suite."""
diff --git a/bot/vikingbot/tests/integration/__init__.py b/bot/vikingbot/tests/integration/__init__.py
deleted file mode 100644
index c210facce..000000000
--- a/bot/vikingbot/tests/integration/__init__.py
+++ /dev/null
@@ -1 +0,0 @@
-"""Integration tests."""
diff --git a/bot/vikingbot/tests/unit/__init__.py b/bot/vikingbot/tests/unit/__init__.py
deleted file mode 100644
index e0310a01e..000000000
--- a/bot/vikingbot/tests/unit/__init__.py
+++ /dev/null
@@ -1 +0,0 @@
-"""Unit tests."""
diff --git a/bot/vikingbot/tests/unit/test_agent/__init__.py b/bot/vikingbot/tests/unit/test_agent/__init__.py
deleted file mode 100644
index e34162309..000000000
--- a/bot/vikingbot/tests/unit/test_agent/__init__.py
+++ /dev/null
@@ -1 +0,0 @@
-"""Agent module tests."""
diff --git a/bot/vikingbot/tests/unit/test_bot_model_timeout.py b/bot/vikingbot/tests/unit/test_bot_model_timeout.py
deleted file mode 100644
index 2604c83b0..000000000
--- a/bot/vikingbot/tests/unit/test_bot_model_timeout.py
+++ /dev/null
@@ -1,103 +0,0 @@
-from types import SimpleNamespace
-from unittest import mock
-
-import pytest
-from pydantic import ValidationError
-
-from openviking.models.vlm.backends.litellm_vlm import LiteLLMVLMProvider
-from vikingbot.config.loader import _merge_vlm_model_config
-from vikingbot.config.schema import AgentsConfig
-from vikingbot.providers.vlm_adapter import VLMProviderAdapter
-
-
-def _chat_response(content: str = "ok"):
- return SimpleNamespace(
- choices=[
- SimpleNamespace(
- message=SimpleNamespace(content=content, tool_calls=None),
- finish_reason="stop",
- )
- ],
- usage=SimpleNamespace(prompt_tokens=1, completion_tokens=1, total_tokens=2),
- )
-
-
-def test_agents_config_accepts_timeout():
- config = AgentsConfig(timeout=120.0)
-
- assert config.timeout == 120.0
-
-
-def test_agents_config_rejects_non_positive_timeout():
- with pytest.raises(ValidationError):
- AgentsConfig(timeout=0)
-
-
-def test_bot_agents_timeout_inherits_vlm_timeout_when_omitted():
- bot_data = {"agents": {"model": "bot-model"}}
-
- _merge_vlm_model_config(bot_data, {"model": "vlm-model", "timeout": 180.0})
-
- assert bot_data["agents"]["model"] == "bot-model"
- assert bot_data["agents"]["timeout"] == 180.0
-
-
-def test_bot_agents_timeout_inherits_vlm_default_timeout_when_omitted():
- bot_data = {"agents": {"model": "bot-model"}}
-
- _merge_vlm_model_config(bot_data, {"model": "vlm-model"})
-
- assert bot_data["agents"]["timeout"] == 60.0
-
-
-def test_bot_agents_timeout_overrides_vlm_timeout():
- bot_data = {"agents": {"model": "bot-model", "timeout": 45.0}}
-
- _merge_vlm_model_config(bot_data, {"model": "vlm-model", "timeout": 180.0})
-
- assert bot_data["agents"]["timeout"] == 45.0
-
-
-async def test_vlm_adapter_passes_litellm_timeout_to_chat_request(monkeypatch):
- captured = {}
-
- async def fake_acompletion(**kwargs):
- captured.update(kwargs)
- return _chat_response()
-
- monkeypatch.setattr("openviking.models.vlm.backends.litellm_vlm.acompletion", fake_acompletion)
-
- vlm = LiteLLMVLMProvider(
- {
- "provider": "litellm",
- "api_key": "sk-test",
- "api_base": "https://example.invalid",
- "model": "openai/gpt-4o-mini",
- "timeout": 75.0,
- }
- )
- provider = VLMProviderAdapter(vlm, default_model="openai/gpt-4o-mini")
- response = await provider.chat(messages=[{"role": "user", "content": "hi"}])
-
- assert response.content == "ok"
- assert captured["timeout"] == 75.0
-
-
-def test_volcengine_vlm_passes_timeout_to_sync_client():
- from openviking.models.vlm.backends.volcengine_vlm import VolcEngineVLM
-
- vlm = VolcEngineVLM(
- {
- "provider": "volcengine",
- "model": "doubao-test",
- "api_key": "ak-test",
- "api_base": "https://example.invalid/api/v3",
- "timeout": 88.0,
- }
- )
-
- with mock.patch("volcenginesdkarkruntime.Ark") as fake:
- vlm.get_client()
-
- assert fake.call_args.kwargs["timeout"] == 88.0
diff --git a/bot/vikingbot/tests/unit/test_bus/__init__.py b/bot/vikingbot/tests/unit/test_bus/__init__.py
deleted file mode 100644
index 785e27236..000000000
--- a/bot/vikingbot/tests/unit/test_bus/__init__.py
+++ /dev/null
@@ -1 +0,0 @@
-"""Message bus tests."""
diff --git a/bot/vikingbot/tests/unit/test_channels/__init__.py b/bot/vikingbot/tests/unit/test_channels/__init__.py
deleted file mode 100644
index 4294d6287..000000000
--- a/bot/vikingbot/tests/unit/test_channels/__init__.py
+++ /dev/null
@@ -1 +0,0 @@
-"""Channel adapter tests."""
diff --git a/bot/vikingbot/tests/unit/test_config/__init__.py b/bot/vikingbot/tests/unit/test_config/__init__.py
deleted file mode 100644
index b27d053b5..000000000
--- a/bot/vikingbot/tests/unit/test_config/__init__.py
+++ /dev/null
@@ -1 +0,0 @@
-"""Configuration tests."""
diff --git a/bot/vikingbot/tests/unit/test_config/test_vlm_only_config_survives_ovcli_error.py b/bot/vikingbot/tests/unit/test_config/test_vlm_only_config_survives_ovcli_error.py
deleted file mode 100644
index 33737a1c7..000000000
--- a/bot/vikingbot/tests/unit/test_config/test_vlm_only_config_survives_ovcli_error.py
+++ /dev/null
@@ -1,59 +0,0 @@
-"""Bot configuration must remain independent from ovcli.conf.
-
-VLM settings and the static OpenViking upstream are loaded from ov.conf. An
-invalid ovcli.conf must not discard VLM settings, and a valid ovcli user key
-must not be copied into the Bot process configuration.
-"""
-
-import json
-
-from vikingbot.config import loader
-
-
-def _write_conf(tmp_path, monkeypatch):
- conf = tmp_path / "ov.conf"
- conf.write_text(
- json.dumps(
- {
- "vlm": {
- "provider": "deepseek",
- "api_base": "https://api.deepseek.com",
- "api_key": "sk-deepseek-test-key",
- "model": "deepseek-chat",
- },
- # api_key auth mode is what drives the ovcli user-key lookup.
- "server": {"auth_mode": "api_key"},
- }
- )
- )
- monkeypatch.setattr(loader, "CONFIG_PATH", conf)
- return conf
-
-
-def test_vlm_config_preserved_when_ovcli_config_is_invalid(tmp_path, monkeypatch):
- _write_conf(tmp_path, monkeypatch)
- ovcli_conf = tmp_path / "ovcli.conf"
- ovcli_conf.write_text("{invalid json")
- monkeypatch.setenv("OPENVIKING_CLI_CONFIG_FILE", str(ovcli_conf))
-
- config = loader.load_config()
-
- # The vlm-derived agent config must survive the ovcli failure.
- assert config.agents.model == "deepseek-chat"
- assert config.agents.provider == "deepseek"
- assert config.agents.api_key == "sk-deepseek-test-key"
- assert config.agents.api_base == "https://api.deepseek.com"
- assert not config.ov_server.api_key
-
-
-def test_ovcli_user_key_is_not_copied_into_bot_config(tmp_path, monkeypatch):
- _write_conf(tmp_path, monkeypatch)
- ovcli_conf = tmp_path / "ovcli.conf"
- ovcli_conf.write_text(json.dumps({"url": "http://ov.local", "api_key": "user-api-key"}))
- monkeypatch.setenv("OPENVIKING_CLI_CONFIG_FILE", str(ovcli_conf))
-
- config = loader.load_config()
-
- assert config.agents.provider == "deepseek"
- assert config.agents.model == "deepseek-chat"
- assert not config.ov_server.api_key