Skip to content

fix(ai): gateway reviews keep requests in flight at the backend slot count, however many ai.ollama_urls are configured #1354

Closed

Description

@dan-petty

Ready: implementation plan (refined 2026-10-17 at 3dd7fa6)

This section supersedes any text below that conflicts with it.

Decisions

  • Can an explicit --concurrency exceed the cap?: no, not in gateway mode. A value above the slot sum (DEFAULT_GATEWAY_REVIEW_CONCURRENCY, src/devops_cli/config/defaults.py:430) is clamped to it, and one warning names both numbers, for example "--concurrency 8 is above the gateway's 3 backend slots; using 3". The cap protects the backend, and group analysis already clamps the same way (src/devops_cli/review/analysis.py:773-776).
  • Reviews only, or every gateway call?: every chat request that LLMClient sends to the gateway in the process. One process-wide limiter lives in src/devops_cli/ai/client/network.py. It is taken in the two methods every gateway chat call passes through: _openai_compat_messages (src/devops_cli/ai/client/openai.py:177) and _openai_compat_stream (openai.py:206), when provider == "gateway". This covers persona calls, group analysis (analysis.py:700), roadmap intake (src/devops_cli/roadmap/intake_model.py:163-168) and every devops ai chat command, so the code supports it cleanly.
  • What takes no slot: model listing (openai.py:234), the gateway's health and model-info probes (src/devops_cli/ai/gateway.py:239-281) and embeddings. Embeddings use their own client and ai.gateway_concurrency["embedding"] cap (src/devops_cli/ai/rag/embeddings.py:955-968), and the embedding group is served by a backend outside the slot table (k8s/llm/gateway/configmap.yaml:50-53). The pydantic-ai direct path (LLMClient.direct_request*, src/devops_cli/ai/client/unified.py:938) is used by no command, and refactor(ai): one model seam: llmclient over three wire adapters behind one factory, no second provider stack #1001 removes that second stack. So it is left alone.
  • Limiter shape: a module-level threading.BoundedSemaphore(DEFAULT_GATEWAY_REVIEW_CONCURRENCY) and an acquire_gateway_slot() context manager beside acquire_ollama_slot (network.py:243). The wait is bounded by a new DEFAULT_GATEWAY_SLOT_TIMEOUT_SECONDS: float = 900.0 in defaults.py, the same bound as the Ollama lease (defaults.py:74). A wait past it raises AIClientError naming the cap.
  • Slot scope and retries: one slot per logical call, held across the transport's own retries. Retries run inside the pooled client (src/devops_cli/ai/client/base.py:78-92, src/devops_cli/ai/retries/__init__.py:108), so there is no per-attempt hook without a second transport layer. Holding the slot during a backoff only lowers throughput while the backend is already refusing, and it never lets the count exceed the cap. A stream holds its slot until the generator closes.
  • Worker pools in gateway mode: the file pool is min(total_files, slot sum), whatever ai.ollama_urls and ai.ollama_max_parallel hold. A file's personas still run in parallel (pipeline.py:1211, src/devops_cli/ai/agents/pipeline.py:357). The limiter, not the pools, keeps requests in flight at or under the slot sum.
  • Complexity: execute_multi_persona_review (pipeline.py:3658) already carries # noqa: C901, so it gains no branch. The sizing moves into a pure helper, _review_worker_count(config, total_files, requested) -> tuple[int, int | None], which returns the worker count and the clamped request (or None). The caller prints the warning.
  • Non-gateway providers: unchanged. Ollama keeps its per-URL leases (network.py:243-281) and its max(DEFAULT_REVIEW_CONCURRENCY, urls * max_parallel) sizing, and an explicit --concurrency is still honoured there.
  • Pre-analysis sizing (pipeline.py:2593-2603): unchanged. It calls no model (ai_client=None at pipeline.py:1180).
  • Constant names: DEFAULT_GATEWAY_REVIEW_SLOTS and DEFAULT_GATEWAY_REVIEW_CONCURRENCY keep their names. They describe the backends that the review and chat groups route to. A chat call to a group served elsewhere, such as devops-background, still takes a slot. That errs on the side of fewer requests and never more. A rename would rewrite three done task files for no change in behaviour.
  • Wording of the criteria: "verifier" in the body below reads "group analysis". refactor(review): the model verifier, the hallucination catalog and the model-claim guards are deleted, and person-recorded verdicts become suppressions #1150 deleted the verifier stage, and group analysis (feat(review): each finding group gets a model-written explanation and plan grounded in the rule's documentation at the installed tool version, kept apart from findings, with every quote, citation, version and code block checked #1147) is the second stage.

