Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
schema: spec-driven
created: 2026-11-13
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Design

## Decisions
- **One resolver.** The plan builders call `resolve_ai_endpoint`, the resolver that the review run calls in `_make_review_clients`. The model request uses `task="analysis"`, so a task override of the provider or the endpoint (`ai.tasks.analysis`) shows in the plan, as it does in the run. The embedding request of `review corpus generate` uses the global AI settings, as before.
- **A missing setting is a placeholder in the plan.** When the resolver raises `ConfigurationError`, the plan target starts with `<{key}>`, the setting that the error names, such as `<ai.gateway_url>`. A dry-run plan shows a value that it does not have as a named placeholder (`src/devops_cli/dry_run/requests.py`), and the Argo CD plans do the same for an unset `argocd.url` (`https://<argocd-url>`). The error is not hidden: the plan names its setting, and the real run stops with the same error.
- **The path follows the provider.** Ollama gets `/api/chat` and `/api/embeddings`. Claude gets `/v1/messages`, because the resolver gives the API root `https://api.anthropic.com`. Every other provider gets `/chat/completions` and `/embeddings` after the resolved base, which already holds `/v1` where the provider needs it.

## Rejected alternatives
- **Stop the dry run with the error of the resolver.** The real run of `review corpus generate` makes no model request and needs no AI setting, but its plan lists an embedding request. An error in the plan builder would make that dry run fail where the real run passes. `devops ai benchmark` also does not resolve the endpoint under `--dry-run` (`src/devops_cli/commands/benchmark.py`).
- **Keep a default host for the gateway.** The rule of #1165 refuses a service URL that no setting gives, and `tests/config/test_service_url_defaults.py` holds `ai.gateway_url` to no code default.
- **Fix `src/devops_cli/ai/ai_plans.py` and extend the ast-grep rule in this change.** The issue names one module. The other module has nine sites and its own plans, and a rule change that matches a fallback in a list fails the scan until all nine are gone. The follow-up issue holds both.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Proposal: #5345

## Why
The dry-run plan of `devops review path`, `branch`, `pr`, `benchmark`, `corpus score` and `samples validate --review` gets the target of its model request from `_get_review_llm_target` in `src/devops_cli/review/review_plans.py`. When `ai.provider` is `gateway` and `ai.gateway_url` is not set, that function gives a fixed HTTPS host from the source. When `ai.provider` is `ollama` and `ai.ollama_urls` is empty, it gives a localhost address, and `_get_review_embedding_target` does the same for `review corpus generate`. The rule of #1165 allows no service URL that the configuration does not give. Closes #5345.

## What Changes
- The review plan builders get the model endpoint from `resolve_ai_endpoint` (`src/devops_cli/ai/endpoints.py`), the resolver that the review run uses before it makes its model client. The model request uses the `analysis` task, as the review run does.
- When the resolver refuses because a setting is not set, the plan target names that setting as a placeholder, for example `<ai.gateway_url>/chat/completions`. The dry run still makes zero requests and exits 0. The real run stops with the error of the resolver, as before.
- The fixed gateway host and the two localhost fallbacks go out of the module.
- The gateway target is `<ai.gateway_url>/chat/completions`. The old target added `/v1` to the configured URL, so a configured `https://gateway.example.com/v1` gave `/v1/v1/chat/completions`. The model client uses `ai.gateway_url` as its base URL with no more `/v1`.
- The Anthropic target is `<base>/v1/messages`, the path of the Messages API, in place of an OpenAI path.

The dry-run output is behavior that a person observes, so the change has a spec delta. The change fits one pull request.

## Capabilities
- `configured-service-urls` (modified): ADDED requirement "A review dry run takes its model endpoint from the configuration".

## Impact
- The dry-run plan of the review commands names the configured endpoint, or the setting that is not set. No setting, no dependency and no real run changes.
- `src/devops_cli/ai/ai_plans.py` keeps nine localhost fallbacks of the same kind, and the guard of #1165 does not match a fallback in a list. #5395 holds that work.
- Rollback: `git revert`. No downtime.

## Feasibility
- `git show origin/main:src/devops_cli/review/review_plans.py` at release 0.127.0: line 24 is `gw = (settings.ai.gateway_url or "https://<fixed-host>").rstrip("/")`, and lines 21 and 35 fall back to `["http://localhost:11434"]`.
- With a config file that holds only `ai.provider: gateway`, `DEVOPS_CLI_CONFIG=<file> uv run devops review path src --dry-run` on the worktree at `origin/main` printed `POST https://<fixed-host>/v1/chat/completions`. With `ai.gateway_url: https://gateway.example.com/v1`, it printed `POST https://gateway.example.com/v1/v1/chat/completions`. With only `ai.provider: ollama`, it printed `POST http://localhost:11434/api/chat`.
- The guard of #1165 is the ast-grep rule `net-localhost-fallback` (`.ast-grep/rules/net-localhost-fallback.yml`). It reads every file under `src/`, this module too, so no file list needs a change. `uv run ast-grep scan --rule .ast-grep/rules/net-localhost-fallback.yml src/devops_cli/review/review_plans.py` found nothing on `origin/main`: the rule matches `or "http://localhost..."` and not `or ["http://localhost..."]` or a host that is not loopback. `git grep -n 'or \["https\?://localhost' src` finds 11 such sites: 2 in this module and 9 in `src/devops_cli/ai/ai_plans.py`.
- `resolve_ai_endpoint` raises `ConfigurationError` with `key="ai.gateway_url"` for a gateway with no URL and `key="ai.ollama_urls"` for Ollama with no URL (`tests/ai/test_service_client_configured_urls.py`). `_make_review_clients` in `src/devops_cli/ai/review/runner.py` calls it with the `analysis` task before the review run makes its client.
- `OpenAIClient._openai_client` (`src/devops_cli/ai/client/openai.py`) gives `ai.gateway_url` to the OpenAI SDK as `base_url`, and the SDK posts a chat to `<base_url>/chat/completions`.

## Existing Tool Considered
- The project resolver `resolve_ai_endpoint` covers the need, and the change uses it in place of the copy of the provider rules in the module. No open tool resolves the endpoints of the devops-cli settings.
- Internal code that the change deletes: the provider branches of `_get_review_llm_target` and `_get_review_embedding_target`, with their fixed host and localhost fallbacks.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
## ADDED Requirements

### Requirement: A review dry run takes its model endpoint from the configuration
The dry-run plan of a review command SHALL get the target of each model request from the configured AI endpoint, by the resolver that the review run uses. The model request of a review SHALL use the `analysis` task settings. When the resolver names a setting that is not set, the plan target SHALL start with that setting as a placeholder, such as `<ai.gateway_url>`, and the dry run SHALL make zero requests. A plan target SHALL NOT hold a gateway, Ollama or loopback address that no setting gives.

#### Scenario: Gateway provider with no gateway URL
- **WHEN** `ai.provider` is `gateway`, `ai.gateway_url` is not set and `devops review path src --dry-run` runs
- **THEN** the command exits 0 and its plan has the request `POST <ai.gateway_url>/chat/completions`

#### Scenario: Gateway provider with a gateway URL
- **WHEN** `ai.provider` is `gateway` and `ai.gateway_url` is `https://gateway.example.com/v1`
- **THEN** the review path plan has the request `POST https://gateway.example.com/v1/chat/completions`

#### Scenario: Ollama provider with no Ollama URLs
- **WHEN** `ai.provider` is `ollama` and `ai.ollama_urls` is empty
- **THEN** the review path plan has the request `POST <ai.ollama_urls>/api/chat`, and the corpus generate plan has the request `POST <ai.ollama_urls>/api/embeddings`

#### Scenario: The analysis task names its own provider
- **WHEN** `ai.provider` is `ollama` and `ai.tasks.analysis` sets the provider `gateway` with the base URL `https://gateway.example.com/v1`
- **THEN** the review path plan has the request `POST https://gateway.example.com/v1/chat/completions`
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Tasks

## 1. The review plans take the model endpoint from the configuration

Kind: behavior. Reads: `src/devops_cli/ai/endpoints.py`, `src/devops_cli/ai/review/runner.py`. Changes: `tests/review/test_review_plan_model_endpoint.py`, `tests/commands/test_review_dry_run.py`, `src/devops_cli/review/review_plans.py`.

- [x] 1.1 Write the failing tests: a gateway with no `ai.gateway_url` plans `<ai.gateway_url>/chat/completions`, a configured gateway URL gets no second `/v1`, Ollama with no URLs plans `<ai.ollama_urls>/api/chat` and `<ai.ollama_urls>/api/embeddings`, Claude plans `/v1/messages`, and the `analysis` task provider wins; `devops review path src --dry-run` with no `ai.gateway_url` prints the placeholder and exits 0
- Verification: `$ uv run devops ci test tests/review/test_review_plan_model_endpoint.py tests/commands/test_review_dry_run.py`
- [x] 1.2 Resolve the plan targets with `resolve_ai_endpoint`, show the setting of a `ConfigurationError` as the placeholder, and delete the fixed gateway host and the two localhost fallbacks
- Verification: `$ uv run devops ci test tests/review/test_review_plan_model_endpoint.py tests/commands/test_review_dry_run.py`

## Workflow follow-up

- The nine localhost fallbacks of `src/devops_cli/ai/ai_plans.py`, its gateway and Qdrant fallbacks, and a guard rule that matches a fallback list: #5395.
- Pending a person: review the diff in tuicr (tuicr -r origin/main..HEAD).
19 changes: 19 additions & 0 deletions openspec/specs/configured-service-urls/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,22 @@ The reason text of `normalize_ollama_base_url`, the error text of `_parse_and_va
#### Scenario: A Qdrant error text that holds the request URL
- **WHEN** a native call of `QdrantClient.list_collections` fails with an exception whose text holds `http://user:secret@example.com:6333/prefix/path`
- **THEN** the `QdrantClientError` text and the debug log line hold `example.com:6333` and hold no `user`, no `secret`, no `prefix`, no `path` and no `://`

### Requirement: A review dry run takes its model endpoint from the configuration
The dry-run plan of a review command SHALL get the target of each model request from the configured AI endpoint, by the resolver that the review run uses. The model request of a review SHALL use the `analysis` task settings. When the resolver names a setting that is not set, the plan target SHALL start with that setting as a placeholder, such as `<ai.gateway_url>`, and the dry run SHALL make zero requests. A plan target SHALL NOT hold a gateway, Ollama or loopback address that no setting gives.

#### Scenario: Gateway provider with no gateway URL
- **WHEN** `ai.provider` is `gateway`, `ai.gateway_url` is not set and `devops review path src --dry-run` runs
- **THEN** the command exits 0 and its plan has the request `POST <ai.gateway_url>/chat/completions`

#### Scenario: Gateway provider with a gateway URL
- **WHEN** `ai.provider` is `gateway` and `ai.gateway_url` is `https://gateway.example.com/v1`
- **THEN** the review path plan has the request `POST https://gateway.example.com/v1/chat/completions`

#### Scenario: Ollama provider with no Ollama URLs
- **WHEN** `ai.provider` is `ollama` and `ai.ollama_urls` is empty
- **THEN** the review path plan has the request `POST <ai.ollama_urls>/api/chat`, and the corpus generate plan has the request `POST <ai.ollama_urls>/api/embeddings`

#### Scenario: The analysis task names its own provider
- **WHEN** `ai.provider` is `ollama` and `ai.tasks.analysis` sets the provider `gateway` with the base URL `https://gateway.example.com/v1`
- **THEN** the review path plan has the request `POST https://gateway.example.com/v1/chat/completions`
42 changes: 27 additions & 15 deletions src/devops_cli/review/review_plans.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,45 @@
from pathlib import Path
from typing import Any

from devops_cli.ai.endpoints import resolve_ai_endpoint
from devops_cli.config.settings import load_settings
from devops_cli.dry_run.requests import PlannedRequest
from devops_cli.exceptions import ConfigurationError


def _resolved_base(task: str | None) -> tuple[str, str]:
"""The provider and base URL of the configured AI endpoint, as the review run resolves them.

When a setting is not set, the base URL is that setting as a placeholder, such as
`<ai.gateway_url>`, so the plan names it. The review run stops with the same
`ConfigurationError` (#1165, #5345).
"""
settings = load_settings()
cfg = settings.ai.for_task(task) if task else settings.ai
provider = (cfg.provider or "").lower()
try:
endpoint = resolve_ai_endpoint(cfg, settings=settings)
except ConfigurationError as exc:
return provider, f"<{exc.key or 'ai.provider'}>"
return endpoint.provider, endpoint.primary_url


def _get_review_llm_target() -> str:
"""Resolve the target URL for LLM review and evaluation calls."""
settings = load_settings()
provider = settings.ai.provider
provider, base = _resolved_base("analysis")
if provider == "ollama":
endpoint = (settings.ai.get_ollama_urls or ["http://localhost:11434"])[0]
return f"{endpoint.rstrip('/')}/api/chat"
if provider == "gateway":
gw = (settings.ai.gateway_url or "https://ai.retric.click").rstrip("/")
return f"{gw}/v1/chat/completions"
base = settings.ai.api_base_url or "https://api.openai.com/v1"
return f"{base.rstrip('/')}/chat/completions"
return f"{base}/api/chat"
if provider == "claude":
return f"{base}/v1/messages"
return f"{base}/chat/completions"


def _get_review_embedding_target() -> str:
"""Resolve the target URL for text/code embeddings."""
settings = load_settings()
provider = settings.ai.provider
provider, base = _resolved_base(None)
if provider == "ollama":
endpoint = (settings.ai.get_ollama_urls or ["http://localhost:11434"])[0]
return f"{endpoint.rstrip('/')}/api/embeddings"
base = settings.ai.api_base_url or "https://api.openai.com/v1"
return f"{base.rstrip('/')}/embeddings"
return f"{base}/api/embeddings"
return f"{base}/embeddings"


def plan_review_path(
Expand Down
16 changes: 16 additions & 0 deletions tests/commands/test_review_dry_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from __future__ import annotations

import json
from collections.abc import Callable
from pathlib import Path
from typing import Any

Expand Down Expand Up @@ -228,6 +229,21 @@ def test_request_plan_commands_run_cleanly(name: str, callback: Any, argv: list[
)


def test_review_path_dry_run_names_the_unset_gateway_url(
cli: Callable[..., Any], config: Callable[..., Path]
) -> None:
"""A review dry run with no `ai.gateway_url` names the setting as the target of its model request."""
config(ai={"provider": "gateway", "gateway_url": None})

result = cli("review", "path", "src", "--dry-run")

assert result.exit_code == 0, result.output
assert result.output.splitlines()[:2] == [
"Plan for review path",
" 1. POST <ai.gateway_url>/chat/completions",
]


def test_review_json_dry_run_formats() -> None:
"""Verify json output formats for review dry runs."""
res_score = runner.invoke(
Expand Down
81 changes: 81 additions & 0 deletions tests/review/test_review_plan_model_endpoint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
"""The dry-run plan of a review takes its model endpoint from the configuration (#5345).

The plan names the setting that is not set, and never a host that no setting gives (#1165).
"""

from __future__ import annotations

from collections.abc import Callable
from pathlib import Path
from typing import Any

import pytest

from devops_cli.dry_run.requests import PlannedRequest
from devops_cli.review.review_plans import plan_review_corpus_generate, plan_review_path

REVIEW_BODY = '{"model": "<review-model>", "target": "src", "pattern": "*"}'
CORPUS_BODY = '{"input": "<corpus-source-files>", "sources": "src"}'


@pytest.mark.parametrize(
("ai", "target"),
[
pytest.param(
{"provider": "gateway", "gateway_url": None},
"<ai.gateway_url>/chat/completions",
id="gateway-without-url",
),
pytest.param(
{"provider": "gateway", "gateway_url": "https://gateway.example.com/v1"},
"https://gateway.example.com/v1/chat/completions",
id="gateway-with-url",
),
pytest.param(
{"provider": "ollama", "ollama_urls": []},
"<ai.ollama_urls>/api/chat",
id="ollama-without-urls",
),
pytest.param(
{"provider": "ollama", "ollama_urls": ["http://ollama.example.com:11434/"]},
"http://ollama.example.com:11434/api/chat",
id="ollama-with-urls",
),
pytest.param(
{"provider": "claude"},
"https://api.anthropic.com/v1/messages",
id="claude",
),
pytest.param(
{
"provider": "ollama",
"tasks": {
"analysis": {
"provider": "gateway",
"api_base_url": "https://gateway.example.com/v1",
}
},
},
"https://gateway.example.com/v1/chat/completions",
id="analysis-task-provider",
),
],
)
def test_review_plan_posts_to_the_configured_model_endpoint(
config: Callable[..., Path], ai: dict[str, Any], target: str
) -> None:
"""The review path plan posts to the endpoint of the analysis task, or names its unset setting."""
config(ai=ai)

assert plan_review_path(targets=["src"]) == [
PlannedRequest(method="POST", target=target, stdin=REVIEW_BODY)
]


def test_corpus_plan_names_the_unset_ollama_setting(config: Callable[..., Path]) -> None:
"""The corpus generate plan names `ai.ollama_urls` when no Ollama URL is set."""
config(ai={"provider": "ollama", "ollama_urls": []})

assert plan_review_corpus_generate(sources=["src"]) == [
PlannedRequest(method="POST", target="<ai.ollama_urls>/api/embeddings", stdin=CORPUS_BODY)
]