Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion server/cli/code_maintenance.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,8 +445,15 @@ async def _describe(args: argparse.Namespace) -> int:
from server.code.store.index import CodeRepo
from server.core.ch import get_client
from server.embedding.embedder import get_embedder
from server.llm.client import get_llm_client
from server.llm.client import get_llm_client, llm_is_fake

if llm_is_fake():
Comment thread
zlareb1 marked this conversation as resolved.
Resolved
Comment thread
zlareb1 marked this conversation as resolved.
Resolved
print(
"refusing to describe with the fake llm (LOOM_LLM_PROVIDER=fake, or its "
"API key is not set): its echo would be stored as chunk descriptions",
file=sys.stderr,
)
return 2
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
await connect_code_plane(get_client(), args)
org, ns = _identity(args)
repo = CodeRepo()
Expand Down
27 changes: 26 additions & 1 deletion server/code/app/index/pass_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,28 @@ async def _slice_done(repo: Any, org: str, ns: str, slice_key: str) -> int:
return int(n) if key == slice_key and n.isdigit() else 0


_fake_llm_warned = False


def _llm_is_fake() -> bool:
"""True when the description writer would run on the fake client, whose echo
must never be stored (and embedded) as a chunk's `llm` description. Logs once
per process."""
from server.llm.client import llm_is_fake

global _fake_llm_warned
if not llm_is_fake():
return False
if not _fake_llm_warned:
_fake_llm_warned = True
logger.warning(
"code_ingest: LOOM_CODE_CONTEXT_DESCRIPTION_ENABLED is on but the llm "
"provider is fake (configured so, or its API key is not set); chunks "
"get structural context instead of llm descriptions"
)
return True


class CommitStream:
"""One entry's pending commits, ingested one `step` at a time: the
per-entry setup — the embedder, the describe gate, the
Expand Down Expand Up @@ -99,7 +121,10 @@ async def _prepare(self) -> dict[str, Any]:
describe = settings.code_context_description_enabled
llm = None
if describe:
if await namespace_over_cap(cfg.org, cfg.namespace):
if _llm_is_fake():
describe = False
self.result.note = "describe skipped: the llm provider is fake"
elif await namespace_over_cap(cfg.org, cfg.namespace):
describe = False
self.result.note = "describe skipped: namespace over budget cap"
else:
Expand Down
93 changes: 93 additions & 0 deletions tests/unit/test_code_describe_fake_llm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
"""The description writer never runs on the fake LLM client.

With `llm.provider = fake` (or a keyed provider whose key is absent, which falls
back to the fake client) the fake's echo would be stored as the chunk's `llm`
description and embedded. The ingest pass gives those chunks structural context
instead, and `loom code describe` refuses to start."""

from __future__ import annotations

import argparse
from types import SimpleNamespace
from typing import Any

import pytest

from server.code.app.index import pass_stream
from server.code.app.index.pass_budget import CodeIngestResult
from server.llm import client as llm_client
from server.settings import settings


def _stream() -> pass_stream.CommitStream:
cfg = SimpleNamespace(
org="o", namespace="ns", path_prefixes=(), embed_reuse_from=None, git_dir="/nowhere"
)
return pass_stream.CommitStream(cfg, None, "head", "base", ["sha"], CodeIngestResult())


@pytest.fixture
def prepare_env(monkeypatch: pytest.MonkeyPatch) -> None:
async def under_cap(*_a: Any) -> bool:
return False

async def no_denylist(*_a: Any) -> None:
return None

monkeypatch.setattr(settings, "code_context_description_enabled", True)
monkeypatch.setattr("server.observability.budget.namespace_over_cap", under_cap)
monkeypatch.setattr("server.code.app.index.denylist.load_denylist", no_denylist)
monkeypatch.setattr("server.embedding.embedder.get_embedder", lambda: object())


def _no_client() -> Any:
raise AssertionError("the description writer must not build a client")


async def test_a_fake_provider_describes_nothing(
monkeypatch: pytest.MonkeyPatch, prepare_env: None
) -> None:
monkeypatch.setattr(settings, "llm_provider", "fake")
monkeypatch.setattr(llm_client, "get_llm_client", _no_client)
stream = _stream()
ctx = await stream._prepare()
assert ctx["describe"] is False
assert ctx["llm"] is None
assert stream.result.note == "describe skipped: the llm provider is fake"


async def test_a_keyed_provider_without_its_key_describes_nothing(
monkeypatch: pytest.MonkeyPatch, prepare_env: None
) -> None:
monkeypatch.setattr(settings, "llm_provider", "anthropic")
monkeypatch.setattr(llm_client, "provider_key_missing", lambda _p: True)
monkeypatch.setattr(llm_client, "get_llm_client", _no_client)
ctx = await _stream()._prepare()
assert ctx["describe"] is False
assert ctx["llm"] is None


async def test_a_real_provider_still_describes(
monkeypatch: pytest.MonkeyPatch, prepare_env: None
) -> None:
real = object()
monkeypatch.setattr(llm_client, "llm_is_fake", lambda: False)
monkeypatch.setattr(llm_client, "get_llm_client", lambda: real)
ctx = await _stream()._prepare()
assert ctx["describe"] is True
assert ctx["llm"] is real


async def test_the_describe_cli_refuses_a_fake_provider(
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
) -> None:
from server.cli import code_maintenance

async def no_connect(*_a: Any) -> None:
raise AssertionError("refused before connecting")

monkeypatch.setattr(settings, "llm_provider", "fake")
monkeypatch.setattr(llm_client, "get_llm_client", _no_client)
monkeypatch.setattr(code_maintenance, "connect_code_plane", no_connect)
assert await code_maintenance._describe(argparse.Namespace()) == 2
assert "fake llm" in capsys.readouterr().err
Loading