From 7d5d5e0f48885dba34d8ce3c3f7bbac85761b7f8 Mon Sep 17 00:00:00 2001 From: logohere <69400476+logohere@users.noreply.github.com> Date: Wed, 11 Nov 2026 18:15:08 -0500 Subject: [PATCH 1/2] fix: preserve provider registration and stop-sequence handling --- agent/chat_completion_helpers.py | 4 ++-- agent/gemini_cloudcode_adapter.py | 36 +++++++++++++++++++++++++++---- agent/gemini_native_adapter.py | 34 +++++++++++++++++++++++++++-- agent/logit_bias.py | 13 +++++++++-- cli.py | 4 ++-- dag_cli/auth.py | 12 +++++++++++ dag_cli/config.py | 2 ++ dag_cli/model_normalize.py | 2 +- dag_cli/model_switch.py | 8 ++++++- dag_cli/models.py | 7 ++++++ dag_cli/providers.py | 6 ++++++ 11 files changed, 114 insertions(+), 14 deletions(-) diff --git a/agent/chat_completion_helpers.py b/agent/chat_completion_helpers.py index b6c70cd..482e708 100644 --- a/agent/chat_completion_helpers.py +++ b/agent/chat_completion_helpers.py @@ -765,7 +765,7 @@ def build_api_kwargs(agent, api_messages: list) -> dict: supports_reasoning=agent._supports_reasoning_extra_body(), qwen_session_metadata=_qwen_meta, logit_bias=_build_logit_bias(), - stop=_build_stop_sequences(), + stop=_build_stop_sequences(agent.provider), ) # ── Legacy flag path ──────────────────────────────────────────── @@ -814,7 +814,7 @@ def build_api_kwargs(agent, api_messages: list) -> dict: anthropic_max_output=_ant_max, provider_name=agent.provider, logit_bias=_build_logit_bias(), - stop=_build_stop_sequences(), + stop=_build_stop_sequences(agent.provider), ) diff --git a/agent/gemini_cloudcode_adapter.py b/agent/gemini_cloudcode_adapter.py index ecdb6e7..67e245e 100644 --- a/agent/gemini_cloudcode_adapter.py +++ b/agent/gemini_cloudcode_adapter.py @@ -47,6 +47,7 @@ ) logger = logging.getLogger(__name__) +_GEMINI_MAX_STOP_SEQUENCES = 5 # ============================================================================= @@ -130,6 +131,34 @@ def _translate_tool_result_to_gemini(message: Dict[str, Any]) -> Dict[str, Any]: } +def _normalize_gemini_stop_sequences(stop: Any) -> Optional[List[str]]: + """Return a Gemini-safe stop list capped at the provider limit.""" + if not stop: + return None + if isinstance(stop, str): + values = [stop] + elif isinstance(stop, (list, tuple)): + values = [str(s) for s in stop if s] + else: + return None + + deduped: List[str] = [] + seen: set[str] = set() + for value in values: + if value in seen: + continue + seen.add(value) + deduped.append(value) + + if len(deduped) > _GEMINI_MAX_STOP_SEQUENCES: + logger.warning( + "Gemini supports at most %d stop sequences; truncating %d extra entries", + _GEMINI_MAX_STOP_SEQUENCES, + len(deduped) - _GEMINI_MAX_STOP_SEQUENCES, + ) + return deduped[:_GEMINI_MAX_STOP_SEQUENCES] or None + + def _build_gemini_contents( messages: List[Dict[str, Any]], ) -> tuple[List[Dict[str, Any]], Optional[Dict[str, Any]]]: @@ -284,10 +313,9 @@ def build_gemini_request( generation_config["maxOutputTokens"] = max_tokens if isinstance(top_p, (int, float)): generation_config["topP"] = float(top_p) - if isinstance(stop, str) and stop: - generation_config["stopSequences"] = [stop] - elif isinstance(stop, list) and stop: - generation_config["stopSequences"] = [str(s) for s in stop if s] + stop_sequences = _normalize_gemini_stop_sequences(stop) + if stop_sequences: + generation_config["stopSequences"] = stop_sequences normalized_thinking = _normalize_thinking_config(thinking_config) if normalized_thinking: generation_config["thinkingConfig"] = normalized_thinking diff --git a/agent/gemini_native_adapter.py b/agent/gemini_native_adapter.py index 32ebbdc..cc1bf13 100644 --- a/agent/gemini_native_adapter.py +++ b/agent/gemini_native_adapter.py @@ -32,6 +32,7 @@ logger = logging.getLogger(__name__) DEFAULT_GEMINI_BASE_URL = "https://generativelanguage.googleapis.com/v1beta" +_GEMINI_MAX_STOP_SEQUENCES = 5 # Published max output-token ceiling shared by every current Gemini text model # (2.5 + 3.x: flash, flash-lite, pro). Used as the default when the caller @@ -61,6 +62,34 @@ def is_native_gemini_base_url(base_url: str) -> bool: return not normalized.endswith("/openai") +def _normalize_gemini_stop_sequences(stop: Any) -> Optional[List[str]]: + """Return a Gemini-safe stop list capped at the provider limit.""" + if not stop: + return None + if isinstance(stop, str): + values = [stop] + elif isinstance(stop, (list, tuple)): + values = [str(s) for s in stop if s] + else: + return None + + deduped: List[str] = [] + seen: set[str] = set() + for value in values: + if value in seen: + continue + seen.add(value) + deduped.append(value) + + if len(deduped) > _GEMINI_MAX_STOP_SEQUENCES: + logger.warning( + "Gemini supports at most %d stop sequences; truncating %d extra entries", + _GEMINI_MAX_STOP_SEQUENCES, + len(deduped) - _GEMINI_MAX_STOP_SEQUENCES, + ) + return deduped[:_GEMINI_MAX_STOP_SEQUENCES] or None + + def probe_gemini_tier( api_key: str, base_url: str = DEFAULT_GEMINI_BASE_URL, @@ -445,8 +474,9 @@ def build_gemini_request( generation_config["maxOutputTokens"] = GEMINI_DEFAULT_MAX_OUTPUT_TOKENS if top_p is not None: generation_config["topP"] = top_p - if stop: - generation_config["stopSequences"] = stop if isinstance(stop, list) else [str(stop)] + stop_sequences = _normalize_gemini_stop_sequences(stop) + if stop_sequences: + generation_config["stopSequences"] = stop_sequences normalized_thinking = _normalize_thinking_config(thinking_config) if normalized_thinking: generation_config["thinkingConfig"] = normalized_thinking diff --git a/agent/logit_bias.py b/agent/logit_bias.py index aaed502..21635ad 100644 --- a/agent/logit_bias.py +++ b/agent/logit_bias.py @@ -13,6 +13,10 @@ log = logging.getLogger(__name__) +_STOP_SEQUENCE_LIMITS: dict[str, int] = { + "cerebras": 4, +} + def _strings() -> List[str]: """Forbidden strings from the unified config parser.""" @@ -40,7 +44,7 @@ def build_logit_bias() -> Optional[Dict[int, int]]: return banned or None -def build_stop_sequences() -> Optional[List[str]]: +def build_stop_sequences(provider_name: str | None = None) -> Optional[List[str]]: """Return forbidden strings as stop sequences. Works on ALL providers. Zero dependencies. @@ -48,7 +52,12 @@ def build_stop_sequences() -> Optional[List[str]]: the API cuts generation at that token. """ strings = _strings() - return strings if strings else None + if not strings: + return None + limit = _STOP_SEQUENCE_LIMITS.get((provider_name or "").strip().lower()) + if limit is not None: + return strings[:limit] + return strings __all__ = ["build_logit_bias", "build_stop_sequences"] diff --git a/cli.py b/cli.py index 74402fb..7d3e2b5 100644 --- a/cli.py +++ b/cli.py @@ -387,9 +387,9 @@ def load_cli_config() -> Dict[str, Any]: # Default configuration defaults = { "model": { - "default": "", + "default": "gemma-4-31b", "base_url": "", - "provider": "auto", + "provider": "cerebras", }, "terminal": { "env_type": "local", diff --git a/dag_cli/auth.py b/dag_cli/auth.py index a4e40ca..bb3dc82 100644 --- a/dag_cli/auth.py +++ b/dag_cli/auth.py @@ -70,6 +70,7 @@ DEFAULT_NOUS_PORTAL_URL = "https://portal.nousresearch.com" DEFAULT_NOUS_INFERENCE_URL = "https://inference-api.nousresearch.com/v1" DEFAULT_NOUS_CLIENT_ID = "dag-cli" +DEFAULT_CEREBRAS_BASE_URL = "https://api.cerebras.ai/v1" NOUS_INFERENCE_INVOKE_SCOPE = "inference:invoke" DEFAULT_NOUS_SCOPE = NOUS_INFERENCE_INVOKE_SCOPE NOUS_DEVICE_CODE_SOURCE = "device_code" @@ -170,6 +171,14 @@ class ProviderConfig: PROVIDER_REGISTRY: Dict[str, ProviderConfig] = { + "cerebras": ProviderConfig( + id="cerebras", + name="Cerebras", + auth_type="api_key", + inference_base_url=DEFAULT_CEREBRAS_BASE_URL, + api_key_env_vars=("CEREBRAS_API_KEY",), + base_url_env_var="CEREBRAS_BASE_URL", + ), "nous": ProviderConfig( id="nous", name="Nous Portal", @@ -1586,6 +1595,9 @@ def resolve_provider( except Exception as e: logger.debug("Could not detect active auth provider: %s", e) + if has_usable_secret(os.getenv("CEREBRAS_API_KEY")): + return "cerebras" + if has_usable_secret(os.getenv("OPENAI_API_KEY")) or has_usable_secret(os.getenv("OPENROUTER_API_KEY")): return "openrouter" diff --git a/dag_cli/config.py b/dag_cli/config.py index 2b9428d..cec2a13 100644 --- a/dag_cli/config.py +++ b/dag_cli/config.py @@ -5482,6 +5482,7 @@ def _load_config_impl(*, want_deepcopy: bool) -> Dict[str, Any]: # overload (529), service errors (503), or connection failures. # # Supported providers: +# cerebras (CEREBRAS_API_KEY) — Cerebras Inference API # openrouter (OPENROUTER_API_KEY) — routes to any model # openai-codex (OAuth — dag auth) — OpenAI Codex # nous (OAuth — dag auth) — Nous Portal @@ -5514,6 +5515,7 @@ def _load_config_impl(*, want_deepcopy: bool) -> Dict[str, Any]: # overload (529), service errors (503), or connection failures. # # Supported providers: +# cerebras (CEREBRAS_API_KEY) — Cerebras Inference API # openrouter (OPENROUTER_API_KEY) — routes to any model # openai-codex (OAuth — dag auth) — OpenAI Codex # nous (OAuth — dag auth) — Nous Portal diff --git a/dag_cli/model_normalize.py b/dag_cli/model_normalize.py index 616505d..0321f85 100644 --- a/dag_cli/model_normalize.py +++ b/dag_cli/model_normalize.py @@ -98,6 +98,7 @@ "minimax-cn", "alibaba", "qwen-oauth", + "cerebras", "xiaomi", "arcee", "ollama-cloud", @@ -470,4 +471,3 @@ def normalize_model_for_provider(model_input: str, target_provider: str) -> str: # --------------------------------------------------------------------------- # Batch / convenience helpers # --------------------------------------------------------------------------- - diff --git a/dag_cli/model_switch.py b/dag_cli/model_switch.py index 5059a0f..f187ad0 100644 --- a/dag_cli/model_switch.py +++ b/dag_cli/model_switch.py @@ -188,7 +188,13 @@ class DirectAlias(NamedTuple): # Built-in direct aliases (can be extended via config.yaml model_aliases:) -_BUILTIN_DIRECT_ALIASES: dict[str, DirectAlias] = {} +_BUILTIN_DIRECT_ALIASES: dict[str, DirectAlias] = { + "gemma-4-31b-it": DirectAlias( + model="gemma-4-31b", + provider="cerebras", + base_url="", + ), +} # Merged dict (builtins + user config); populated by _load_direct_aliases() DIRECT_ALIASES: dict[str, DirectAlias] = {} diff --git a/dag_cli/models.py b/dag_cli/models.py index b28a1cc..73d028b 100644 --- a/dag_cli/models.py +++ b/dag_cli/models.py @@ -216,6 +216,12 @@ def _xai_curated_models() -> list[str]: "gpt-4o", "gpt-4o-mini", ], + "cerebras": [ + "gemma-4-31b", + "gpt-oss-120b", + "glm-4.7", + "qwen3-32b", + ], "openai-codex": _codex_curated_models(), "xai-oauth": _xai_curated_models(), "copilot-acp": [ @@ -994,6 +1000,7 @@ class ProviderEntry(NamedTuple): CANONICAL_PROVIDERS: list[ProviderEntry] = [ ProviderEntry("nous", "Nous Portal", "Nous Portal (Everything your agent needs, 300+ models with bundled tool use)"), ProviderEntry("openrouter", "OpenRouter", "OpenRouter (Pay-per-use API aggregator)"), + ProviderEntry("cerebras", "Cerebras", "Cerebras Inference API (Gemma 4 31B, GPT OSS 120B, GLM 4.7, Qwen3 32B)"), ProviderEntry("novita", "NovitaAI", "NovitaAI (Cloud: Model API, Agent Sandbox, GPU Cloud)"), ProviderEntry("lmstudio", "LM Studio", "LM Studio (Local desktop app with built-in model server)"), ProviderEntry("anthropic", "Anthropic", "Anthropic (Claude models via API key or Claude Code)"), diff --git a/dag_cli/providers.py b/dag_cli/providers.py index 862795f..8c769ff 100644 --- a/dag_cli/providers.py +++ b/dag_cli/providers.py @@ -44,6 +44,11 @@ class DagOverlay: DAG_OVERLAYS: Dict[str, DagOverlay] = { + "cerebras": DagOverlay( + transport="openai_chat", + base_url_override="https://api.cerebras.ai/v1", + base_url_env_var="CEREBRAS_BASE_URL", + ), "openrouter": DagOverlay( transport="openai_chat", is_aggregator=True, @@ -366,6 +371,7 @@ class ProviderDef: _LABEL_OVERRIDES: Dict[str, str] = { "nous": "Nous Portal", + "cerebras": "Cerebras", "openai-codex": "OpenAI Codex", "copilot-acp": "GitHub Copilot ACP", "stepfun": "StepFun Step Plan", From 233167cbafef567dbbba53f2f13e195df8e18330 Mon Sep 17 00:00:00 2001 From: logohere <69400476+logohere@users.noreply.github.com> Date: Wed, 11 Nov 2026 18:15:36 -0500 Subject: [PATCH 2/2] test: cover recovered provider and stop-sequence changes --- tests/test_cerebras_default.py | 52 +++++++++++++++++++++++++++++ tests/test_gemini_stop_sequences.py | 22 ++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 tests/test_cerebras_default.py create mode 100644 tests/test_gemini_stop_sequences.py diff --git a/tests/test_cerebras_default.py b/tests/test_cerebras_default.py new file mode 100644 index 0000000..260e436 --- /dev/null +++ b/tests/test_cerebras_default.py @@ -0,0 +1,52 @@ +from __future__ import annotations + +from pathlib import Path + +from agent.logit_bias import build_stop_sequences +from dag_cli.auth import PROVIDER_REGISTRY +from dag_cli.models import CANONICAL_PROVIDERS +from dag_cli.model_normalize import normalize_model_for_provider +from dag_cli.models import get_default_model_for_provider, provider_model_ids +from dag_cli.model_switch import resolve_alias + + +def test_cerebras_is_registered() -> None: + assert "cerebras" in PROVIDER_REGISTRY + + +def test_cerebras_is_canonical_provider() -> None: + assert any(p.slug == "cerebras" for p in CANONICAL_PROVIDERS) + + +def test_cerebras_default_model() -> None: + assert get_default_model_for_provider("cerebras") == "gemma-4-31b" + + +def test_cerebras_prefix_is_stripped() -> None: + assert normalize_model_for_provider("cerebras/gemma-4-31b", "cerebras") == "gemma-4-31b" + + +def test_cerebras_model_catalog_contains_public_models() -> None: + models = provider_model_ids("cerebras") + assert models[:4] == [ + "gemma-4-31b", + "gpt-oss-120b", + "glm-4.7", + "qwen3-32b", + ] + + +def test_cli_default_points_to_cerebras() -> None: + cli_text = Path(__file__).resolve().parents[1] / "cli.py" + content = cli_text.read_text(encoding="utf-8") + assert '"provider": "cerebras"' in content + assert '"default": "gemma-4-31b"' in content + + +def test_cerebras_stop_sequences_are_capped(monkeypatch) -> None: + monkeypatch.setattr("agent.logit_bias.get_forbidden_strings", lambda: ["a", "b", "c", "d", "e"]) + assert build_stop_sequences("cerebras") == ["a", "b", "c", "d"] + + +def test_old_cerebras_slug_resolves_to_new_slug() -> None: + assert resolve_alias("gemma-4-31b-it", "cerebras") == ("cerebras", "gemma-4-31b", "gemma-4-31b-it") diff --git a/tests/test_gemini_stop_sequences.py b/tests/test_gemini_stop_sequences.py new file mode 100644 index 0000000..05fd317 --- /dev/null +++ b/tests/test_gemini_stop_sequences.py @@ -0,0 +1,22 @@ +from __future__ import annotations + +from agent.gemini_cloudcode_adapter import build_gemini_request as build_cloudcode_request +from agent.gemini_native_adapter import build_gemini_request as build_native_request + + +def test_native_gemini_stop_sequences_are_capped_and_deduped() -> None: + request = build_native_request( + messages=[{"role": "user", "content": "hello"}], + stop=["a", "b", "a", "c", "d", "e", "f"], + ) + + assert request["generationConfig"]["stopSequences"] == ["a", "b", "c", "d", "e"] + + +def test_cloudcode_gemini_stop_sequences_are_capped_and_deduped() -> None: + request = build_cloudcode_request( + messages=[{"role": "user", "content": "hello"}], + stop=["a", "b", "a", "c", "d", "e", "f"], + ) + + assert request["generationConfig"]["stopSequences"] == ["a", "b", "c", "d", "e"]