mirror of
https://github.com/rennf93/roboco.git
synced 2026-08-03 07:23:24 +02:00
feat(kimi): Kimi K3 provider on the official kimi-code CLI (#713)
* feat(kimi): Kimi K3 provider on the official kimi-code CLI (Wave 1) ModelProvider.KIMI routes through KimiCliProvider driving Moonshot's kimi CLI on a Kimi subscription (OAuth device-code, no metered key). One-shot delivery roles only (V1), interactive ban wired in both guard lists. Auth: one shared RW auth mount; containers symlink credentials/ and oauth/ (the CLI's cross-process refresh-lock dir) into a container-local KIMI_CODE_HOME so every container and the host redeem the SAME rotating refresh chain - live-verified that per-copy chains cross-invalidate after the reuse-grace window. No orchestrator refresh daemon; an expires_at preflight exits 78. Config renderer mirrors the login-managed provider/model blocks field-for-field (live-captured; the model value is the CLI-side name, never the raw API id), plus per-role deny rules and the bash-guard as a PreToolUse hook via a wrapper script (an env key on a hooks entry makes the CLI silently drop ALL hooks - live-verified). Usage capture sums wire.jsonl usage.record 4-bucket events; sniff classifies rate-limit/auth from structured error text only, mapped to the shared 75/78 park contract. Image installs the CLI latest-at-build (no version pin, by policy) with the resolved version stamped as provenance, binary split to /usr/local away from mutable state. Migrations 090 (enum) + 091 (provider seed); catalog, pricing, routing mode, and orchestrator park/usage wiring mirror the codex integration. * feat(kimi): surface sweep + fleet-wide pin drop (Wave 2) Compose x3 gain the agent-kimi-image service and the orchestrator's read-write ~/.kimi-code mount + kimi-usage dir; .env.example documents the Kimi block. Panel mirrors ModelProvider.KIMI and adds the kimi routing mode (catalog filter, mode button, mix-picker group, badge) with tests; provider routes gain the kimi remediation entry. CLAUDE.md and docs/map document the runtime. Per the no-pins policy, agent-grok/ gemini/codex Dockerfiles drop their version pins for latest-at-build with resolved-version provenance stamps (grok resolves 0.2.112 vs the old 0.2.56 pin - verified by real builds of all four images). --------- Co-authored-by: Renn F <rennf93@users.noreply.github.com>
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
"""Codex (OPENAI) and Gemini (GEMINI) are V1 delivery-roles-only — neither has
|
||||
an interactive-session driver image (unlike GROK's dedicated
|
||||
GROK_PROMPTER_IMAGE / GROK_SECRETARY_IMAGE). Routing either to the persistent
|
||||
Intake/Secretary agent must refuse loudly instead of silently falling through
|
||||
to the plain Claude SDK-driver image with a mismatched provider env.
|
||||
"""Codex (OPENAI), Gemini (GEMINI), and Kimi (KIMI) are V1 delivery-roles-only
|
||||
— none has an interactive-session driver image (unlike GROK's dedicated
|
||||
GROK_PROMPTER_IMAGE / GROK_SECRETARY_IMAGE). Routing any of them to the
|
||||
persistent Intake/Secretary agent must refuse loudly instead of silently
|
||||
falling through to the plain Claude SDK-driver image with a mismatched
|
||||
provider env.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -67,7 +68,9 @@ class TestRejectInteractiveUnsupportedProvider:
|
||||
un-exempt a chat."""
|
||||
assert set(INTERACTIVE_AGENT_SLUGS) == {INTAKE_AGENT_ID, SECRETARY_AGENT_ID}
|
||||
|
||||
@pytest.mark.parametrize("provider", [ModelProvider.OPENAI, ModelProvider.GEMINI])
|
||||
@pytest.mark.parametrize(
|
||||
"provider", [ModelProvider.OPENAI, ModelProvider.GEMINI, ModelProvider.KIMI]
|
||||
)
|
||||
def test_raises_for_delivery_only_providers(self, provider: ModelProvider) -> None:
|
||||
with pytest.raises(RuntimeError, match="delivery-roles-only"):
|
||||
_reject_interactive_unsupported_provider(INTAKE_AGENT_ID, provider)
|
||||
@@ -93,7 +96,9 @@ class TestRejectInteractiveUnsupportedProvider:
|
||||
|
||||
|
||||
class TestIntakeSpawnRefusesDeliveryOnlyProvider:
|
||||
@pytest.mark.parametrize("provider", [ModelProvider.OPENAI, ModelProvider.GEMINI])
|
||||
@pytest.mark.parametrize(
|
||||
"provider", [ModelProvider.OPENAI, ModelProvider.GEMINI, ModelProvider.KIMI]
|
||||
)
|
||||
@pytest.mark.asyncio
|
||||
async def test_refuses_before_any_container_work(
|
||||
self, monkeypatch: pytest.MonkeyPatch, provider: ModelProvider
|
||||
@@ -149,7 +154,9 @@ class TestIntakeSpawnRefusesDeliveryOnlyProvider:
|
||||
|
||||
|
||||
class TestSecretarySpawnRefusesDeliveryOnlyProvider:
|
||||
@pytest.mark.parametrize("provider", [ModelProvider.OPENAI, ModelProvider.GEMINI])
|
||||
@pytest.mark.parametrize(
|
||||
"provider", [ModelProvider.OPENAI, ModelProvider.GEMINI, ModelProvider.KIMI]
|
||||
)
|
||||
@pytest.mark.asyncio
|
||||
async def test_refuses_before_any_container_work(
|
||||
self, monkeypatch: pytest.MonkeyPatch, provider: ModelProvider
|
||||
|
||||
@@ -0,0 +1,156 @@
|
||||
"""KIMI 429/auth parking: same exit-code convention as codex/grok, scoped to
|
||||
ModelProvider.KIMI so a numeric-code collision with another provider's crash
|
||||
can never mis-park (see ``_KIMI_RATE_LIMIT_EXIT_CODE`` / ``_KIMI_AUTH_EXIT_CODE``
|
||||
in ``roboco.runtime.orchestrator``).
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
import pytest
|
||||
from roboco.models.runtime import AgentInstance
|
||||
from roboco.runtime.orchestrator import (
|
||||
_KIMI_AUTH_EXIT_CODE,
|
||||
_KIMI_RATE_LIMIT_EXIT_CODE,
|
||||
AgentOrchestrator,
|
||||
AgentState,
|
||||
)
|
||||
|
||||
|
||||
def _kimi_instance(provider_type: str = "kimi") -> AgentInstance:
|
||||
cfg = type("C", (), {"provider_type": provider_type, "model": "kimi-code/k3"})()
|
||||
inst = AgentInstance(agent_id="be-dev-1", state=AgentState.ACTIVE, config=cfg)
|
||||
inst.current_task_id = "task-1"
|
||||
inst.container_id = "cid"
|
||||
return inst
|
||||
|
||||
|
||||
class _FakeTracker:
|
||||
def __init__(self) -> None:
|
||||
self.activated_with: dict[str, object] | None = None
|
||||
|
||||
async def activate(
|
||||
self,
|
||||
*,
|
||||
retry_after: float,
|
||||
affected_agents: list[str],
|
||||
kind: str = "rate_limited",
|
||||
) -> None:
|
||||
self.activated_with = {
|
||||
"retry_after": retry_after,
|
||||
"affected_agents": affected_agents,
|
||||
"kind": kind,
|
||||
}
|
||||
|
||||
|
||||
def test_is_kimi_rate_limit_exit() -> None:
|
||||
inst = _kimi_instance()
|
||||
assert AgentOrchestrator._is_kimi_rate_limit_exit(inst, _KIMI_RATE_LIMIT_EXIT_CODE)
|
||||
assert not AgentOrchestrator._is_kimi_rate_limit_exit(inst, 0)
|
||||
assert not AgentOrchestrator._is_kimi_rate_limit_exit(inst, 1)
|
||||
# A codex exit at the SAME numeric code must NOT be classified as kimi.
|
||||
assert not AgentOrchestrator._is_kimi_rate_limit_exit(
|
||||
_kimi_instance(provider_type="openai"), _KIMI_RATE_LIMIT_EXIT_CODE
|
||||
)
|
||||
assert not AgentOrchestrator._is_kimi_rate_limit_exit(
|
||||
_kimi_instance(provider_type="anthropic"), _KIMI_RATE_LIMIT_EXIT_CODE
|
||||
)
|
||||
|
||||
|
||||
def test_is_kimi_auth_exit() -> None:
|
||||
inst = _kimi_instance()
|
||||
assert AgentOrchestrator._is_kimi_auth_exit(inst, _KIMI_AUTH_EXIT_CODE)
|
||||
assert not AgentOrchestrator._is_kimi_auth_exit(inst, 0)
|
||||
assert not AgentOrchestrator._is_kimi_auth_exit(inst, 1)
|
||||
assert not AgentOrchestrator._is_kimi_auth_exit(
|
||||
_kimi_instance(provider_type="openai"), _KIMI_AUTH_EXIT_CODE
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_park_kimi_rate_limited_activates_and_offlines(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
orch = AgentOrchestrator.__new__(AgentOrchestrator)
|
||||
orch._waiting_records = {}
|
||||
orch._rate_limit_ceo_notified = set()
|
||||
inst = _kimi_instance()
|
||||
inst.error_count = 2 # pretend prior crashes — parking must NOT count one
|
||||
tracker = _FakeTracker()
|
||||
monkeypatch.setattr(orch, "_make_tracker", lambda _p: tracker)
|
||||
finalize = AsyncMock()
|
||||
monkeypatch.setattr(orch, "_finalize_spawn_session", finalize)
|
||||
monkeypatch.setattr(orch, "_persist_waiting_record", AsyncMock())
|
||||
|
||||
await orch._park_kimi_rate_limited("be-dev-1", inst)
|
||||
|
||||
finalize.assert_awaited_once()
|
||||
assert inst.state == AgentState.OFFLINE
|
||||
assert inst.container_id is None
|
||||
assert inst.error_count == 0 # a 429 is not a crash
|
||||
assert tracker.activated_with == {
|
||||
"retry_after": pytest.approx(60.0),
|
||||
"affected_agents": ["be-dev-1"],
|
||||
"kind": "rate_limited",
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_park_kimi_auth_unavailable_activates_with_auth_missing_kind(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
orch = AgentOrchestrator.__new__(AgentOrchestrator)
|
||||
orch._waiting_records = {}
|
||||
orch._rate_limit_ceo_notified = set()
|
||||
inst = _kimi_instance()
|
||||
inst.error_count = 2
|
||||
tracker = _FakeTracker()
|
||||
monkeypatch.setattr(orch, "_make_tracker", lambda _p: tracker)
|
||||
monkeypatch.setattr(orch, "_finalize_spawn_session", AsyncMock())
|
||||
monkeypatch.setattr(orch, "_persist_waiting_record", AsyncMock())
|
||||
|
||||
await orch._park_kimi_auth_unavailable("be-dev-1", inst)
|
||||
|
||||
assert inst.state == AgentState.OFFLINE
|
||||
assert inst.container_id is None
|
||||
assert inst.error_count == 0
|
||||
assert tracker.activated_with == {
|
||||
"retry_after": pytest.approx(60.0),
|
||||
"affected_agents": ["be-dev-1"],
|
||||
"kind": "auth_missing",
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_handle_stopped_container_parks_on_kimi_429(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
orch = AgentOrchestrator.__new__(AgentOrchestrator)
|
||||
inst = _kimi_instance()
|
||||
park = AsyncMock()
|
||||
finalize = AsyncMock()
|
||||
monkeypatch.setattr(orch, "_park_kimi_rate_limited", park)
|
||||
monkeypatch.setattr(orch, "_finalize_spawn_session", finalize)
|
||||
|
||||
await orch._handle_stopped_container("be-dev-1", inst, _KIMI_RATE_LIMIT_EXIT_CODE)
|
||||
|
||||
park.assert_awaited_once_with("be-dev-1", inst)
|
||||
finalize.assert_not_awaited()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_handle_stopped_container_parks_on_kimi_auth_exit(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
orch = AgentOrchestrator.__new__(AgentOrchestrator)
|
||||
inst = _kimi_instance()
|
||||
park = AsyncMock()
|
||||
finalize = AsyncMock()
|
||||
monkeypatch.setattr(orch, "_park_kimi_auth_unavailable", park)
|
||||
monkeypatch.setattr(orch, "_finalize_spawn_session", finalize)
|
||||
|
||||
await orch._handle_stopped_container("be-dev-1", inst, _KIMI_AUTH_EXIT_CODE)
|
||||
|
||||
park.assert_awaited_once_with("be-dev-1", inst)
|
||||
finalize.assert_not_awaited()
|
||||
@@ -0,0 +1,144 @@
|
||||
"""KIMI agents capture real input/output/cache-split token usage from their
|
||||
captured ``usage.json`` — Kimi's wire.jsonl carries a genuine, already-disjoint
|
||||
4-bucket split (see ``kimi_cli_usage``), so finalize must return the real
|
||||
4-tuple instead of folding everything into output.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import tempfile
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
from roboco.models.runtime import AgentInstance
|
||||
from roboco.runtime import orchestrator as orch_mod
|
||||
from roboco.runtime.orchestrator import AgentOrchestrator
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def _write_usage(path: Path, **fields: object) -> None:
|
||||
payload = {
|
||||
"model": "kimi-code/k3",
|
||||
"tokens_input": 0,
|
||||
"tokens_output": 0,
|
||||
"tokens_cache_read": 0,
|
||||
"tokens_cache_write": 0,
|
||||
"cost_usd": 0.0,
|
||||
"turns": 1,
|
||||
**fields,
|
||||
}
|
||||
path.write_text(json.dumps(payload), encoding="utf-8")
|
||||
|
||||
|
||||
def test_kimi_usage_returns_real_split(
|
||||
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
||||
) -> None:
|
||||
usage = tmp_path / "usage.json"
|
||||
_write_usage(
|
||||
usage, tokens_input=300, tokens_output=130, tokens_cache_read=30, turns=2
|
||||
)
|
||||
orch = AgentOrchestrator.__new__(AgentOrchestrator)
|
||||
monkeypatch.setattr(
|
||||
orch, "_kimi_usage_json", lambda _aid: json.loads(usage.read_text())
|
||||
)
|
||||
|
||||
expected_turns = 2
|
||||
assert orch._kimi_usage_tokens("be-dev-1") == (300, 130, 30, 0)
|
||||
assert orch._kimi_usage_turns("be-dev-1") == expected_turns
|
||||
|
||||
|
||||
def test_kimi_usage_zero_when_store_missing(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
orch = AgentOrchestrator.__new__(AgentOrchestrator)
|
||||
monkeypatch.setattr(orch, "_kimi_usage_json", lambda _aid: None)
|
||||
assert orch._kimi_usage_tokens("be-dev-1") == (0, 0, 0, 0)
|
||||
assert orch._kimi_usage_turns("be-dev-1") == 0
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_resolve_final_usage_routes_kimi_to_usage_json(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
orch = AgentOrchestrator.__new__(AgentOrchestrator)
|
||||
monkeypatch.setattr(
|
||||
orch,
|
||||
"_kimi_usage_json",
|
||||
lambda _aid: {
|
||||
"tokens_input": 12,
|
||||
"tokens_output": 34,
|
||||
"tokens_cache_read": 5,
|
||||
"tokens_cache_write": 1,
|
||||
},
|
||||
)
|
||||
cfg = type("C", (), {"provider_type": "kimi"})()
|
||||
orch._instances = {"be-dev-1": AgentInstance(agent_id="be-dev-1", config=cfg)}
|
||||
|
||||
assert await orch._resolve_final_token_usage("be-dev-1") == (12, 34, 5, 1)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_resolve_final_turns_tools_routes_kimi_to_usage_json(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
orch = AgentOrchestrator.__new__(AgentOrchestrator)
|
||||
monkeypatch.setattr(orch, "_kimi_usage_turns", lambda _aid: 3)
|
||||
cfg = type("C", (), {"provider_type": "kimi"})()
|
||||
orch._instances = {"be-dev-1": AgentInstance(agent_id="be-dev-1", config=cfg)}
|
||||
|
||||
# Kimi has no tool-call signal — tool_calls stays 0.
|
||||
assert await orch._resolve_final_turns_tools("be-dev-1") == (3, 0)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_resolve_active_tokens_routes_kimi_to_usage_json(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
orch = AgentOrchestrator.__new__(AgentOrchestrator)
|
||||
monkeypatch.setattr(
|
||||
orch,
|
||||
"_kimi_usage_json",
|
||||
lambda _aid: {"tokens_input": 12, "tokens_output": 34},
|
||||
)
|
||||
cfg = type("C", (), {"provider_type": "kimi"})()
|
||||
orch._instances = {"be-dev-1": AgentInstance(agent_id="be-dev-1", config=cfg)}
|
||||
async with httpx.AsyncClient() as client:
|
||||
assert await orch._resolve_active_tokens(client, "be-dev-1") == (12, 34, 0, 0)
|
||||
|
||||
|
||||
def test_kimi_usage_dir_branches_compose_vs_local(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
monkeypatch.setattr(orch_mod, "PROJECT_HOST_PATH", "")
|
||||
local = AgentOrchestrator._kimi_usage_dir("be-dev-1")
|
||||
assert "roboco-kimi-usage" in str(local)
|
||||
assert local.name == "be-dev-1"
|
||||
|
||||
monkeypatch.setattr(orch_mod, "PROJECT_HOST_PATH", "/volume1/roboco")
|
||||
monkeypatch.setattr(orch_mod, "KIMI_USAGE_DATA_DIR", "/data/kimi-usage")
|
||||
assert str(AgentOrchestrator._kimi_usage_dir("be-dev-1")) == (
|
||||
"/data/kimi-usage/be-dev-1"
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"bad",
|
||||
["..", ".", "../etc", "a/b", "a\\b", "", "be-dev-1/../x", "x\x00y"],
|
||||
)
|
||||
def test_kimi_usage_dir_rejects_path_traversal(bad: str) -> None:
|
||||
with pytest.raises(ValueError, match="unsafe agent id"):
|
||||
AgentOrchestrator._kimi_usage_dir(bad)
|
||||
|
||||
|
||||
def test_kimi_usage_json_reads_the_real_local_dir(
|
||||
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
||||
) -> None:
|
||||
monkeypatch.setattr(orch_mod, "PROJECT_HOST_PATH", "")
|
||||
monkeypatch.setattr(tempfile, "gettempdir", lambda: str(tmp_path))
|
||||
udir = tmp_path / "roboco-kimi-usage" / "be-dev-1"
|
||||
udir.mkdir(parents=True)
|
||||
_write_usage(udir / "usage.json", tokens_input=55, tokens_output=10)
|
||||
orch = AgentOrchestrator.__new__(AgentOrchestrator)
|
||||
assert orch._kimi_usage_tokens("be-dev-1") == (55, 10, 0, 0)
|
||||
Reference in New Issue
Block a user