Repository navigation
Description
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_decidecalls 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_decidetwice on one model whose client is anhttpx.AsyncClientthat 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_jevtwice 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
ValueErrorindecide - THEN
_run_decideraisesValueError, 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
decidereceives 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_decidecallsasyncio.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_decidekeep 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
dan-petty commentedon Nov 10, 2026
Intake placed this item to the backlog: intake puts every new item in the backlog. A person plans the milestones.
fixprefix of the title gives the type.(ai)scope of the title gives the scope.It breaks every caller that reuses one Jev model and blocks open items 4282, 4298, 4306 and 4308.Any long-lived process that calls Jev more than once with one model is broken today.The fix needs a new model per call or a shared loop, plus a test with a local HTTP server.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.devops-cli-service commentedon Nov 10, 2026
Status set to Ready at ec160a2 (see proposed design in issue body).
dan-petty commentedon Nov 10, 2026
Extra evidence from #4298 (live run, 2026-11-10). A fix that wraps the call in
async with modeldoes not help for the model thatcreate_jev_modelbuilds: the gateway settings give the provider a client of the HTTP entry point (create_pydantic_ai_providerpasseshttp_client), so the provider does not own it and_refresh_http_clientdoes nothing. What works is a new model for each request andawait client.aclose()on the loop that used it.src/devops_cli/roadmap/intake/intake_jev.pydoes this (PR #4323), and its testtest_a_model_the_gateway_settings_build_decides_one_request_after_anothershows the pattern. I filed #4318 for the same fault before I saw this issue; #4318 is a duplicate of this one.docs(openspec): archive the change of #4319
docs(openspec): archive the change of #4319
devops-cli-service commentedon Nov 10, 2026
Released in v0.106.1: https://github.com/dan-petty/devops-cli/releases/tag/v0.106.1
devops-cli-service commentedon Nov 10, 2026
Live since 2026-11-10 06:39 UTC