Files
roboco/tests/integration/test_migration_091_seed_kimi_provider.py
6374bbbed0 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>
2026-07-29 01:48:55 +02:00

163 lines
5.1 KiB
Python

"""Migration 091 tests — seed_kimi_provider.
Verifies the post-upgrade state and exercises the downgrade SQL ordering,
mirroring ``test_migration_083_seed_openai_provider.py``'s own shape (Kimi's
row is seeded ``enabled=true`` directly, the same posture as Codex's — there
is no ``apply_mode="kimi"``-only gate it needs to wait behind, unlike GROK's
disabled-until-key-set seed).
NOT a real alembic round-trip — the suite builds the test DB via
Base.metadata.create_all (see conftest). Migration 091's upgrade()/downgrade()
bodies are reviewed here; the tests guard the resulting DB-level contract —
in particular ``enabled=True`` at seed time and NULL base_url/auth_token
(subscription auth, no stored secret).
"""
from __future__ import annotations
from typing import TYPE_CHECKING
from uuid import uuid4
import pytest
from roboco.db.tables import ModelAssignmentTable, ProviderConfigTable
from roboco.models.base import AssignmentScope, ModelProvider
from sqlalchemy import text
if TYPE_CHECKING:
from sqlalchemy.ext.asyncio import AsyncSession
_INSERT_SQL = text(
"""
INSERT INTO provider_configs
(id, name, type, base_url, auth_token_encrypted, enabled, created_at)
VALUES
(
gen_random_uuid(),
'Kimi (Moonshot)',
'kimi',
NULL,
NULL,
true,
now()
)
ON CONFLICT (name) DO NOTHING
"""
)
@pytest.mark.asyncio
async def test_migration_091_upgrade_insert_contract(
db_session: AsyncSession,
) -> None:
"""The upgrade INSERT SQL seeds the Kimi row ENABLED with no stored
secret (subscription auth), and is idempotent."""
# --- First run: the row should be inserted.
await db_session.execute(_INSERT_SQL)
await db_session.flush()
result = await db_session.execute(
text(
"SELECT name, type, enabled, base_url, auth_token_encrypted "
"FROM provider_configs "
"WHERE name = 'Kimi (Moonshot)'"
)
)
rows = list(result)
assert len(rows) == 1
name, ptype, enabled, base_url, auth_token = rows[0]
assert name == "Kimi (Moonshot)"
assert ptype == "kimi"
# The load-bearing assertion: enabled=True at seed time (parity with
# Codex — there's no key-collection step gating this row).
assert enabled is True
assert base_url is None
assert auth_token is None
# --- Second run: ON CONFLICT DO NOTHING must not create a duplicate.
await db_session.execute(_INSERT_SQL)
await db_session.flush()
result = await db_session.execute(
text("SELECT id FROM provider_configs WHERE name = 'Kimi (Moonshot)'")
)
assert len(list(result)) == 1, (
"Expected exactly one 'Kimi (Moonshot)' row after two INSERT "
"executions; ON CONFLICT DO NOTHING must prevent duplicates."
)
@pytest.mark.asyncio
async def test_migration_091_downgrade_deletes_assignments_before_config(
db_session: AsyncSession,
) -> None:
"""Downgrade SQL deletes model_assignments before provider_configs.
A FK RESTRICT constraint on model_assignments.provider_config_id means
deleting provider_configs first would raise an IntegrityError.
"""
suffix = uuid4().hex[:8]
kimi = ProviderConfigTable(
name=f"Kimi (Moonshot)-test-{suffix}",
type=ModelProvider.KIMI,
enabled=True,
)
db_session.add(kimi)
await db_session.flush()
assignment = ModelAssignmentTable(
scope=AssignmentScope.AGENT_SLUG,
scope_value=f"test-agent-{suffix}",
provider_config_id=kimi.id,
model_name="kimi-code/k3",
)
db_session.add(assignment)
await db_session.flush()
result = await db_session.execute(
text("SELECT id FROM provider_configs WHERE name = :name").bindparams(
name=kimi.name
)
)
assert result.scalar_one_or_none() is not None
result = await db_session.execute(
text("SELECT id FROM model_assignments WHERE scope_value = :sv").bindparams(
sv=assignment.scope_value
)
)
assert result.scalar_one_or_none() is not None
# Step 1: delete referencing model_assignments first.
await db_session.execute(
text(
"DELETE FROM model_assignments "
"WHERE provider_config_id IN ("
" SELECT id FROM provider_configs WHERE name = :name"
")"
).bindparams(name=kimi.name)
)
# Step 2: now safe to delete the provider row.
await db_session.execute(
text("DELETE FROM provider_configs WHERE name = :name").bindparams(
name=kimi.name
)
)
result = await db_session.execute(
text("SELECT id FROM provider_configs WHERE name = :name").bindparams(
name=kimi.name
)
)
assert result.scalar_one_or_none() is None, (
"provider_configs row should be deleted by downgrade"
)
result = await db_session.execute(
text("SELECT id FROM model_assignments WHERE scope_value = :sv").bindparams(
sv=assignment.scope_value
)
)
assert result.scalar_one_or_none() is None, (
"model_assignments row should be deleted before provider_configs"
)