Delivery

  1. PR 1 (S): gateway reviews and every gateway chat call stay within the backend slot sum. This PR closes fix(ai): gateway reviews keep requests in flight at the backend slot count, however many ai.ollama_urls are configured #1354. Tests first:

    • tests/test_review_pipeline.py: with a gateway config, three ai.ollama_urls, ollama_max_parallel=2 and six files, ReviewWorkerPool.create gets concurrency=3 (today 6). With concurrency=8 in gateway mode it gets 3, and one warning names 8 and 3. With provider=ollama and concurrency=8 over ten files it still gets 8.
    • tests/test_gateway_review_stall.py: a gateway LLMClient on a fake transport counts requests in flight, under a lock and with a short hold in each request. Six files with five personas each and three ai.ollama_urls give (peak <= 3, calls) == (True, 30). Today the peak reaches 15.
    • tests/test_ai_client_network.py: eight threads through acquire_gateway_slot() never exceed the cap. With the timeout patched to a few milliseconds and every slot held, a waiter raises AIClientError naming the cap. A provider=openai client sends while every gateway slot is held.

    Files: src/devops_cli/ai/client/network.py, src/devops_cli/ai/client/openai.py, src/devops_cli/ai/review/pipeline.py, src/devops_cli/config/defaults.py, src/devops_cli/lang/en/messages.py (ReviewMessages, :19), src/devops_cli/lang/en/help.py (ReviewCommandHelp.concurrency, :1369), the regenerated CLI docs, the three test files, changelog.d/1354.md (under ### Fixed) and docs/agent/tasks/task-1354-gateway-review-slot-limit.md (Done).

Acceptance criteria

  • In gateway mode, the persona stage's file pool is min(total_files, DEFAULT_GATEWAY_REVIEW_CONCURRENCY), whatever ai.ollama_urls holds. Group analysis keeps its existing clamp (analysis.py:773-776).
  • An explicit --concurrency above the slot sum is clamped to it in gateway mode, with one warning naming both numbers. The warning text lives in ReviewMessages.
  • One process-wide limiter in network.py covers every chat request LLMClient sends with provider == "gateway", streamed or not. Requests in flight never exceed the slot sum.
  • Tests: with three URLs the pool size is 3 (today 6), and peak requests in flight stay at or below 3 (today 15). Each new test's call phase is under 1 s, and none touches the network.
  • The --concurrency help for devops review says it is capped at the gateway's backend slots, and uv run devops docs check passes after uv run devops docs generate --sync-readme.
  • changelog.d/1354.md records the change. CHANGELOG.md and docs/ROADMAP.md are not edited.
  • The PR body cites the fail-to-pass check run for the new tests.
  • uv run devops ci passes.

Current references

  • Worker sizing, gateway and Ollama: src/devops_cli/ai/review/pipeline.py:3711-3735 (batch_capacity at :3727, explicit concurrency at :3728-3729)
  • File worker pool: src/devops_cli/ai/review/pipeline.py:3782
  • execute_multi_persona_review, with # noqa: C901: src/devops_cli/ai/review/pipeline.py:3658
  • Personas run with parallel=True: src/devops_cli/ai/review/pipeline.py:1211
  • Persona thread pool, up to 8: src/devops_cli/ai/agents/pipeline.py:357
  • Group analysis clamp: src/devops_cli/review/analysis.py:773-776
  • Slot table and sum: src/devops_cli/config/defaults.py:425-430
  • Ollama slot lease (the pattern to mirror): src/devops_cli/ai/client/network.py:243-281
  • Gateway chat calls: src/devops_cli/ai/client/openai.py:177-204 and :206-232
  • Provider dispatch: src/devops_cli/ai/client/unified.py:404-424
  • Existing pool-size tests: tests/test_review_pipeline.py:1143-1177
  • Existing gateway review tests: tests/test_gateway_review_stall.py:303

Dependencies and order


Problem

This is a Copilot thread on release PR #1283, verified at b57ef7f. #1069 aimed to keep gateway reviews within the backend's slot count. In gateway mode, though, file workers are max(DEFAULT_GATEWAY_REVIEW_CONCURRENCY, len(ollama_urls) * ollama_max_parallel) at both the persona and verifier stages [code: src/devops_cli/config/defaults.py:445; src/devops_cli/ai/review/pipeline.py:3530, :3827]. Each file also runs its personas in parallel. A probe peaked at 15 requests in flight with one URL and 30 with three, against 3 slots. Gateway traffic goes only to gateway_url, so each extra ai.ollama_urls entry raises the overload without adding capacity. That can rebuild the backend queue #1069 meant to prevent. The owner's setup lists several Ollama URLs. The v0.2.28 release notes no longer claim the limit.

Acceptance criteria

  • In gateway mode, both stages size their worker pools at DEFAULT_GATEWAY_REVIEW_CONCURRENCY, whatever ai.ollama_urls holds.
  • One thread-safe limit at the slot sum covers persona and verifier requests, so requests in flight never exceed it.
  • Tests: with three URLs the pool size is 3 (today 6), and peak requests in flight stay at or below 3 (today 15).
  • changelog.d/<n>.md records the change; CHANGELOG.md and docs/ROADMAP.md are not edited.
  • uv run devops ci passes.

Strategic alignment

Activity

  1. added
    type/bug
    scope/ai
    scope/review
    priority/p1-high
    on Oct 7, 2026
  2. dan-petty-bot commented on Oct 8, 2026

    @dan-petty-bot
    Collaborator

    Intake placed this item to the backlog: at intake only a critical fix goes into a release; a release start pulls Ready items in, P0 features first.

    • Priority: P2-Medium. It undermines the concurrency guarantee #1069 established and can rebuild backend overload for multi-URL setups, but the system still functions and is not a security or total-outage issue.
    • Value: Medium. Users with multiple Ollama URLs (like the owner) experience backend overload and queue growth, but single-URL users are less affected.
    • Effort: Medium. Requires changing pool sizing in two pipeline stages, adding a shared thread-safe semaphore, and writing concurrency tests, but the scope is confined to a few files.
  3. added this to the v0.2.37 milestone on Oct 11, 2026
  4. dan-petty commented on Oct 17, 2026

    @dan-petty
    OwnerAuthor

    Readiness check for v0.2.37, against release/v0.2.36 at 22d8748. Line numbers are at that commit.

    Status: ready after a reference refresh. One pull request (S).

    Today

    • src/devops_cli/ai/review/pipeline.py:3727 sets batch_capacity = max(base_capacity, len(ollama_urls) * max_par), and the worker pool is created at :3782.
    • Personas run with parallel=True (pipeline.py:1211), up to 8 workers (src/devops_cli/ai/agents/pipeline.py:357).
    • There is no in-flight limiter for the gateway. src/devops_cli/ai/client/network.py:158 has only per-Ollama-URL semaphores.

    References that moved

    Dependencies: #1069, #1283 and #1150 are closed.

    The body's 15 was the one-URL figure. With today's three URLs it is 30.

    Questions for the owner

    • Should an explicit --concurrency still be able to exceed the cap? :3729 allows it today.
    • Should the limit apply to reviews only, or to every gateway call?

    Plan, tests first

    • Tests: a three-URL gateway configuration gives review.workers == 3, with at most 3 requests in flight.
    • Fix:
      • In gateway mode, use the slot sum alone.
      • Add a process-wide BoundedSemaphore in ai/client/network.py around each persona call and each group-analysis call.
    • Add changelog.d/1354.md.
  5. added
    status/in-progress
    on Oct 17, 2026
  6. added a commit that references this issue on Oct 18, 2026
    30b8fdd
  7. dan-petty commented on Oct 18, 2026

    @dan-petty
    OwnerAuthor

    What changed

    #1639, merged into release/v0.2.37 as 30b8fdd.

    In gateway mode a review sized its persona-stage file pool by ai.ollama_urls times ai.ollama_max_parallel, although gateway traffic goes only to gateway_url, and each file runs its personas in parallel. With three URLs the stage reviewed six files at once and kept more requests in flight than the gateway's 3 backend slots, which rebuilds the backend queue #1069 meant to prevent.

    One limiter per process now covers every chat request LLMClient sends to the gateway. src/devops_cli/ai/client/network.py holds a BoundedSemaphore sized at DEFAULT_GATEWAY_REVIEW_CONCURRENCY and acquire_gateway_slot(), whose wait is bounded by the new DEFAULT_GATEWAY_SLOT_TIMEOUT_SECONDS (900 s) and fails with an AIClientError naming the slot count. uses_gateway_slots(config), true for the gateway provider only, is the single test both the client and the review sizing use, so a client with ai.gateway_enabled and another provider (whose chat calls go elsewhere) takes no slot and keeps its node sizing and an explicit --concurrency. _openai_compat_messages and _openai_compat_stream take a slot for the gateway provider, which covers persona calls, group analysis, roadmap intake and the devops ai chat commands. A stream holds its slot until its generator closes, and one logical call holds one slot across the transport's own retries. Model listing, gateway probes, embeddings and other providers take no slot. The limit is per process: a second CLI process or the in-cluster…

    24 file(s) changed.

    How it was verified

    Check runs at 3f7f716:

    • Prune PR DevContainer Images: pass
    • Audit Defect PR Hardening: skipping
    • zizmor: pass
    • devops-cli: pass
    • bandit: pass
    • CodeQL: pass
    • Dependency Freshness: skipping
    • Rule Pack Tests: pass
    • Fail-to-Pass Self-Check: pass
    • Changed-Test Independence: pass
    • Tests & Coverage: pass
    • Analyze (python): pass
    • Fail-to-Pass Check: pass
    • Analyze (actions): pass
    • Static Analysis: pass
    • DevContainer Image: skipping
    • Service Image: skipping

    Acceptance Criteria of docs/agent/tasks/task-1354-gateway-review-slot-limit.md:

    • With the gateway provider, the persona stage's file pool is min(total_files, DEFAULT_GATEWAY_REVIEW_CONCURRENCY), whatever ai.ollama_urls holds: test_gateway_file_pool_is_the_slot_sum_whatever_the_ollama_nodes gets 3 with three URLs and ollama_max_parallel=2 over six files (6 on the base). Group analysis keeps its clamp.
    • An explicit --concurrency above the slot sum is clamped to it with the gateway provider, with one warning naming both numbers, whose text lives in ReviewMessages (test_gateway_concurrency_above_the_slot_sum_is_clamped_with_one_warning). An Ollama review keeps its node sizing (6) and an explicit 8 over ten files with no warning, with ai.gateway_enabled set or not (test_ollama_review_keeps_its_node_sizing_and_an_explicit_concurrency).
    • One limiter per process in network.py covers every chat request LLMClient sends with provider == "gateway", streamed or not. Eight threads never hold more than the slot sum (test_gateway_slots_never_admit_more_than_the_slot_sum), a wait past the timeout raises an AIClientError naming the slot sum (test_a_wait_past_the_slot_timeout_names_the_slot_sum), a gateway chat call sends nothing while every slot is held, plain or streamed (test_a_gateway_chat_call_waits_for_a_gateway_slot), an open stream keeps its slot after its first chunk and gives it back when closed early (test_a_gateway_stream_holds_its_slot_until_the_generator_closes), and an openai client sends while every gateway slot is held, with ai.gateway_enabled set or not (test_another_provider_sends_while_every_gateway_slot_is_held).
    • Six files with two personas each over three ai.ollama_urls, through a gateway LLMClient on a fake gateway that holds each request until more than the slot sum are in flight or 0.25 s after the first request, make 12 calls with at most 3 in flight (TestGatewayRequestsStayWithinTheSlotSum). The same test fails at 4 in flight on the base and with the limiter switched off. No new test touches the network.
    • A gateway call that waits 120 s for a slot and 2 s for its reply records one ai.llm.gateway_slot span under its call span, one 120 s observation of the wait histogram, and 2 s as its llm.wall_seconds and spend duration, plain or streamed (test_a_gateway_call_is_timed_from_when_its_slot_is_held).
    • devops review benchmark -c 8 records 3 as its setup's concurrency through the gateway and 8 with Ollama (test_a_review_benchmark_records_the_concurrency_its_reviews_ran_at).
    • A repaired reply is a plain FormattedLLMResponse, with or without a schema (test_a_repaired_reply_builds_no_model_class_of_its_own).
    • The --concurrency help for devops review says it is capped at the gateway's backend slots, and uv run devops docs check passes after uv run devops docs generate --sync-readme.
    • changelog.d/1354.md records the change under ### Fixed and ### Added. CHANGELOG.md and docs/ROADMAP.md are not edited.
    • Each new test's call phase is under 1 s. On the shared development host at a load average of 20 to 35, TestGatewayRequestsStayWithinTheSlotSum took 0.39 s to 0.81 s over ten runs, and the limiter and sizing tests each run in under 0.15 s.
  8. added a commit that references this issue on Oct 20, 2026
    954914e
  9. removed
    status/in-progress
    on Oct 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority/p1-highscope/aiscope/reviewtype/bug

    Type

    No type

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions