Skip to content

fix(ai): a second Jev request on one model fails #4319

Closed

Description

@dan-petty

What happened

A second request on one Jev model fails with RuntimeError: Event loop is closed. _run_decide in src/devops_cli/ai/jev.py runs each request with asyncio.run. The HTTP client that create_jev_model builds belongs to the event loop of the first asyncio.run, and that loop is closed when the request ends. The tests do not show it, because httpx2.MockTransport has no loop.

Evidence

Found on 2026-11-10 while building the Jev completeness eval (#4308). One model, two calls of model.decide through asyncio.run, against the live gateway: the first call answers, the second raises at selector_events.py _check_closed. A new model for each call (create_jev_model() in a loop) works: 2 of 2 calls answered.

What should happen

A function that calls Jev more than once with one model works on every call, or the public helper builds the model for each call. A test fails today: it makes two requests on one model through a client that has a loop (for example a local HTTP server), and it passes after the fix.

Impact

Any caller that keeps a model for more than one request is affected: the open items #4282, #4298 and #4306 call Jev from a long-lived process. #4308 builds a new model for each request as a workaround in its own module.

Acceptance criteria

  • A test makes two _run_decide calls on one model over a real local HTTP server and both answer.
  • No caller needs a new model for each request.

Proposed design

Problem statement

A second request on one Jev model fails with RuntimeError: Event loop is closed. _run_decide in src/devops_cli/ai/jev.py calls asyncio.run for each request. The HTTP client of the model binds to the loop of the first call, and asyncio.run closes that loop when the call ends. Every caller that keeps a model for more than one request is affected, and the tests do not show the defect because httpx2.MockTransport has no loop.

Acceptance criteria

Requirement: Repeated requests on one model

_run_decide SHALL answer every request that it makes on one model, including a model whose HTTP client belongs to an event loop.

Scenario: Two requests over a local HTTP server

  • WHEN a test calls _run_decide twice on one model whose client is an httpx.AsyncClient that talks to a real local HTTP server
  • THEN both calls return a response and neither raises RuntimeError

Requirement: No new model for each request

The public Jev helpers SHALL work on every call when the caller passes the same model to them more than once.

Scenario: Two routing calls with one model

  • WHEN a test calls route_with_jev twice with the same loop-bound model
  • THEN both calls return a decision

Requirement: Errors reach the caller

_run_decide MUST raise to the caller the exception that model.decide raises, with the same type.

Scenario: Failing decide call

  • WHEN a test model raises ValueError in decide
  • THEN _run_decide raises ValueError, and a later call on the same model still runs

Requirement: Settings merge is kept

_run_decide SHALL pass to decide the model settings merged with the settings of the call, and the call settings SHALL win.

Scenario: Call settings override model settings

  • WHEN a test model has a setting and the call gives a different value for the same key
  • THEN decide receives the value of the call

Existing Tool Considered

The BlockingPortal of anyio (anyio.from_thread.start_blocking_portal, MIT license, maintained) covers the need. It runs one event loop in a background thread and lets a sync caller run a coroutine on it with portal.call. It lacks only the lifetime of the portal: the change adds about 15 lines that start it on first use and stop it at exit. The cost is small: one short build session and one test file. A hand-written loop thread would cost more tokens to build and to fix, so the change does not use one.

Key questions

  • What causes the failure of the second request?: _run_decide calls asyncio.run(model.decide(...)) for each request. The HTTP client of the model binds to the loop of the first call, and that loop is closed when the call ends. The second call then uses a closed loop. [sources: src/devops_cli/ai/jev.py:162]
  • How does _run_decide keep one event loop alive for all requests on a model?: Run every request on one long-lived loop in a background thread, through an anyio blocking portal that starts on first use and stops at interpreter exit. The client then always meets the same open loop, and no caller changes. (best practice) [sources: src/devops_cli/ai/jev.py]
    • Chosen: Shared anyio blocking portal. An existing, maintained library gives a persistent loop with a thread-safe sync call. It fixes models that the caller passes in, and it needs no knowledge of the internals of pydantic_ai.
    • Rejected: Build a new model for each call in the public helpers. It does not fix a model that the caller passes in, and the acceptance criteria say that no caller needs a new model for each request.
    • Rejected: Hand-written loop thread with asyncio.run_coroutine_threadsafe. It repeats what the anyio portal does, and the project must keep and fix that code.
    • Rejected: Close and rebuild the HTTP client after each call. It depends on private fields of the client in pydantic_ai and breaks when that library changes.

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Activity

  1. added
    source/agent
    type/bug
    scope/ai
    status/new
    value/high
    effort/medium
    on Nov 10, 2026
  2. dan-petty commented on Nov 10, 2026

    @dan-petty
    OwnerAuthor

    Intake placed this item to the backlog: intake puts every new item in the backlog. A person plans the milestones.

    • Type: type/bug. The fix prefix of the title gives the type.
    • Scope: scope/ai. The (ai) scope of the title gives the scope.
    • Priority: P1-High. It breaks every caller that reuses one Jev model and blocks open items 4282, 4298, 4306 and 4308.
    • Value: High. Any long-lived process that calls Jev more than once with one model is broken today.
    • Effort: Medium. The fix needs a new model per call or a shared loop, plus a test with a local HTTP server.
    • Related: refactor(ai): jev.py exposes a public one-request decide function #4309, which asks for a different change. Both touch _run_decide in jev.py, but 4309 adds a public wrapper while the candidate fixes the closed-loop crash on a second call.
  3. added
    priority/p1-high
    on Nov 10, 2026
  4. added
    status/ready
    and removed
    status/new
    on Nov 10, 2026
  5. devops-cli-service commented on Nov 10, 2026

    @devops-cli-service
    Contributor

    Status set to Ready at ec160a2 (see proposed design in issue body).

  6. dan-petty commented on Nov 10, 2026

    @dan-petty
    OwnerAuthor

    Extra evidence from #4298 (live run, 2026-11-10). A fix that wraps the call in async with model does not help for the model that create_jev_model builds: the gateway settings give the provider a client of the HTTP entry point (create_pydantic_ai_provider passes http_client), so the provider does not own it and _refresh_http_client does nothing. What works is a new model for each request and await client.aclose() on the loop that used it. src/devops_cli/roadmap/intake/intake_jev.py does this (PR #4323), and its test test_a_model_the_gateway_settings_build_decides_one_request_after_another shows the pattern. I filed #4318 for the same fault before I saw this issue; #4318 is a duplicate of this one.

  7. added
    status/in-progress
    and removed
    status/ready
    on Nov 10, 2026
  8. added
    status/in-review
    and removed
    status/in-progress
    on Nov 10, 2026
  9. removed
    status/in-review
    on Nov 10, 2026
  10. devops-cli-service commented on Nov 10, 2026

    @devops-cli-service
    Contributor

    Live since 2026-11-10 06:39 UTC

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

    effort/mediumpriority/p1-highscope/aisource/agenttype/bugvalue/high

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions