Skip to content

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

fix(compile): isolate account locks and stop stalled review loops (#6133)

Jiajie - He/him/hisa9c21c2fdcb7
diff --git a/bot/tests/test_bot_provider_thinking.py b/bot/tests/test_bot_provider_thinking.py
index 6f0900fbf..230b609e8 100644
--- a/bot/tests/test_bot_provider_thinking.py
+++ b/bot/tests/test_bot_provider_thinking.py
@@ -1,4 +1,5 @@
from types import SimpleNamespace
+from unittest.mock import MagicMock
import pytest
from vikingbot.providers.vlm_adapter import VLMProviderAdapter
@@ -183,7 +184,9 @@ async def test_vlm_adapter_volcengine_stream_respects_optional_max_tokens(
usage=None,
)
- return chunks()
+ response = MagicMock()
+ response.__aenter__.return_value = chunks()
+ return response
vlm = SimpleNamespace(
provider="volcengine",
diff --git a/bot/tests/test_bot_vlm_credentials.py b/bot/tests/test_bot_vlm_credentials.py
index 168f362fb..934f3302c 100644
--- a/bot/tests/test_bot_vlm_credentials.py
+++ b/bot/tests/test_bot_vlm_credentials.py
@@ -10,6 +10,7 @@ from vikingbot.providers.vlm_adapter import VLMProviderAdapter
from openviking.models.vlm import MultiCredentialVLM
from openviking.models.vlm.backends.litellm_vlm import LiteLLMVLMProvider
+from openviking.models.vlm.retry_vlm import RetryVLM
def _write_config(tmp_path, monkeypatch, data):
@@ -149,7 +150,7 @@ def test_bot_inherits_root_vlm_credentials_when_agents_model_is_omitted(tmp_path
@pytest.mark.parametrize("bot_override", [False, True], ids=["inherited-model", "bot-model"])
-def test_agent_max_tokens_reaches_provider(tmp_path, monkeypatch, bot_override):
+def test_agent_generation_settings_reach_provider(tmp_path, monkeypatch, bot_override):
agents = {"max_tokens": 8192}
if bot_override:
agents.update(provider="openai", model="bot-model", api_key="bot-key")
@@ -162,6 +163,7 @@ def test_agent_max_tokens_reaches_provider(tmp_path, monkeypatch, bot_override):
"model": "root-model",
"api_key": "root-key",
"max_tokens": 4096,
+ "thinking": False,
},
"bot": {"agents": agents},
},
@@ -169,6 +171,7 @@ def test_agent_max_tokens_reaches_provider(tmp_path, monkeypatch, bot_override):
provider = _make_provider(config)
assert config.agents.inherits_root_vlm() is (not bot_override)
assert provider._vlm.max_tokens == 8192
+ assert provider._vlm.thinking is bot_override
def test_explicit_bot_model_uses_bot_credentials_instead_of_root(tmp_path, monkeypatch):
@@ -248,6 +251,7 @@ def test_bot_credentials_without_outer_model_use_bot_chain(tmp_path, monkeypatch
{
"vlm": {
"model": "root-primary",
+ "thinking": False,
"credentials": [
{
"id": "root-primary",
@@ -264,6 +268,7 @@ def test_bot_credentials_without_outer_model_use_bot_chain(tmp_path, monkeypatch
provider = _make_provider(config)
assert config.agents.inherits_root_vlm() is False
+ assert provider._vlm.thinking is True
assert config.agents.model == ""
assert isinstance(provider._vlm, MultiCredentialVLM)
assert provider._vlm._credential_ids == ["bot-primary", "bot-backup"]
@@ -353,7 +358,8 @@ def test_explicit_litellm_provider_uses_vlm_adapter(tmp_path, monkeypatch):
provider = _make_provider(config)
assert isinstance(provider, VLMProviderAdapter)
- assert isinstance(provider._vlm, LiteLLMVLMProvider)
+ assert isinstance(provider._vlm, RetryVLM)
+ assert isinstance(provider._vlm.inner, LiteLLMVLMProvider)
assert provider._vlm.model == "openrouter/openai/gpt-4o-mini"
diff --git a/bot/tests/test_bot_vlm_streaming.py b/bot/tests/test_bot_vlm_streaming.py
index ba0a46250..f17dad76e 100644
--- a/bot/tests/test_bot_vlm_streaming.py
+++ b/bot/tests/test_bot_vlm_streaming.py
@@ -1,8 +1,10 @@
from types import SimpleNamespace
+from unittest.mock import MagicMock
import pytest
from vikingbot.providers.vlm_adapter import VLMProviderAdapter
+from openviking.models.model_error import ModelError, ModelErrorKind
from openviking.models.vlm import FailoverVLM, MultiCredentialVLM, OpenAIVLM, VolcEngineVLM
@@ -23,7 +25,9 @@ class FakeCompletions:
raise chunk
yield chunk
- return generate()
+ response = MagicMock()
+ response.__aenter__.return_value = generate()
+ return response
def stream_chunk(
@@ -112,13 +116,16 @@ async def test_vlm_adapter_streams_openai_content_deltas():
@pytest.mark.asyncio
async def test_vlm_adapter_streams_with_credential_failover_before_first_delta():
- primary_completions = FakeCompletions(error=RuntimeError("status 429: rate limit"))
+ primary_completions = FakeCompletions(
+ error=ModelError(ModelErrorKind.RATE_LIMIT_ERROR, "Rate limit exceeded")
+ )
backup_completions = FakeCompletions(chunks=[stream_chunk("backup", finish_reason="stop")])
primary = VolcEngineVLM(
{
"provider": "volcengine",
"model": "primary-model",
"api_key": "primary-key",
+ "max_retries": 0,
}
)
backup = VolcEngineVLM(
@@ -126,6 +133,8 @@ async def test_vlm_adapter_streams_with_credential_failover_before_first_delta()
"provider": "volcengine",
"model": "backup-model",
"api_key": "backup-key",
+ "thinking": True,
+ "reasoning_effort": "low",
}
)
attach_client(primary, primary_completions)
@@ -149,6 +158,7 @@ async def test_vlm_adapter_streams_with_credential_failover_before_first_delta()
assert events[-1].response.content == "backup"
assert len(primary_completions.calls) == 1
assert backup_completions.calls[0]["model"] == "backup-model"
+ assert backup_completions.calls[0]["reasoning_effort"] == "low"
assert credentials.active_credential_id == "backup"
@@ -178,7 +188,10 @@ async def test_vlm_adapter_streams_with_legacy_primary_backup_failover():
@pytest.mark.asyncio
async def test_credential_stream_does_not_replay_after_a_visible_delta():
primary_completions = FakeCompletions(
- chunks=[stream_chunk("partial"), RuntimeError("status 429: rate limit")]
+ chunks=[
+ stream_chunk("partial"),
+ ModelError(ModelErrorKind.RATE_LIMIT_ERROR, "Rate limit exceeded"),
+ ]
)
backup_completions = FakeCompletions(chunks=[stream_chunk("duplicate", finish_reason="stop")])
primary = OpenAIVLM({"provider": "openai", "model": "primary-model", "api_key": "primary-key"})
@@ -202,4 +215,5 @@ async def test_credential_stream_does_not_replay_after_a_visible_delta():
assert events[0].content == "partial"
assert events[-1].type == "response"
assert events[-1].response.finish_reason == "error"
+ assert len(primary_completions.calls) == 1
assert backup_completions.calls == []
diff --git a/bot/tests/test_compile_execution.py b/bot/tests/test_compile_execution.py
new file mode 100644
index 000000000..218ed94b7
--- /dev/null
+++ b/bot/tests/test_compile_execution.py
@@ -0,0 +1,106 @@
+"""Core regressions for account isolation and advisory Compile execution."""
+
+import asyncio
+from collections import Counter
+from types import SimpleNamespace
+from unittest.mock import AsyncMock
+
+import pytest
+from vikingbot.compile import review
+from vikingbot.compile.models import CompileLimits
+from vikingbot.compile.pipeline_io import JsonModel, ModelCallLimitError
+from vikingbot.compile.plan import Contract, Node, ReviewDecision
+from vikingbot.compile.service import BotCompileService
+from vikingbot.config.schema import Config
+from vikingbot.providers.base import LLMResponse, ToolCallRequest
+
+
+ "mode,account,peak", [("api_key", "b", 2), ("api_key", "a", 1), ("dev", "b", 1)]
+)
+async def test_target_locks_follow_effective_account(tmp_path, mode, account, peak):
+ config = Config(storage_workspace=str(tmp_path))
+ config.ov_server.set_effective_auth_mode(mode)
+ service = BotCompileService(agent_loop=SimpleNamespace(config=config))
+ active = highest = 0
+
+ async def execute(*args):
+ nonlocal active, highest
+ active += 1
+ highest = max(highest, active)
+ await asyncio.sleep(0)
+ active -= 1
+
+ service._execute_task = execute
+ request = SimpleNamespace(to="viking://resources/wiki")
+ await asyncio.gather(
+ service._run_task("a", request, {"account_id": "a", "user_id": "alice"}),
+ service._run_task("b", request, {"account_id": account, "user_id": "bob"}),
+ )
+ assert highest == peak
+
+
[email protected]("failure", ["model", "record", "cancel"])
+async def test_review_failures_preserve_plan_and_cancellation(failure):
+ contract = Contract(extract={"instructions": "Copy", "execution": "direct", "output": "files"})
+ decision = ReviewDecision(
+ action="revise", contract=contract, plan="p.finalize(mapped, into=target)"
+ )
+
+ async def ask(*args, **kwargs):
+ args[4](decision)
+ if failure != "record":
+ error = asyncio.CancelledError() if failure == "cancel" else OSError("Review failed")
+ raise error
+ return decision
+
+ runtime = SimpleNamespace(
+ contract=contract,
+ model=SimpleNamespace(ask=ask),
+ files=SimpleNamespace(put=AsyncMock()),
+ resources=SimpleNamespace(name="read_skill_resource"),
+ failures=[],
+ metrics=Counter(),
+ )
+ if failure == "record":
+ runtime.files.put.side_effect = OSError("Record failed")
+ args = (runtime, Node("mapped", "map", "sources", task="extract"), [], "", set(), 0, "", {})
+ if failure == "cancel":
+ with pytest.raises(asyncio.CancelledError):
+ await review.run(*args)
+ else:
+ assert await review.run(*args) is None
+ assert runtime.contract is contract
+
+
+def response(name, arguments):
+ """Return one scripted reader or final-result tool call."""
+ return LLMResponse(None, [ToolCallRequest("call", name, arguments, 0)])
+
+
[email protected]("case", ["repeat", "progress", "error"])
+async def test_direct_reads_stop_repetition_but_allow_progress(case):
+ offsets = [n // 2 for n in range(102)] if case == "progress" else list(range(4))
+ files = SimpleNamespace(put=AsyncMock())
+ responses = [response("read", {"offset": n}) for n in offsets]
+ responses.append(response("emit", {"action": "continue"}))
+ provider = SimpleNamespace(thinking=True, chat=AsyncMock(side_effect=responses))
+ model = JsonModel(provider, "test", 0, files, CompileLimits(), {}, Counter())
+ reader = SimpleNamespace(
+ name="read", description="", parameters={}, execute=AsyncMock(return_value="x")
+ )
+ if case == "progress":
+ reader.execute.side_effect = [f"chunk{n}" for n in offsets]
+ if case == "error":
+ reader.execute.side_effect = [
+ error(f"request_id={i}")
+ for i, error in enumerate((OSError, ValueError, TimeoutError, PermissionError))
+ ]
+ result = model.direct("map", "", {}, ReviewDecision, None, "test", readers={"read": reader})
+ if case == "progress":
+ assert (await result).action == "continue"
+ else:
+ with pytest.raises(ModelCallLimitError, match="made no progress|failed repeatedly"):
+ await result
+ assert provider.chat.await_count == (len(offsets) + 1 if case == "progress" else 4)
+ assert provider.chat.await_args.kwargs["thinking"] is False
diff --git a/bot/vikingbot/compile/entry.py b/bot/vikingbot/compile/entry.py
index 97dacf6d2..8768dceb8 100644
--- a/bot/vikingbot/compile/entry.py
+++ b/bot/vikingbot/compile/entry.py
@@ -4,9 +4,10 @@ from typing import Literal
from pydantic import Field
+from openviking.models.model_error import ModelErrorKind, model_error_kind
from vikingbot.compile import file_ops
from vikingbot.compile.ops.common import payload
-from vikingbot.compile.pipeline_io import ModelCallError
+from vikingbot.compile.pipeline_io import ModelCallError, ModelCallLimitError
from vikingbot.compile.results import FileResponse, Group
@@ -55,7 +56,15 @@ async def run(runtime, sources, context):
try:
response = await runtime.model.ask("compile", system, data, EntryResponse, validate)
- except (ModelCallError, ValueError):
+ except ModelCallError as exc:
+ runtime.direct_output_format = None
+ if not isinstance(exc, ModelCallLimitError) and model_error_kind(exc) not in {
+ ModelErrorKind.INPUT_TOO_LARGE,
+ ModelErrorKind.TRANSIENT_NETWORK_ERROR,
+ }:
+ raise
+ return None
+ except ValueError:
runtime.direct_output_format = None
return None
if response.needs_pipeline or any(
diff --git a/bot/vikingbot/compile/models.py b/bot/vikingbot/compile/models.py
index f8d4f6908..ebb503528 100644
--- a/bot/vikingbot/compile/models.py
+++ b/bot/vikingbot/compile/models.py
@@ -37,10 +37,6 @@ class CompileLimits(BaseModel):
agent_iterations: int = 120
# Per-child model/tool rounds, including draft checks and submission; parent budget is separate.
subagent_iterations: int = Field(default=70, ge=1)
- # Wall-clock limits include provider capacity waits, transport retries and tool reads.
- pipeline_call_seconds: float = Field(default=600, gt=0)
- pipeline_plan_seconds: float = Field(default=600, gt=0)
- pipeline_agent_seconds: float = Field(default=1200, gt=0)
# Per-task Map workers, also used for Resource source subagents.
source_concurrency: int = Field(default=6, ge=1)
# Per-task Shuffle routing workers and embedding batches.
diff --git a/bot/vikingbot/compile/pipeline_io.py b/bot/vikingbot/compile/pipeline_io.py
index c51d185b6..817193361 100644
--- a/bot/vikingbot/compile/pipeline_io.py
+++ b/bot/vikingbot/compile/pipeline_io.py
@@ -103,7 +103,11 @@ def retry_allowed(failures: Counter, error: str) -> bool:
class ModelCallError(OSError):
- """A model timeout or provider failure; output validation errors remain separate."""
+ """A failed model request or stalled read loop; output validation errors remain separate."""
+
+
+class ModelCallLimitError(ModelCallError):
+ """A local deadline or no-progress limit allows direct generation to use the pipeline."""
class TaskFiles:
@@ -228,20 +232,20 @@ class JsonModel:
self.identity.update(
batch_target_chars=self.budget, output_reserve_tokens=self.output_tokens
)
- self.identity["thinking"] = {"compile": True}
+ self.identity["thinking"] = {
+ "map": False,
+ "route": False,
+ "reduce": False,
+ "combine": False,
+ }
underlying = getattr(provider, "_provider", provider)
backend = getattr(underlying, "_vlm", underlying)
+ backend = getattr(backend, "primary", backend)
+ self.timeout = getattr(backend, "timeout", 600.0)
self.identity["backend_settings"] = {
name: getattr(backend, name, None)
for name in ("model", "thinking", "temperature", "max_tokens", "reasoning_effort")
}
- # Explicit Ark configuration controls direct generation without changing other stages.
- self.identity["reasoning_effort"] = (
- {"compile": backend.reasoning_effort}
- if getattr(backend, "provider", None) == "volcengine"
- and getattr(backend, "reasoning_effort", None) is not None
- else {}
- )
configured_tokens = self.identity["backend_settings"]["max_tokens"]
self.max_tokens = (
configured_tokens
@@ -276,12 +280,17 @@ class JsonModel:
self.metrics[f"{stage}_calls"] += 1
number = self.metrics[f"{stage}_calls"]
started = time.monotonic()
+ seconds = max(600, self.timeout)
+ thinking = self.identity["thinking"].get(stage.split("_", 1)[0])
report = {
"stage": stage,
"call": number,
"max_tokens": max_tokens,
- "timeout_seconds": self.limits.pipeline_call_seconds,
- "thinking": self.identity["thinking"].get(stage, False),
+ "timeout_seconds": seconds,
+ "thinking": thinking
+ if thinking is not None
+ else self.identity["backend_settings"]["thinking"],
+ "reasoning_effort": self.identity["backend_settings"]["reasoning_effort"],
"message_chars": sum(len(json.dumps(m, ensure_ascii=False)) for m in messages),
"tool_schema_chars": len(json.dumps(tools, ensure_ascii=False)),
"tool_result_chars": sum(
@@ -295,19 +304,14 @@ class JsonModel:
json.dumps([messages, tools], ensure_ascii=False), text_type="mixed"
)
try:
- completion_overrides = {}
- if stage in self.identity["reasoning_effort"]:
- report["reasoning_effort"] = self.identity["reasoning_effort"][stage]
- completion_overrides["reasoning_effort"] = report["reasoning_effort"]
- async with asyncio.timeout(self.limits.pipeline_call_seconds):
+ async with asyncio.timeout(seconds):
response = await self.provider.chat(
messages=messages,
tools=tools,
model=self.model,
temperature=self.temperature,
max_tokens=max_tokens,
- thinking=report["thinking"],
- **completion_overrides,
+ thinking=thinking,
)
report.update(
finish_reason=response.finish_reason,
@@ -331,8 +335,8 @@ class JsonModel:
return response
except TimeoutError as exc:
report["error"] = "TimeoutError"
- raise ModelCallError(
- f"{stage}: model call wall-clock budget exhausted ({self.limits.pipeline_call_seconds:g}s)"
+ raise ModelCallLimitError(
+ f"{stage}: model call wall-clock budget exhausted ({seconds:g}s)"
) from exc
except Exception as exc:
report["error"] = type(exc).__name__
@@ -356,21 +360,20 @@ class JsonModel:
agent=False,
readers=None,
) -> R:
- """Bound an assignment; readers adds scoped tools for direct calls without widening worker access."""
- seconds = (
- self.limits.pipeline_plan_seconds
- if stage in {"plan", "review"}
- else self.limits.pipeline_agent_seconds
- )
+ """Bound each assignment to twice the model timeout, with a 1200-second minimum."""
+ seconds = max(1200, 2 * self.timeout)
try:
async with asyncio.timeout(seconds):
return await self._ask(
stage, system, data, schema, validate, agent=agent, readers=readers
)
except TimeoutError as exc:
- raise ModelCallError(f"{stage}: wall-clock budget exhausted ({seconds:g}s)") from exc
+ raise ModelCallLimitError(
+ f"{stage}: assignment wall-clock budget exhausted ({seconds:g}s)"
+ ) from exc
async def _ask(self, stage, system, data, schema, validate, *, agent, readers):
+ """Run a stage with scoped readers; return validated output and cache eligible results."""
if schema is FileResponse and stage != "skill_repair":
# Inline original excerpts when located; unlocated records retain full shards.
# The scoped reader can expand partial evidence without catalog access.
@@ -429,7 +432,7 @@ class JsonModel:
return result
async def direct(self, stage, system, data, schema, validate, key, *, readers=None):
- """Allow scoped reads; each Review gets three repairs, Skill repair three submissions."""
+ """Return validated output; reject three consecutive rounds of repeated tool results."""
tools = [
{
"type": "function",
@@ -477,6 +480,8 @@ class JsonModel:
]
failures = 0
retries = Counter()
+ seen_results = set()
+ stalled_rounds = 0
while True:
response = await self.call(stage, messages, tools, max_tokens=self.max_tokens)
raw = None
@@ -507,12 +512,34 @@ class JsonModel:
],
}
)
+ progress = False
for call in calls:
try:
value = await readers[call.name].execute(**call.arguments)
except (ValueError, OSError, NotFoundError) as exc:
+ # Changing error details do not reset a reader's failure budget.
+ if not retry_allowed(retries, f"read:{call.name}"):
+ raise ModelCallLimitError(
+ f"{stage}: reader {call.name} failed repeatedly"
+ ) from exc
value = "Error: " + str(exc)[:800]
+ else:
+ retries.pop(f"read:{call.name}", None)
+ try:
+ content = json.loads(
+ value, object_hook=lambda v: v.get("text", v.get("excerpts", v))
+ )
+ except (json.JSONDecodeError, TypeError):
+ content = value
+ fingerprint = digest([call.name, content])
+ progress = progress or fingerprint not in seen_results
+ if fingerprint in seen_results:
+ value = "Unchanged: same tool result. Use previous results or submit."
+ seen_results.add(fingerprint)
messages.append({"role": "tool", "tool_call_id": call.id, "content": value})
+ stalled_rounds = 0 if progress else stalled_rounds + 1
+ if stalled_rounds >= 3:
+ raise ModelCallLimitError(f"{stage}: tool calls made no progress")
continue
if calls:
if len(calls) != 1 or calls[0].name != "emit":
@@ -564,7 +591,7 @@ class JsonModel:
)
self.metrics["validation_failures"] += 1
if stage == "compile" and category == "truncated":
- raise ModelCallError(f"{stage}: {error}") from exc
+ raise ValueError(f"{stage}: {error}") from exc
# Routing owns retries per primary record, including malformed responses.
if schema is RouteBatchResponse or (
failures > 3
diff --git a/bot/vikingbot/compile/review.py b/bot/vikingbot/compile/review.py
index 91950c4aa..09bcecc96 100644
--- a/bot/vikingbot/compile/review.py
+++ b/bot/vikingbot/compile/review.py
@@ -2,9 +2,12 @@
from __future__ import annotations
+import asyncio
import json
from dataclasses import asdict
+from loguru import logger
+
from vikingbot.agent.tools.base import Tool
from vikingbot.compile import file_ops
from vikingbot.compile.plan import PLANNING_RULES, ReviewDecision, parse_plan
@@ -175,12 +178,7 @@ class StageOutputReader(Tool):
async def run(runtime, node, outputs, remaining_plan, completed_names, revisions, prompt, context):
- """Return an accepted decision and parsed suffix, or None to retain the existing plan.
-
- Only future configurations may change; output format and scope meanings remain fixed.
- Invalid decisions get up to three repairs per review; exhausted or model/read failures
- are recorded. Cancellation and diagnostic-write failures propagate; the contract stays fixed.
- """
+ """Return a validated revision or None; cancellation propagates."""
record = {}
accepted = None
try:
@@ -245,8 +243,18 @@ async def run(runtime, node, outputs, remaining_plan, completed_names, revisions
)
record["decision"] = decision.model_dump(exclude_none=True)
record["status"] = "accepted" if accepted else "continued"
- except (OSError, ValueError, TypeError) as exc:
+ except Exception as exc:
+ if asyncio.current_task().cancelling():
+ raise asyncio.CancelledError() from exc
+ accepted = None
record.update(status="rejected", error=str(exc)[:800])
runtime.metrics["review_rejections"] += 1
- await runtime.files.put(f"reviews/{node.name}", record)
+ logger.warning("Compile stage review {} failed: {}", node.name, exc)
+ try:
+ await runtime.files.put(f"reviews/{node.name}", record)
+ except Exception as exc:
+ if asyncio.current_task().cancelling():
+ raise asyncio.CancelledError() from exc
+ logger.warning("Compile stage review {} record write failed: {}", node.name, exc)
+ return None
return accepted
diff --git a/bot/vikingbot/compile/service.py b/bot/vikingbot/compile/service.py
index 5b0f7f25f..fc38dbc00 100644
--- a/bot/vikingbot/compile/service.py
+++ b/bot/vikingbot/compile/service.py
@@ -193,11 +193,7 @@ class CompileCapabilities:
class _CompileProvider(LLMProvider):
- """Share model-call capacity across Compile tasks, including streams and compaction.
-
- A slot covers a complete model response and is released on failure or cancellation.
- Workspace tools and waits do not hold slots. The underlying provider owns retries.
- """
+ """Share model-call capacity across Compile tasks."""
def __init__(self, provider: LLMProvider, slots: asyncio.Semaphore):
super().__init__()
@@ -205,12 +201,12 @@ class _CompileProvider(LLMProvider):
self._slots = slots
async def chat(self, *args: Any, **kwargs: Any) -> LLMResponse:
- """Forward one non-streaming request under the shared capacity limit."""
+ """Forward a request under the shared capacity limit."""
async with self._slots:
return await self._provider.chat(*args, **kwargs)
async def chat_stream(self, *args: Any, **kwargs: Any) -> AsyncIterator[LLMStreamEvent]:
- """Hold capacity until the response stream finishes or its consumer closes it."""
+ """Hold capacity until the stream closes."""
async with self._slots:
async with aclosing(self._provider.chat_stream(*args, **kwargs)) as stream:
async for event in stream:
@@ -246,7 +242,7 @@ class BotCompileService:
)
self._semaphore = asyncio.Semaphore(self.limits.concurrent_tasks)
self._model_slots = asyncio.Semaphore(model_concurrency)
- self._target_locks: dict[str, tuple[asyncio.Lock, int]] = {}
+ self._target_locks: dict[tuple[str, str], tuple[asyncio.Lock, int]] = {}
self._target_locks_guard = asyncio.Lock()
self._admission_guard = asyncio.Lock()
self._admitted_tasks = 0
@@ -315,6 +311,11 @@ class BotCompileService:
stage="queued",
)
connection = connection or {}
+ account_id = connection.get("account_id")
+ if self._openviking_auth_mode() != "dev" and not (account_id or "").strip():
+ raise CompileFailure(
+ "UNAUTHENTICATED", "Compile requires account_id.", stage="queued"
+ )
normalized_request = await self._normalize_request(request, connection=connection)
task_id = task_id or "cmp_" + uuid.uuid4().hex
now = utc_now()
@@ -638,13 +639,15 @@ class BotCompileService:
)
return parts[-1], "viking://" + "/".join(parts[: index + 1])
- async def _retain_target_lock(self, target: str) -> asyncio.Lock:
+ async def _retain_target_lock(self, target: tuple[str, str]) -> asyncio.Lock:
+ """Retain a task lock keyed by effective account and canonical target URI."""
async with self._target_locks_guard:
lock, references = self._target_locks.get(target, (asyncio.Lock(), 0))
self._target_locks[target] = (lock, references + 1)
return lock
- async def _release_target_lock(self, target: str, lock: asyncio.Lock) -> None:
+ async def _release_target_lock(self, target: tuple[str, str], lock: asyncio.Lock) -> None:
+ """Remove an account-target lock after its last active or waiting task exits."""
async with self._target_locks_guard:
current, references = self._target_locks.get(target, (lock, 0))
if current is not lock:
@@ -668,7 +671,11 @@ class BotCompileService:
request: SanitizedCompileRequest,
connection: dict[str, Any],
) -> None:
- task_lock = await self._retain_target_lock(request.to)
+ account_id = (
+ "default" if self._openviking_auth_mode() == "dev" else connection["account_id"]
+ )
+ target = (account_id, request.to)
+ task_lock = await self._retain_target_lock(target)
acquired = False
try:
await self._acquire_execution_slot(task_lock)
@@ -688,7 +695,7 @@ class BotCompileService:
if acquired:
self._semaphore.release()
task_lock.release()
- await self._release_target_lock(request.to, task_lock)
+ await self._release_target_lock(target, task_lock)
async def _execute_task(
self,
@@ -1362,9 +1369,7 @@ class BotCompileService:
skill_name, str(skill_dir), target_uri=target_uri, wait=True
)
else:
- published = await client.add_skill(
- str(skill_dir), target_uri=target_uri, wait=True
- )
+ published = await client.add_skill(str(skill_dir), target_uri=target_uri, wait=True)
result["updated" if exists else "created"].append(
str(published.get("root_uri") or published.get("uri") or root_uri)
)
diff --git a/bot/vikingbot/config/loader.py b/bot/vikingbot/config/loader.py
index c485c99ee..8bf74e40c 100644
--- a/bot/vikingbot/config/loader.py
+++ b/bot/vikingbot/config/loader.py
@@ -157,6 +157,8 @@ def load_config() -> Config:
vlm_data,
inherit_model=not bot_vlm_explicit,
)
+ if not bot_vlm_explicit:
+ bot_data["agents"].setdefault("thinking", root_vlm_config.thinking)
server_managed = _is_truthy_env(VIKINGBOT_WITH_OPENVIKING_SERVER_ENV)
configured_bot_server = bot_data.get("ov_server", {})
diff --git a/bot/vikingbot/providers/vlm_adapter.py b/bot/vikingbot/providers/vlm_adapter.py
index f47575a32..6fed37aed 100644
--- a/bot/vikingbot/providers/vlm_adapter.py
+++ b/bot/vikingbot/providers/vlm_adapter.py
@@ -5,20 +5,21 @@ LLMProvider interface, so that bot.agents.provider / bot.agents.model
configuration semantics are consistent with openviking server's vlm section.
"""
-import asyncio
import json
import re
import time
import traceback
from collections.abc import AsyncIterator, Mapping
+from contextlib import aclosing
from typing import Any
from loguru import logger
from openviking.models.model_error import model_error_kind
from openviking.models.vlm.backends.volcengine_vlm import build_volcengine_request_headers
-from openviking.utils.model_retry import is_retryable_rate_limit_error, rate_limit_retry_delay
+from openviking.utils.model_retry import effective_max_retries, retry_async
from openviking.utils.multimodal import redact_image_data_urls
+from openviking_cli.utils import get_logger
from vikingbot.integrations.langfuse import LangfuseClient
from vikingbot.providers.base import (
LLMProvider,
@@ -286,7 +287,6 @@ class VLMProviderAdapter(LLMProvider):
)
# --- Call VLM backend ---
- attempt = 1
# An explicit empty list asks VLM backends for a structured response
# without exposing tools, so per-response usage is preserved.
response_tools = tools if tools is not None else []
@@ -296,31 +296,14 @@ class VLMProviderAdapter(LLMProvider):
if reasoning_effort is not None and self._vlm.provider == "volcengine"
else {}
)
- while True:
- try:
- result = await self._vlm.get_completion_async(
- messages=messages,
- thinking=thinking
- if thinking is not None
- else getattr(self._vlm, "thinking", None),
- tools=response_tools,
- tool_choice="auto" if response_tools else None,
- max_tokens=max_tokens,
- **completion_overrides,
- )
- break
- except Exception as e:
- if not is_retryable_rate_limit_error(e):
- raise
- delay = rate_limit_retry_delay(attempt)
- logger.warning(
- "VLM adapter chat rate limited; retrying attempt={} delay={:.1f}s error={}",
- attempt,
- delay,
- e,
- )
- await asyncio.sleep(delay)
- attempt += 1
+ result = await self._vlm.get_completion_async(
+ messages=messages,
+ thinking=thinking if thinking is not None else getattr(self._vlm, "thinking", None),
+ tools=response_tools,
+ tool_choice="auto" if response_tools else None,
+ max_tokens=max_tokens,
+ **completion_overrides,
+ )
llm_response = self._convert_response(result)
@@ -364,13 +347,15 @@ class VLMProviderAdapter(LLMProvider):
try:
stream_with_failover = getattr(self._vlm, "stream_with_failover", None)
if callable(stream_with_failover):
- async for event in self._chat_stream_failover_with_retry(
- stream_with_failover,
- messages=messages,
- tools=tools,
- model=model,
- max_tokens=max_tokens,
- temperature=temperature,
+ async for event in stream_with_failover(
+ lambda vlm: self._chat_stream_with_retry(
+ vlm,
+ messages=messages,
+ tools=tools,
+ model=model,
+ max_tokens=max_tokens,
+ temperature=temperature,
+ )
):
yield event
return
@@ -440,47 +425,6 @@ class VLMProviderAdapter(LLMProvider):
getattr(vlm, "get_async_client", None)
)
- async def _chat_stream_failover_with_retry(
- self,
- stream_with_failover: Any,
- *,
- messages: list[dict[str, Any]],
- tools: list[dict[str, Any]] | None,
- model: str | None,
- max_tokens: int | None,
- temperature: float,
- ) -> AsyncIterator[LLMStreamEvent]:
- attempt = 1
- while True:
- emitted = False
- try:
- async for event in stream_with_failover(
- lambda vlm: self._chat_stream_backend(
- vlm,
- messages=messages,
- tools=tools,
- model=model,
- max_tokens=max_tokens,
- temperature=temperature,
- )
- ):
- emitted = True
- yield event
- return
- except Exception as exc:
- if emitted or not is_retryable_rate_limit_error(exc):
- raise
- delay = rate_limit_retry_delay(attempt)
- logger.warning(
- "VLM credential stream rate limited; retrying attempt={} "
- "delay={:.1f}s error={}",
- attempt,
- delay,
- exc,
- )
- await asyncio.sleep(delay)
- attempt += 1
-
async def _chat_stream_with_retry(
self,
vlm: Any,
@@ -490,33 +434,35 @@ class VLMProviderAdapter(LLMProvider):
max_tokens: int | None,
temperature: float,
) -> AsyncIterator[LLMStreamEvent]:
- attempt = 1
- while True:
- emitted = False
+ """Retry before the first event; never replay emitted output."""
+
+ async def start_stream():
+ """Return the stream and first event; close failed attempts."""
+ stream = self._chat_stream_backend(
+ vlm,
+ messages=messages,
+ tools=tools,
+ model=model,
+ max_tokens=max_tokens,
+ temperature=temperature,
+ )
try:
- async for event in self._chat_stream_backend(
- vlm,
- messages=messages,
- tools=tools,
- model=model,
- max_tokens=max_tokens,
- temperature=temperature,
- ):
- emitted = True
- yield event
- return
- except Exception as exc:
- if emitted or not is_retryable_rate_limit_error(exc):
- raise
- delay = rate_limit_retry_delay(attempt)
- logger.warning(
- "VLM adapter stream rate limited; retrying attempt={} delay={:.1f}s error={}",
- attempt,
- delay,
- exc,
- )
- await asyncio.sleep(delay)
- attempt += 1
+ return stream, await anext(stream)
+ except BaseException:
+ await stream.aclose()
+ raise
+
+ stream, first = await retry_async(
+ start_stream,
+ max_retries=effective_max_retries(getattr(vlm, "max_retries", 0)),
+ logger=get_logger(__name__),
+ operation_name=f"{vlm.provider} VLM stream",
+ model_errors_only=True,
+ )
+ async with aclosing(stream):
+ yield first
+ async for event in stream:
+ yield event
async def _chat_stream_backend(
self,
@@ -528,6 +474,7 @@ class VLMProviderAdapter(LLMProvider):
max_tokens: int | None,
temperature: float,
) -> AsyncIterator[LLMStreamEvent]:
+ """Release the HTTP response after normal or failed SDK iteration."""
kwargs = self._build_stream_kwargs(
vlm,
messages=messages,
@@ -545,39 +492,40 @@ class VLMProviderAdapter(LLMProvider):
client = vlm.get_async_client()
response = await client.chat.completions.create(**kwargs)
- async for chunk in response:
- chunk_usage = self._parse_usage(getattr(chunk, "usage", None))
- if chunk_usage:
- usage = chunk_usage
-
- choices = getattr(chunk, "choices", None) or []
- if not choices:
- continue
- choice = choices[0]
- if getattr(choice, "finish_reason", None):
- finish_reason = choice.finish_reason or finish_reason
- delta = getattr(choice, "delta", None)
- if delta is None:
- continue
-
- reasoning_delta = stream_delta_value(delta, "reasoning_content")
- if reasoning_delta:
- reasoning_parts.append(reasoning_delta)
- yield LLMStreamEvent(type="reasoning_delta", content=reasoning_delta)
-
- content_delta = stream_delta_value(delta, "content")
- if content_delta:
- content_parts.append(content_delta)
- yield LLMStreamEvent(type="content_delta", content=content_delta)
-
- for fallback_index, delta_tool_call in enumerate(
- getattr(delta, "tool_calls", None) or []
- ):
- merge_stream_tool_call_delta(
- tool_calls,
- delta_tool_call,
- fallback_index=fallback_index,
- )
+ async with response as stream:
+ async for chunk in stream:
+ chunk_usage = self._parse_usage(getattr(chunk, "usage", None))
+ if chunk_usage:
+ usage = chunk_usage
+
+ choices = getattr(chunk, "choices", None) or []
+ if not choices:
+ continue
+ choice = choices[0]
+ if getattr(choice, "finish_reason", None):
+ finish_reason = choice.finish_reason or finish_reason
+ delta = getattr(choice, "delta", None)
+ if delta is None:
+ continue
+
+ reasoning_delta = stream_delta_value(delta, "reasoning_content")
+ if reasoning_delta:
+ reasoning_parts.append(reasoning_delta)
+ yield LLMStreamEvent(type="reasoning_delta", content=reasoning_delta)
+
+ content_delta = stream_delta_value(delta, "content")
+ if content_delta:
+ content_parts.append(content_delta)
+ yield LLMStreamEvent(type="content_delta", content=content_delta)
+
+ for fallback_index, delta_tool_call in enumerate(
+ getattr(delta, "tool_calls", None) or []
+ ):
+ merge_stream_tool_call_delta(
+ tool_calls,
+ delta_tool_call,
+ fallback_index=fallback_index,
+ )
if usage:
self._record_vlm_usage(vlm, usage, time.perf_counter() - start_time)
@@ -625,6 +573,9 @@ class VLMProviderAdapter(LLMProvider):
}
if effective_max_tokens is not None:
kwargs["max_tokens"] = effective_max_tokens
+ reasoning_effort = getattr(vlm, "reasoning_effort", None)
+ if getattr(vlm, "thinking", False) and reasoning_effort is not None:
+ kwargs["reasoning_effort"] = reasoning_effort
kwargs["extra_headers"] = build_volcengine_request_headers(
getattr(vlm, "extra_headers", None)
)
diff --git a/docs/en/guides/01-configuration.md b/docs/en/guides/01-configuration.md
index 1ec973366..37997fa18 100644
--- a/docs/en/guides/01-configuration.md
+++ b/docs/en/guides/01-configuration.md
@@ -943,6 +943,8 @@ Set the number of concurrent workers per Resource Compile task and stage, for bo
Concurrency values must be positive integers; omitted or `null` values inherit the default. `shuffle_batch_size` must be a positive integer, defaults to `4`, and does not accept `null`. Routing saves valid decisions individually and re-batches only failed records; the final retry uses single-record requests, with at most three rounds per record. Restart VikingBot after changing these settings. All Compile tasks within one VikingBot service still share the model-request capacity set by `vlm.max_concurrent`, so raising stage concurrency cannot exceed that total limit. Embedding requests also obey the embedding service's own concurrency limit. Recovery uses the same reduce setting by default; an explicit `--concurrency` overrides recovery concurrency.
+Compile inherits the model's thinking and reasoning settings. Model requests use the effective model timeout (`bot.agents.timeout`, or inherited `vlm.timeout`).
+
### query_planner
Optional lightweight model for retrieval intent analysis and query planning. It uses the same configuration shape as `vlm` for `search()` intent analysis, query expansion, and optional server recall-digest rewriting. If `query_planner` is omitted or empty, OpenViking falls back to `vlm` for backward compatibility.
diff --git a/docs/zh/guides/01-configuration.md b/docs/zh/guides/01-configuration.md
index 3bd9e50ce..a6244251d 100644
--- a/docs/zh/guides/01-configuration.md
+++ b/docs/zh/guides/01-configuration.md
@@ -937,6 +937,8 @@ VLM 的 `model` 填写对应的方舟模型 endpoint ID。`video_fps` 仅用于
三个并发配置项必须为正整数;省略或设为 `null` 时继承默认值。`shuffle_batch_size` 必须为正整数,省略时为 `4`,不接受 `null`。路由逐条保存合法结果,仅失败记录重新组批重试,最后一次降为单条;每条记录最多尝试三轮。修改后需重启 VikingBot 服务。一个 VikingBot 服务内的所有 Compile 任务仍共享 `vlm.max_concurrent` 限制的模型请求容量,因此提高阶段并发不会突破这一总上限。Embedding 请求另受 embedding 服务的并发限制。恢复任务默认使用相同的 reduce 配置,显式 `--concurrency` 可覆盖恢复并发数。
+Compile 继承模型的 thinking 和 reasoning 配置。模型请求使用有效模型超时配置(`bot.agents.timeout` 或继承的 `vlm.timeout`)。
+
### query_planner
可选的轻量模型配置,用于检索前的意图分析和 query 规划/改写。配置结构与 `vlm` 相同,用于 `search()` 的意图分析、query expansion,以及可选的服务端 recall 摘要重写。未配置或配置为空时,OpenViking 会回退到 `vlm`,保持向后兼容。
diff --git a/openviking/models/vlm/backends/volcengine_vlm.py b/openviking/models/vlm/backends/volcengine_vlm.py
index c57c8568d..38460a363 100644
--- a/openviking/models/vlm/backends/volcengine_vlm.py
+++ b/openviking/models/vlm/backends/volcengine_vlm.py
@@ -200,7 +200,7 @@ class VolcEngineVLM(OpenAIVLM):
tool_choice: Optional[str] = None,
messages: Optional[List[Dict[str, Any]]] = None,
) -> Union[str, VLMResponse]:
- """Get text completion via Chat Completions API."""
+ """Get text completion; configured effort applies only when thinking is enabled."""
effective_thinking = self.thinking if thinking is None else thinking
kwargs_messages = sanitize_openai_messages(
messages or [{"role": "user", "content": prompt}]
@@ -216,6 +216,8 @@ class VolcEngineVLM(OpenAIVLM):
kwargs["extra_body"] = dict(self.extra_request_body)
if self.max_tokens is not None:
kwargs["max_tokens"] = self.max_tokens
+ if effective_thinking and self.reasoning_effort is not None:
+ kwargs["reasoning_effort"] = self.reasoning_effort
if tools:
kwargs["tools"] = tools
kwargs["tool_choice"] = tool_choice or "auto"
@@ -245,8 +247,10 @@ class VolcEngineVLM(OpenAIVLM):
max_tokens: Optional[int] = None,
reasoning_effort: Optional[str] = None,
) -> Union[str, VLMResponse]:
- """Get text completion; per-call reasoning effort applies only with thinking enabled."""
+ """Get text completion; explicit effort overrides configured effort when thinking is enabled."""
effective_thinking = self.thinking if thinking is None else thinking
+ if reasoning_effort is None:
+ reasoning_effort = self.reasoning_effort
effective_max_tokens = max_tokens if max_tokens is not None else self.max_tokens
kwargs_messages = sanitize_openai_messages(
messages or [{"role": "user", "content": prompt}]
diff --git a/tests/unit/test_vikingbot_vlm_adapter_retry.py b/tests/unit/test_vikingbot_vlm_adapter_retry.py
index b05a39932..9232c84d4 100644
--- a/tests/unit/test_vikingbot_vlm_adapter_retry.py
+++ b/tests/unit/test_vikingbot_vlm_adapter_retry.py
@@ -1,3 +1,4 @@
+import json
from collections import Counter
from types import SimpleNamespace
from unittest.mock import AsyncMock
@@ -5,13 +6,14 @@ from unittest.mock import AsyncMock
import httpx
import pytest
import vikingbot.providers.vlm_adapter as vlm_adapter
+from vikingbot.compile.models import CompileLimits
from vikingbot.compile.pipeline_io import JsonModel, ModelCallError
from vikingbot.providers.vlm_adapter import VLMProviderAdapter
from volcenginesdkarkruntime._exceptions import (
ArkAPITimeoutError,
ArkBadRequestError,
- ArkRateLimitError,
)
+from volcenginesdkarkruntime._streaming import AsyncStream
from openviking.models.model_error import ModelError, ModelErrorKind, model_error_kind
from openviking.models.vlm.backends.openai_vlm import OpenAIVLM
@@ -21,7 +23,7 @@ from openviking.models.vlm.backends.volcengine_vlm import (
VolcEngineVLM,
build_volcengine_request_headers,
)
-from openviking.utils.model_retry import is_retryable_rate_limit_error
+from openviking.models.vlm.retry_vlm import RetryVLM
class _DisabledLangfuse:
@@ -48,18 +50,6 @@ class _FakeVolcEngineFailoverVLM(_FakeVLM):
thinking = False
-class _AsyncChunks:
- def __init__(self, chunks):
- self._chunks = chunks
-
- def __aiter__(self):
- return self._iter()
-
- async def _iter(self):
- for chunk in self._chunks:
- yield chunk
-
-
class _FakeStreamingCompletions:
def __init__(self, failures: list[Exception], chunks):
self.failures = list(failures)
@@ -72,7 +62,9 @@ class _FakeStreamingCompletions:
self.kwargs.append(kwargs)
if self.failures:
raise self.failures.pop(0)
- return _AsyncChunks(self.chunks)
+ response = AsyncMock()
+ response.__aenter__.return_value.__aiter__.return_value = self.chunks
+ return response
class _FakeStreamingVLM:
@@ -80,7 +72,8 @@ class _FakeStreamingVLM:
model = "test-model"
thinking = False
- def __init__(self, completions: _FakeStreamingCompletions):
+ def __init__(self, completions: _FakeStreamingCompletions, max_retries: int = 2):
+ self.max_retries = max_retries
self._client = SimpleNamespace(
chat=SimpleNamespace(completions=completions),
)
@@ -98,50 +91,32 @@ class _CaptureLogger:
@pytest.mark.asyncio
-async def test_chat_retries_rate_limit_until_success(monkeypatch):
- sleep_delays: list[float] = []
-
- async def _sleep(delay: float):
- sleep_delays.append(delay)
-
- monkeypatch.setattr(vlm_adapter, "rate_limit_retry_delay", lambda attempt: attempt)
- monkeypatch.setattr(vlm_adapter.asyncio, "sleep", _sleep)
-
- fake_vlm = _FakeVLM(
- [
- RuntimeError("Error code: 429 - ModelAccountTpmRateLimitExceeded"),
- RuntimeError("TooManyRequests: rate limit"),
- ],
- result="done",
[email protected]("max_retries,expected_calls", [(0, 1), (2, 3)])
+async def test_chat_preserves_model_retry_budget(monkeypatch, max_retries, expected_calls):
+ monkeypatch.setattr("openviking.utils.model_retry.asyncio.sleep", AsyncMock())
+ backend = VolcEngineVLM(
+ {"provider": "volcengine", "model": "test-model", "max_retries": max_retries}
+ )
+ error = ModelError(ModelErrorKind.RATE_LIMIT_ERROR, "rate limited", status_code=429)
+ completion = AsyncMock(side_effect=error)
+ monkeypatch.setattr(backend, "get_completion_async", completion)
+ adapter = VLMProviderAdapter(
+ RetryVLM(backend), "test-model", langfuse_client=_DisabledLangfuse()
)
- adapter = VLMProviderAdapter(fake_vlm, "test-model", langfuse_client=_DisabledLangfuse())
response = await adapter.chat(messages=[{"role": "user", "content": "hello"}])
- assert response.content == "done"
- assert response.finish_reason == "stop"
- assert fake_vlm.calls == 3
- assert sleep_delays == [1, 2]
+ assert response.finish_reason == "error"
+ assert response.error_kind is ModelErrorKind.RATE_LIMIT_ERROR
+ assert completion.await_count == expected_calls
@pytest.mark.asyncio
-async def test_chat_preserves_input_error_for_compile_without_retry(monkeypatch):
- async def _sleep(_delay: float):
- raise AssertionError("non-retryable errors must not sleep/retry")
-
- monkeypatch.setattr(vlm_adapter.asyncio, "sleep", _sleep)
-
+async def test_chat_preserves_input_error_for_compile_without_retry():
fake_vlm = _FakeVLM([ModelError(ModelErrorKind.INPUT_TOO_LARGE, "Input rejected")])
adapter = VLMProviderAdapter(fake_vlm, "test-model", langfuse_client=_DisabledLangfuse())
- model = JsonModel(
- adapter,
- "test-model",
- 0.0,
- SimpleNamespace(put=AsyncMock()),
- SimpleNamespace(merge_input_chars=16_000, pipeline_call_seconds=5),
- {},
- Counter(),
- )
+ files = SimpleNamespace(put=AsyncMock())
+ model = JsonModel(adapter, "test-model", 0.0, files, CompileLimits(), {}, Counter())
with pytest.raises(ModelCallError) as raised:
await model.call("reduce", [{"role": "user", "content": "hello"}], [], max_tokens=128)
@@ -224,14 +199,8 @@ async def test_chat_without_tools_preserves_usage_from_openai_backend(monkeypatc
@pytest.mark.asyncio
async def test_chat_stream_retries_rate_limit_until_success(monkeypatch):
- sleep_delays: list[float] = []
-
- async def _sleep(delay: float):
- sleep_delays.append(delay)
-
- monkeypatch.setattr(vlm_adapter, "rate_limit_retry_delay", lambda attempt: attempt)
- monkeypatch.setattr(vlm_adapter.asyncio, "sleep", _sleep)
-
+ sleep = AsyncMock()
+ monkeypatch.setattr("openviking.utils.model_retry.asyncio.sleep", sleep)
chunk = SimpleNamespace(
usage=None,
choices=[
@@ -242,7 +211,7 @@ async def test_chat_stream_retries_rate_limit_until_success(monkeypatch):
],
)
completions = _FakeStreamingCompletions(
- [RuntimeError("Error code: 429 - ModelAccountTpmRateLimitExceeded")],
+ [ModelError(ModelErrorKind.RATE_LIMIT_ERROR, "temporarily unavailable")],
[chunk],
)
adapter = VLMProviderAdapter(
@@ -259,13 +228,48 @@ async def test_chat_stream_retries_rate_limit_until_success(monkeypatch):
]
assert completions.calls == 2
- assert sleep_delays == [1]
+ sleep.assert_awaited_once()
assert [event.type for event in events] == ["content_delta", "response"]
assert events[0].content == "streamed"
assert events[1].response.content == "streamed"
assert events[1].response.finish_reason == "stop"
+ "code,expected_calls,kind",
+ [
+ ("ModelAccountTpmRateLimitExceeded", 3, ModelErrorKind.RATE_LIMIT_ERROR),
+ ("AccountQuotaExceeded", 1, ModelErrorKind.QUOTA_EXHAUSTED),
+ ],
+)
+async def test_chat_stream_obeys_retry_policy(monkeypatch, code, expected_calls, kind):
+ monkeypatch.setattr("openviking.utils.model_retry.asyncio.sleep", AsyncMock())
+ vlm = VolcEngineVLM({"model": "test-model", "api_key": "test-key", "max_retries": 2})
+ payload = (
+ "data: " + json.dumps({"error": {"code": code, "message": "limited"}}) + "\n\n"
+ ).encode()
+ responses = [
+ httpx.Response(
+ 200,
+ stream=httpx.ByteStream(payload),
+ request=httpx.Request("POST", "https://example.test"),
+ )
+ for _ in range(expected_calls)
+ ]
+ async with vlm.get_async_client() as client:
+ create = AsyncMock(
+ side_effect=[AsyncStream(cast_to=dict, response=r, client=client) for r in responses]
+ )
+ monkeypatch.setattr(client.chat.completions, "create", create)
+ adapter = VLMProviderAdapter(vlm, "test-model", langfuse_client=_DisabledLangfuse())
+ events = [event async for event in adapter.chat_stream(messages=[])]
+ assert create.await_count == expected_calls
+ assert all(response.is_closed for response in responses)
+ assert events[-1].response.finish_reason == "error"
+ assert events[-1].response.error_kind is kind
+
+
@pytest.mark.asyncio
async def test_chat_stream_routes_failover_wrapper_through_completion_api():
fake_vlm = _FakeVolcEngineFailoverVLM([], result="fallback-safe")
@@ -298,7 +302,7 @@ async def test_chat_stream_logs_timeout_cause_and_request_metadata(monkeypatch):
error.__cause__ = cause
completions = _FakeStreamingCompletions([error], [])
adapter = VLMProviderAdapter(
- _FakeStreamingVLM(completions),
+ _FakeStreamingVLM(completions, max_retries=0),
"test-model",
langfuse_client=_DisabledLangfuse(),
)
@@ -397,10 +401,14 @@ async def test_volcengine_stream_uses_unique_default_client_request_ids():
@pytest.mark.asyncio
-async def test_volcengine_non_stream_uses_unique_default_client_request_ids(monkeypatch):
+ "thinking,effort,expected",
+ [(True, None, "low"), (True, "high", "high"), (False, None, None), (False, "high", None)],
+)
+async def test_volcengine_request_settings_and_ids(monkeypatch, thinking, effort, expected):
calls: list[dict] = []
- async def create(**kwargs):
+ def create(**kwargs):
calls.append(kwargs)
return SimpleNamespace(
choices=[
@@ -418,20 +426,30 @@ async def test_volcengine_non_stream_uses_unique_default_client_request_ids(monk
"model": "test-model",
"api_key": "test-key",
"max_retries": 0,
+ "thinking": thinking,
+ "reasoning_effort": "low",
}
)
monkeypatch.setattr(
vlm,
"get_async_client",
- lambda: SimpleNamespace(chat=SimpleNamespace(completions=SimpleNamespace(create=create))),
+ lambda: SimpleNamespace(
+ chat=SimpleNamespace(completions=SimpleNamespace(create=AsyncMock(side_effect=create)))
+ ),
)
+ vlm._sync_client = vlm.get_async_client()
+ vlm._sync_client.chat.completions.create = create
for _ in range(2):
- assert await vlm.get_completion_async(messages=[{"role": "user", "content": "hi"}]) == "ok"
+ assert await vlm.get_completion_async("hi", reasoning_effort=effort) == "ok"
+ if effort is None:
+ assert vlm.get_completion("hi") == "ok"
request_ids = [kwargs["extra_headers"][VOLCENGINE_CLIENT_REQUEST_ID_HEADER] for kwargs in calls]
- assert len(set(request_ids)) == 2
+ assert len(set(request_ids)) == len(calls)
assert all(value.startswith(f"{VOLCENGINE_CLIENT_REQUEST_ID},") for value in request_ids)
+ assert all(kwargs.get("reasoning_effort") == expected for kwargs in calls)
+ assert all(("reasoning_effort" in kwargs) == thinking for kwargs in calls)
def test_volcengine_request_headers_preserve_custom_client_request_id():
@@ -448,43 +466,6 @@ def test_volcengine_request_headers_preserve_custom_client_request_id():
}
-def test_rate_limit_classifier_handles_target_error():
- assert is_retryable_rate_limit_error(
- RuntimeError("Error code: 429 - ModelAccountTpmRateLimitExceeded")
- )
- assert is_retryable_rate_limit_error(
- RuntimeError(
- "Error code: 429 - {'error': {'code': 'ModelAccountTpmRateLimitExceeded', "
- "'message': 'TPM (Tokens Per Minute) limit of the model doubao-seed-2-0-pro "
- "is exceeded. Please try again later Request id: "
- "0217817720969006061aa40146dbf4d117b0497e84060d7ac9102', "
- "'param': '', 'type': 'TooManyRequests'}}, request_id: "
- "202606181641366ORRzhOSo5se81lzpolL"
- )
- )
- assert is_retryable_rate_limit_error(RuntimeError("Error code: 429 - busy"))
- assert not is_retryable_rate_limit_error(RuntimeError("Error code: 401 Unauthorized"))
- assert not is_retryable_rate_limit_error(RuntimeError("trace_id=abc429def unrelated"))
- assert not is_retryable_rate_limit_error(RuntimeError("request_id=abc429def unrelated"))
-
-
-def test_rate_limit_classifier_handles_structured_sdk_errors():
- request = httpx.Request("POST", "https://example.test")
- response = httpx.Response(429, request=request)
- exc = ArkRateLimitError(
- "Error code: 429 - rate limited",
- response=response,
- body={
- "code": "ModelAccountTpmRateLimitExceeded",
- "type": "TooManyRequests",
- "message": "TPM limit exceeded",
- },
- request_id="0217817720969006061aa40146dbf4d117b0497e84060d7ac9102",
- )
-
- assert is_retryable_rate_limit_error(exc)
-
-
def test_completion_usage_object_is_normalized():
from openai.types.completion_usage import CompletionUsage