Skip to content

refactor(ai): ask Jev through Agent output types #5282

Open

Description

@dan-petty

Problem or Motivation

Every live Jev path builds a DecisionRequest by hand and calls DecisionModel.decide() directly:

  • stage.verify_screen through src/devops_cli/roadmap/decision_step.py:137-207,
  • intake through src/devops_cli/roadmap/intake/intake_jev.py:316-323,
  • the helpers in src/devops_cli/ai/jev.py:165-219.

Around these calls, we keep our own event-loop glue: an anyio blocking portal (jev.py), asyncio.run with a client close for each request (decision_step.py), and asyncio.run for each call (intake). This glue failed three times: #4319, #4513, and item 2 of #5272 (asyncio.run inside a running loop).

A direct decide() call gets none of the parts that the pydantic-ai base class gives on the Agent path (pydantic_ai/models/decision.py:546-562,591-594,655-656 in 2.59.0; SystemOneModel.decide checks no limits):

The documents name the Agent path as the normal integration:

Proposed Solution & Architectural Design

Move the live calls to Agent(SystemOneModel, output_type=...):

  1. stage.verify_screen: an output model with one bounded float field (Field(ge=0, le=1)) for each question. The field description holds the question. band_of keeps the raw probabilities and compares them with the fitted cuts in our code.
  2. Intake Value and Effort: an IntEnum rubric for each field.
  3. Intake type: an Enum with UseEnumMemberDocstrings, so that each member docstring is the option text.
  4. Intake duplicates: a list field over the shortlist options.
  5. devops ai ask-jev: the same path, with an output type that the kind of question selects.
  6. Callers: an async caller uses await agent.run(...). A sync caller uses agent.run_sync(...), which keeps one loop for each thread. run_sync still fails inside a running loop, so a caller in a loop must use the async form.
  7. Glue: delete the portal and the asyncio.run glue that the move makes unused.
  8. Telemetry: keep content off in the spans, under the telemetry privacy default (cluster OTel only, no prompt or tool content; InstrumentationSettings(include_content=False)). The span keeps the names, the probabilities, the thresholds and the usage.
  9. Refit: the field descriptions change the request text, so the cuts of stage.verify_screen need a new fit. Do this move with the question rewrite of feat(roadmap): ask the verify screen one atomic question each #5281, and fit one time (feat(roadmap): the verify screen records serious findings for a live fit #5273).

Files: src/devops_cli/ai/jev.py, src/devops_cli/ai/jev_ask.py, src/devops_cli/roadmap/decision_step.py, src/devops_cli/roadmap/decision_point.py, src/devops_cli/roadmap/intake/intake_jev.py, src/devops_cli/roadmap/stage/stage_jev_screen.py, tests/ai/, tests/roadmap/.

Target Subsystem

AI & Agentic Review Pipeline (src/devops_cli/ai/, and the decision points in src/devops_cli/roadmap/).

Existing Tool Considered

pydantic-ai's own decision-model support: Agent with SystemOneModel and an output_type (pinned 2.59.0). The project rule is to integrate open tools, not to keep custom glue. This item deletes glue and builds no new tool. Cost: about 0.8M tokens and 6 hours.

Acceptance Criteria

  • A test with the JevFake transport: stage.verify_screen runs through Agent.run and gives the band from the raw probabilities of the reply.
  • A test: a decision called inside a running event loop with the fake gives a decision, not RuntimeError.
  • A test: a reply with one missing answer gives the fallback (intake: triage; decision point: band none), not KeyError.
  • A test: one call adds its input and output tokens to RunUsage, and the spend hook sees the call.
  • A test: the span of a call holds the probabilities, and no state text and no question text.
  • git grep -nE "asyncio\.run|blocking_portal|BlockingPortal" -- src/devops_cli/ai/jev.py src/devops_cli/roadmap/decision_step.py src/devops_cli/roadmap/intake/intake_jev.py returns nothing.

Related

Source: https://pydantic.dev/docs/ai/models/decision/

Alternatives check of 2026-11-16: not-applicable.
Class: refactor, no search.
Tool: none found.

Proposed design

Problem statement

The live Jev paths build a DecisionRequest by hand and call DecisionModel.decide() directly. Each path keeps its own event-loop glue, and that glue failed three times (#4319, #4513, #5272 item 2). A direct call gets no decide span, no limit check, no RunUsage and no check that every asked answer came back; the decision points also record no Jev spend. This change moves the live calls to Agent with SystemOneModel and an output_type, uses build_agent so the spend hook sees each call, deletes the glue, and leaves the cut refit to #5273.

Acceptance criteria

Requirement: The screen answers through the Agent output

stage.verify_screen SHALL send its questions through the Agent path with one bounded float field for each question, and SHALL give the band from the raw probabilities and the fitted cuts.

Scenario: The fake reply gives the band

  • WHEN A test runs the screen through build_agent with the JevFake transport that answers 0.9 to the flag question and 0.1 to each other question.
  • THEN The record holds each raw probability of the reply, and its band equals the band that band_of gives for those probabilities and the cuts of the point.

Requirement: A decision inside a running loop

A decision point SHALL return its decision when a caller awaits it inside a running event loop, and SHALL NOT raise RuntimeError.

Scenario: Await inside a loop

  • WHEN A test awaits ResolverChain.adecide inside a running event loop with the JevFake transport.
  • THEN The call returns a DecisionRecord and raises no RuntimeError.

Requirement: A missing answer gives the fallback band

A reply that misses one asked answer SHALL give the fallback band (None, counted as none) with resolver fallback, not KeyError.

Scenario: Partial reply of a decision point

  • WHEN The JevFake transport answers one of two asked questions.
  • THEN ResolverChain.adecide records no band with resolver fallback, and raises no KeyError.

Requirement: A missing answer gives the intake fallback

A reply that misses one asked intake answer SHALL give the triage result, not KeyError.

Scenario: Partial reply of intake

  • WHEN The JevFake transport answers one of the asked intake questions.
  • THEN Intake records triage and raises no KeyError.

Requirement: The run usage reaches the spend hook

One Jev call SHALL add its input and output tokens to the run's RunUsage, and the spend hook of build_agent SHALL record the call.

Scenario: One call is spent

  • WHEN A test runs one decision through build_agent with the JevFake transport, which reports 64 input tokens and one output token for its one answer, and a spend ledger.
  • THEN The run's usage holds 64 input tokens and 1 output token, and the ledger holds one row for the call.

Requirement: The span keeps no content

The span of a Jev call SHALL hold the question names, the probabilities, the thresholds and the usage, and SHALL hold no state text and no question text.

Scenario: The span holds no text

  • WHEN A test runs one decision with InstrumentationSettings(include_content=False) and captures the span.
  • THEN The span attributes hold the question names, the probabilities, the thresholds and the usage, and no attribute holds the text of the digest or of a question.

Requirement: No event-loop glue stays

The live Jev modules SHALL NOT use asyncio.run, blocking_portal or BlockingPortal.

Scenario: The three modules hold no glue

  • WHEN A test reads the source of src/devops_cli/ai/jev.py, src/devops_cli/roadmap/decision_step.py and src/devops_cli/roadmap/intake/intake_jev.py.
  • THEN No line of those modules holds asyncio.run, blocking_portal or BlockingPortal.

Requirement: ask-jev answers each kind

devops ai ask-jev SHALL send its one question through the Agent path with an output type that the kind selects.

Scenario: Each kind gives its typed answer

  • WHEN A test calls ask_jev for the choice, the noul and the score kind with the JevFake transport.
  • THEN Each call returns a JevAnswer that holds the answer and the probabilities of the reply.

Requirement: Intake reads the enum answers

Intake SHALL read the Value, the Effort, the type and the duplicate answers from the enum and list fields of one output model.

Scenario: Enum and list answers

  • WHEN The JevFake transport answers each enum field with one of its members and the duplicate list with one shortlist option.
  • THEN The intake record holds that member for each field and that option in the duplicate list.

Existing Tool Considered

pydantic-ai 2.75.0, pinned at pyproject.toml:40 and locked at uv.lock:3557-3558, covers the need. Agent with SystemOneModel and an output_type gives the decide span, the limit check, RunUsage and the missing-answer check, and build_agent adds the project's spend hook (src/devops_cli/ai/client/factory.py:183-250,253-300). It lacks no integration piece; the only follow-up is the cut refit of #5273 because the field descriptions change the request text. The change builds no tool of its own. Its written cost is about 0.8M tokens and 6 hours. The alternatives check of 2026-11-16 in the issue body gave not-applicable with Tool: none found; it is a refactor check and made no search.

Key questions

  • How do the live Jev paths call the model now, and what does a direct decide() call lack?: src/devops_cli/ai/jev.py sends each request through the blocking portal. src/devops_cli/roadmap/decision_step.py closes the client on the loop of each request. src/devops_cli/roadmap/intake/intake_jev.py sends from a sync _decide with asyncio.run. A direct call gets no decide span, no limit check, no RunUsage and no missing-answer check. [sources: src/devops_cli/ai/jev.py:125-143,189-210, src/devops_cli/roadmap/decision_step.py:188-223,244-277, src/devops_cli/roadmap/intake/intake_jev.py:259-269,321-328]
  • Which pydantic-ai version does the project pin?: 2.75.0. pyproject.toml:40 pins pydantic-ai[spec]==2.75.0, and uv.lock:3557-3558 locks pydantic-ai 2.75.0. The issue body's 2.59.0 is stale. [sources: pyproject.toml:40, uv.lock:3557-3558]
  • What does the spend ledger hold for a Jev decision point now?: The decision points record no Jev spend now. _decide_closing and _decide_one call decider.decide and do not call record_jev_spend; only decide_with_jev (jev.py:209) and intake's _decide_in_context (intake_jev.py:268) call it. The template comment at k8s/devops/configmap.example.yaml:71-73 says the client records each Jev decision call, which is false for these points. [sources: src/devops_cli/roadmap/decision_step.py:188-223, src/devops_cli/ai/jev.py:209, src/devops_cli/roadmap/intake/intake_jev.py:268, k8s/devops/configmap.example.yaml:71-73]
  • Does this item need the separate small fix for decision-point spend?: No. This item moves the decision points to the Agent spend hook, so the separate fix would be deleted. A small fix can come first only if this item waits. The spend test uses a fake that reports tokens; the live zero usage does not block the refactor. (best practice) [sources: src/devops_cli/roadmap/decision_step.py:188-223, src/devops_cli/ai/spend/ledger.py:1090-1094, k8s/devops/configmap.example.yaml:71-73]
    • Chosen: Let this refactor record the spend through the Agent hook. It removes the direct call and records spend in one change. The small fix would be deleted by this refactor.
    • Rejected: Add record_jev_spend after decider.decide in decision_step.py now. It is a second fix on a path that this refactor replaces. Use it only if this item waits.
  • Does the Agent path carry a decision request, and what does it add?: Yes. The decision-model page says an agent runs on a decision model like any other model, each field of output_type becomes one question, and a Pydantic model with several fields extracts several values in one request. The project pins pydantic-ai 2.75.0, not the 2.59.0 of the issue body. The issue's cited library lines name the decide span, the limit check, RunUsage and the UnexpectedModelBehavior error for a missing answer; build_agent adds the project spend hook. [sources: pyproject.toml:40, uv.lock:3557-3558, src/devops_cli/ai/client/factory.py:183-250,253-300]
  • Which output type carries the stage.verify_screen answers?: One float field for each question, bounded with Field(ge=0, le=1), with the question in the field description. band_of keeps the raw probabilities and compares them with the fitted cuts. (best practice) [sources: src/devops_cli/roadmap/stage/stage_jev_screen.py:63-88,172-183, src/devops_cli/roadmap/decision_step.py:163-176]
    • Chosen: Bounded float for each question. The cut of each question differs and the fit sets it. The page says a field whose bar varies per call is better as a float and compared in your own code.
    • Rejected: IntEnum rubric for each question. A rubric gives a level. band_of needs the probability that the cuts compare.
    • Rejected: Bare output with the question in the instructions. The page says each field carries its own question.
  • Which output type carries the closed-label answers of intake?: One output model holds an IntEnum for Value and for Effort, an Enum with UseEnumMemberDocstrings for the type, and a list field over the shortlist options for the duplicates. Each member docstring is the option text. (best practice) [sources: src/devops_cli/roadmap/intake/intake_jev.py:133-158,232-249]
    • Chosen: Enum and IntEnum fields in one output model. Each field is a closed set of labels. The member is the answer and no cut is needed.
    • Rejected: Bounded floats with fitted cuts. The fields are labels, not probabilities. No fit varies their bar.
    • Rejected: Free-text strings. A label outside the set would reach the caller.
  • How does a sync caller run a Jev decision without an event-loop conflict?: The live paths build the Agent with build_agent so the spend hook is attached. An async caller awaits agent.run(...). A sync caller calls agent.run_sync(...), which keeps one loop for each thread. A caller that already runs a loop uses the async form, because run_sync fails inside a running loop. The portal goes. (best practice) [sources: src/devops_cli/ai/jev.py:125-143,189-210, src/devops_cli/roadmap/decision_step.py:244-277, src/devops_cli/roadmap/decision_point.py:294-331, src/devops_cli/ai/client/factory.py:253-300]
  • Which span content does a Jev call keep?: InstrumentationSettings(include_content=False). The span keeps the question names, the probabilities, the thresholds and the usage. It keeps no state text and no question text. (best practice) [sources: src/devops_cli/telemetry/library_spans.py:126-133, tests/telemetry/test_library_spans.py:94-99]
    • Chosen: Content off. The telemetry privacy default keeps prompt and tool content out of the cluster OTel spans.
    • Rejected: Content on. It puts item text and question text in the spans.
  • What does a reply that misses an asked answer do?: The Agent path raises UnexpectedModelBehavior. The chain catches it and gives the fallback: no band (counted as none) for a decision point and triage for intake. The caller never sees KeyError. (best practice) [sources: src/devops_cli/roadmap/decision_step.py:91-119,188-223, src/devops_cli/roadmap/decision_point.py:294-331, src/devops_cli/roadmap/intake/intake_jev.py:347-354]
  • Which output type does devops ai ask-jev use?: The Agent is built for each call from the kind and its inputs, over the shared model. A choice question gets one field for each option, a score question one field for each level, and a noul question one probability field. (best practice) [sources: src/devops_cli/ai/jev_ask.py:131-160,215-257, src/devops_cli/ai/jev_ask_models.py:23-43]
    • Chosen: One Agent for each call, from the kind and its inputs. The option labels and the level count come from the call, so the output type cannot be fixed at import.
    • Rejected: One fixed output type with a map of probabilities. The page says each field carries its own question.
    • Rejected: Keep the direct decide() call for ask-jev. It keeps the glue and gets no span, no limit check and no RunUsage.
  • Where does the refit of the stage.verify_screen cuts happen?: In feat(roadmap): the verify screen records serious findings for a live fit #5273, after feat(roadmap): ask the verify screen one atomic question each #5281 and this item. The field descriptions change the request text, and a fit needs live cases. This item changes no cut file, so the band of the screen stays fitted to the old request text until feat(roadmap): the verify screen records serious findings for a live fit #5273 lands. (best practice) [sources: src/devops_cli/roadmap/stage/stage_jev_screen.py:55,172-183, src/devops_cli/roadmap/decision_step.py:138-160]

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Activity

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

    @dan-petty
    OwnerAuthor
  3. added
    priority/p2-medium
    on Nov 12, 2026
  4. devops-cli-service commented on Nov 16, 2026

    @devops-cli-service
    Contributor

    Alternatives check: not-applicable

    Class: refactor. The check makes no search and no model call for an item of this class.

    Tool: none found

    Checked on 2026-11-16. A person decides what this means for the item. The check changes no label and closes no issue.

  5. added
    status/ready
    and removed
    status/new
    on Nov 17, 2026
  6. devops-cli-service commented on Nov 17, 2026

    @devops-cli-service
    Contributor

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

  7. dan-petty commented on Nov 19, 2026

    @dan-petty
    OwnerAuthor

    New evidence from the Service config review of 2026-11-19: the Jev calls of the decision points are not in the spend ledger now.

    • record_jev_spend has two callers on origin/main: src/devops_cli/ai/jev.py:209 and src/devops_cli/roadmap/intake/intake_jev.py:268. Intake Jev is off in the cluster. _decide_closing and _decide_one (src/devops_cli/roadmap/decision_step.py:182-217) call decider.decide and record no spend.
    • Prometheus, 7 d: sum by (model)(increase(devops_cli_ai_spend_usd_total[7d])) and the same query on devops_cli_ai_requests_total give no Jev series. Over the same 7 d, devops_cli_decision_point_total counts about 233 stage.verify_screen decisions with the resolver model.
    • LiteLLM counts 0 input tokens for typesafe/jev-1.14, because the pass-through gives no usage. So no source holds the Jev token volume now.
    • The template comment at k8s/devops/configmap.example.yaml:68-69 says "The client records the tokens of each Jev decision call in the spend ledger". That is false for the decision points today.
    • Side effect for the design: track_request_spend calls record_attempt_ai (src/devops_cli/ai/spend/ledger.py:1090-1092), so after the fix the model_calls= and tokens= of a stage result line include the Jev calls.

    If this refactor waits, a small fix can come first: call record_jev_spend(response) after decider.decide in decision_step.py, with the model name of the call. The same change must make the template comment true.

  8. devops-cli-service commented on Nov 27, 2026

    @devops-cli-service
    Contributor

    Next step: the stage job writes the plan of the item. See the section Proposed design of the body.

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/highpriority/p2-mediumscope/aisource/agentstatus/readytype/refactorvalue/medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions