diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index fa4a80025c4..372e68a3d1e 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -101,6 +101,7 @@ jobs: deps_changed: ${{ steps.check.outputs.deps_changed }} cuvs_changed: ${{ steps.check.outputs.cuvs_changed }} langchain_changed: ${{ steps.check.outputs.langchain_changed }} + bot_changed: ${{ steps.check.outputs.bot_changed }} steps: - uses: actions/checkout@v6 with: @@ -152,6 +153,20 @@ jobs: echo "langchain_changed=false" >> $GITHUB_OUTPUT fi + # Run the VikingBot unit test suites when bot code, its dependency + # set, or this workflow change. + BOT_PATTERN="^bot/|pyproject\.toml|uv\.lock|\.github/workflows/pr\.yml" + BOT_CHANGED_FILES=$(git diff --name-only origin/${{ github.base_ref }} HEAD | grep -E "$BOT_PATTERN" || true) + + if [ -n "$BOT_CHANGED_FILES" ]; then + echo "Bot changes detected:" + echo "$BOT_CHANGED_FILES" + echo "bot_changed=true" >> $GITHUB_OUTPUT + else + echo "No bot changes detected." + echo "bot_changed=false" >> $GITHUB_OUTPUT + fi + build: needs: check-deps if: ${{ needs.check-deps.outputs.deps_changed == 'true' }} @@ -165,6 +180,30 @@ jobs: os_json: '["ubuntu-24.04"]' python_json: '["3.11"]' + bot-tests: + needs: check-deps + if: ${{ needs.check-deps.outputs.bot_changed == 'true' }} + runs-on: ubuntu-24.04 + steps: + - uses: actions/checkout@v6 + with: + fetch-depth: 0 + + - uses: actions/setup-python@v6 + with: + python-version: '3.12' + + - uses: astral-sh/setup-uv@v7 + with: + enable-cache: true + + - name: Install bot dependencies + run: uv sync --frozen --extra bot --extra test + + - name: Run VikingBot unit tests + run: | + uv run python -m pytest -q -o addopts='' bot/tests bot/vikingbot + langchain-tests: needs: check-deps if: ${{ needs.check-deps.outputs.langchain_changed == 'true' }} diff --git a/bot/tests/test_agent_loop_outcome.py b/bot/tests/test_agent_loop_outcome.py index 7b8f1780684..447f757f1f5 100644 --- a/bot/tests/test_agent_loop_outcome.py +++ b/bot/tests/test_agent_loop_outcome.py @@ -616,7 +616,12 @@ async def execute(self, tool_name, arguments, **kwargs): "tool result: useful context", "tool result: useful context", ] - assert token_usage == {"prompt_tokens": 17, "completion_tokens": 7, "total_tokens": 24} + assert token_usage == { + "prompt_tokens": 17, + "completion_tokens": 7, + "total_tokens": 24, + "cache_read_input_tokens": 0, + } @pytest.mark.asyncio @@ -643,7 +648,7 @@ async def fake_run_agent_loop(self, **kwargs): agents={ "session_context_enabled": True, "commit_token_threshold": 1, - "commit_keep_recent_count": 0, + "commit_keep_recent_turn_count": 0, }, ) loop = AgentLoop( @@ -1031,7 +1036,7 @@ async def fake_execute_hooks(context, **kwargs): ( "hook", kwargs["force_commit"], - kwargs.get("keep_recent_count"), + kwargs.get("keep_recent_turn_count"), kwargs.get("commit_message_threshold"), ) ) @@ -1078,7 +1083,7 @@ async def fake_run_agent_loop(self, **kwargs): "session_context_enabled": True, "session_context_token_budget": 321, "commit_token_threshold": 100, - "commit_keep_recent_count": 2, + "commit_keep_recent_turn_count": 2, }, ) loop = AgentLoop( @@ -1137,7 +1142,7 @@ async def fake_execute_hooks(context, **kwargs): ( "hook", kwargs["force_commit"], - kwargs.get("keep_recent_count"), + kwargs.get("keep_recent_turn_count"), kwargs.get("commit_message_threshold"), ) ) @@ -1182,7 +1187,7 @@ async def fake_run_agent_loop(self, **kwargs): "session_context_enabled": True, "session_context_token_budget": 321, "commit_token_threshold": 1000, - "commit_keep_recent_count": 2, + "commit_keep_recent_turn_count": 2, }, ) loop = AgentLoop( @@ -1249,7 +1254,7 @@ async def fake_execute_hooks(context, **kwargs): agents={ "session_context_enabled": True, "commit_token_threshold": 1000, - "commit_keep_recent_count": 2, + "commit_keep_recent_turn_count": 2, }, ) loop = AgentLoop( diff --git a/bot/tests/test_compile.py b/bot/tests/test_compile.py index facc5bfa6ff..e5105959678 100644 --- a/bot/tests/test_compile.py +++ b/bot/tests/test_compile.py @@ -2184,7 +2184,7 @@ def test_compile_prompt_describes_editable_target_checkout(): 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 "scans and validates the complete tree" in system assert "Inspect the editable target checkout" in user diff --git a/bot/tests/test_openviking_api_key_type.py b/bot/tests/test_openviking_api_key_type.py index d9fb0c7b891..2e98621c135 100644 --- a/bot/tests/test_openviking_api_key_type.py +++ b/bot/tests/test_openviking_api_key_type.py @@ -71,7 +71,13 @@ async def get_session(self, session_id): async def batch_add_messages(self, session_id, messages): return {"session_id": session_id, "added": len(messages), "message_count": len(messages)} - async def commit_session(self, session_id, keep_recent_count=0, telemetry=False): + async def commit_session( + self, + session_id, + keep_recent_count=0, + telemetry=False, + **_retention_kwargs, + ): return { "session_id": session_id, "status": "committed", @@ -129,6 +135,7 @@ def _make_config(api_key_type: str, mode: str = "remote", **ov_overrides): "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): @@ -1195,7 +1202,7 @@ async def test_compact_hook_session_context_commits_single_session_with_peer_mes "root", session_context_enabled=True, commit_token_threshold=100, - commit_keep_recent_count=2, + commit_keep_recent_turn_count=2, ), ) @@ -1238,7 +1245,14 @@ async def commit_session( user_id=None, **_retention_kwargs, ): - self.commit_calls.append((session_id, keep_recent_count, user_id)) + self.commit_calls.append( + ( + session_id, + _retention_kwargs.get("retention_mode"), + _retention_kwargs.get("keep_recent_turn_count"), + user_id, + ) + ) return {"session_id": session_id, "status": "accepted"} fake_client = _FakeClient() @@ -1284,7 +1298,7 @@ async def _fake_get_client(_workspace_id): "admin", ) ] - assert fake_client.commit_calls == [(storage_session_id, 0, "admin")] + assert fake_client.commit_calls == [(storage_session_id, "turn_budget", 2, "admin")] assert {session_id for session_id, _user_id in fake_client.session_calls} == { storage_session_id } @@ -1346,7 +1360,7 @@ async def test_compact_hook_force_commit_does_not_resync_already_synced_messages "root", session_context_enabled=True, commit_token_threshold=100, - commit_keep_recent_count=2, + commit_keep_recent_turn_count=2, ), ) @@ -1371,8 +1385,21 @@ async def append_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)) + async def commit_session( + self, + session_id, + keep_recent_count=0, + user_id=None, + **_retention_kwargs, + ): + self.commit_calls.append( + ( + session_id, + _retention_kwargs.get("retention_mode"), + _retention_kwargs.get("keep_recent_turn_count"), + user_id, + ) + ) return {"session_id": session_id, "status": "accepted"} fake_client = _FakeClient() @@ -1406,7 +1433,7 @@ async def _fake_get_client(_workspace_id): 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", "turn_budget", 2, "admin")] assert session.metadata["openviking"]["last_synced_local_index"] == 1 assert session.metadata["openviking"]["last_commit_local_index"] == 1 @@ -1424,7 +1451,7 @@ async def test_compact_hook_force_commit_commits_current_session_without_unsynce "root", session_context_enabled=True, commit_token_threshold=1000, - commit_keep_recent_count=2, + commit_keep_recent_turn_count=2, ), ) @@ -1449,8 +1476,21 @@ async def append_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)) + async def commit_session( + self, + session_id, + keep_recent_count=0, + user_id=None, + **_retention_kwargs, + ): + self.commit_calls.append( + ( + session_id, + _retention_kwargs.get("retention_mode"), + _retention_kwargs.get("keep_recent_turn_count"), + user_id, + ) + ) return {"session_id": session_id, "status": "accepted"} fake_client = _FakeClient() @@ -1484,7 +1524,7 @@ async def _fake_get_client(_workspace_id): 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", "turn_budget", 2, "admin")] assert session.metadata["openviking"]["last_commit_performed"] is True @@ -1501,7 +1541,7 @@ async def test_compact_hook_session_context_append_failure_does_not_advance_sync "root", session_context_enabled=True, commit_token_threshold=100, - commit_keep_recent_count=2, + commit_keep_recent_turn_count=2, ), ) @@ -1528,8 +1568,21 @@ async def append_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)) + async def commit_session( + self, + session_id, + keep_recent_count=0, + user_id=None, + **_retention_kwargs, + ): + self.commit_calls.append( + ( + session_id, + _retention_kwargs.get("retention_mode"), + _retention_kwargs.get("keep_recent_turn_count"), + user_id, + ) + ) return {"session_id": session_id, "status": "accepted"} fake_client = _FakeClient() @@ -1581,7 +1634,7 @@ async def test_compact_hook_session_context_commits_when_message_threshold_reach "root", session_context_enabled=True, commit_token_threshold=1000, - commit_keep_recent_count=2, + commit_keep_recent_turn_count=2, ), ) @@ -1606,8 +1659,21 @@ async def append_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)) + async def commit_session( + self, + session_id, + keep_recent_count=0, + user_id=None, + **_retention_kwargs, + ): + self.commit_calls.append( + ( + session_id, + _retention_kwargs.get("retention_mode"), + _retention_kwargs.get("keep_recent_turn_count"), + user_id, + ) + ) return {"session_id": session_id, "status": "accepted"} fake_client = _FakeClient() @@ -1644,7 +1710,7 @@ async def _fake_get_client(_workspace_id): 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", "turn_budget", 2, "admin")] assert session.metadata["openviking"]["last_commit_local_index"] == 2 @@ -1661,7 +1727,7 @@ async def test_compact_hook_session_commit_failure_retries_without_resyncing_mes "root", session_context_enabled=True, commit_token_threshold=100, - commit_keep_recent_count=2, + commit_keep_recent_turn_count=2, ), ) @@ -1687,8 +1753,21 @@ async def append_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)) + async def commit_session( + self, + session_id, + keep_recent_count=0, + user_id=None, + **_retention_kwargs, + ): + self.commit_calls.append( + ( + session_id, + _retention_kwargs.get("retention_mode"), + _retention_kwargs.get("keep_recent_turn_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"} @@ -1719,7 +1798,7 @@ async def _fake_get_client(_workspace_id): 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", "turn_budget", 2, "admin")] state = session.metadata["openviking"] assert state["last_sync_status"] == "error" assert state["last_synced_local_index"] == 1 @@ -1733,7 +1812,7 @@ async def _fake_get_client(_workspace_id): 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", "turn_budget", 2, "admin")] assert session.metadata["openviking"]["last_commit_performed"] is True @@ -1750,7 +1829,7 @@ async def test_compact_hook_session_context_skips_message_threshold_after_recent "root", session_context_enabled=True, commit_token_threshold=1000, - commit_keep_recent_count=2, + commit_keep_recent_turn_count=2, ), ) @@ -1775,8 +1854,21 @@ async def append_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)) + async def commit_session( + self, + session_id, + keep_recent_count=0, + user_id=None, + **_retention_kwargs, + ): + self.commit_calls.append( + ( + session_id, + _retention_kwargs.get("retention_mode"), + _retention_kwargs.get("keep_recent_turn_count"), + user_id, + ) + ) return {"session_id": session_id, "status": "accepted"} fake_client = _FakeClient() diff --git a/bot/tests/test_session_portable_paths.py b/bot/tests/test_session_portable_paths.py index d45f4ab3908..ac2611d5e3a 100644 --- a/bot/tests/test_session_portable_paths.py +++ b/bot/tests/test_session_portable_paths.py @@ -4,6 +4,7 @@ import json import os import unicodedata +from datetime import datetime, timedelta, timezone from pathlib import Path from types import SimpleNamespace @@ -310,12 +311,15 @@ def test_sandbox_and_heartbeat_reuse_existing_legacy_workspace(tmp_path): assert sandbox_manager.to_workspace_id(key) == legacy_name assert sandbox_manager.get_workspace_path(key) == legacy_path + # Recent timestamps so the session never crosses the heartbeat staleness + # window (STALE_SESSION_THRESHOLD) no matter when the suite runs. + last_active = datetime.now(timezone.utc) - timedelta(minutes=5) session_manager = SimpleNamespace( list_sessions=lambda: [ { "key": key, - "created_at": "2026-08-20T00:00:00", - "updated_at": "2026-08-20T00:00:01", + "created_at": last_active.isoformat(), + "updated_at": last_active.isoformat(), "metadata": {}, } ]