Skip to content

feat(roadmap): write the project review narrative with a model #4726

Open

Description

@dan-petty

Problem or Motivation

The project review needs prose for the functional change, the lead paragraph and the incident summary. A model can put wrong numbers or invented issues into prose.

Proposed Solution & Architectural Design

  • A pydantic-ai Agent with the output type list[Statement{text, fact_ids, issue_refs}].
  • Validators: no digit in the text; each fact id exists in data.json; each issue number exists in data.json. Code puts in every number.
  • Calls: the functional change by capability, and the lead. The incident draft runs only when an incident trigger fires.
  • Input projections: headings and change titles for large windows.
  • One retry, then the data-only sentences and devops_cli_project_review_narrative_total{result}.
  • The model is the DeepSeek flash route through the gateway (owner decision of 2026-11-11; Sonnet is on hold).
  • A cap of 0.05 USD for each run: the run refuses a call whose estimate goes over the cap. Spend goes under the row project_review.
  • The model timeout is shorter than the run deadline of the job.
  • Prompt files under src/devops_cli/ai/tasks/.
  • A "model-written" label with the model name on each paragraph.

Target Subsystem

SDLC, Git & PR Governance (src/devops_cli/roadmap/project_review/), with AI prompts in src/devops_cli/ai/tasks/.

Existing Tool Considered

pydantic-ai (in use) gives structured output and output validators. The gateway (in use) routes the model and counts the spend. The custom code is the validators and the fallback. Cost (estimate): about 150-250 lines with tests, one issue-size item, about 0.33-0.50 USD on the DeepSeek implement path and 1-2 days elapsed. Run cost: about 0.004 USD for each weekly run (about 44 K tokens in and 3.8 K out at 0.0784 and 0.2352 USD for each million tokens).

Acceptance Criteria

Tests with a stub model:

  • a digit, an unknown fact id and an unknown issue number are refused;
  • the fallback runs after 1 retry.

A golden test on the range v0.111.0..v0.112.1 with a recorded reply.

A weekly run costs 0.01 USD or less on the DeepSeek route, measured in the gateway counter, and records its spend under the row project_review.

Dependencies

Source: #4679

Alternatives check of 2026-11-29: in-use.
The repository already uses this tool.
Tool: pydantic-ai (https://github.com/pydantic/pydantic-ai), MIT, last activity 2026-11-29, maintained.
Reason: Searches found no library that grounds model prose in verified fact ids; pydantic-ai gives the typed statement output and the output-validator retry path, while the digit/fact-id checks, fallback, metric and cost cap stay project code.

Covers:

  • Typed output as list[Statement] from a pydantic model
  • Output validators that raise ModelRetry and retry once
  • Agent built by the shared build_agent factory
  • Prompt and settings plumbing already used by other tasks

Lacks:

  • No digit-free text rule or fact-id and issue-number checks
  • No data-only fallback after a refused retry
  • No per-run cost cap or project_review spend row
  • No narrative result metric or model-written label
  • No input projection to headings and change titles

Proposed design

Problem statement

The project review needs prose for the functional change and the lead paragraph. A model can put a wrong number or an invented issue into that prose. This change adds a model-written narrative that cites checked facts and issue numbers, refuses a digit in the text and an unknown fact id or issue number, renders every number from the cited fact, and falls back to the data-only sentences after one retry. The incident summary and the weekly job are deferred to separate issues.

Acceptance criteria

Requirement: Structured statement output

The narrative agent SHALL return a list of statements, and each statement SHALL hold the text, the fact ids and the issue references.

Scenario: Stub model returns two valid statements

  • WHEN the stub model returns two statements whose fact ids and issue numbers exist in the review data
  • THEN the narrative holds two statements and each statement keeps its text, fact ids and issue references

Requirement: No digit in the text

The validator MUST refuse a statement whose text holds a digit.

Scenario: Statement text with a digit

  • WHEN the stub model returns a statement whose text holds the digit 7
  • THEN the validator refuses the reply and the agent makes one retry

Requirement: Fact id must exist

The validator MUST refuse a statement whose fact id is absent from the review data.

Scenario: Unknown fact id

  • WHEN the stub model returns a statement with fact id f-9999 that the review data does not hold
  • THEN the validator refuses the reply

Requirement: Issue number must exist

The validator MUST refuse a statement whose issue number is absent from the review data.

Scenario: Unknown issue number

  • WHEN the stub model returns a statement with issue number 999999 that the review data does not hold
  • THEN the validator refuses the reply

Requirement: Code renders each number

The narrative SHALL put each number into the paragraph from the fact that the statement cites.

Scenario: Count rendered from the cited fact

  • WHEN a statement cites a fact with the count 12 and its text holds no digit
  • THEN the rendered paragraph holds 12 and the model text holds no digit

Requirement: Fallback after one retry

The narrative MUST use the data-only sentences after one refused retry.

Scenario: Two refused replies

  • WHEN the stub model returns a refused reply on the first and the second attempt
  • THEN the narrative holds the data-only sentences and makes no third model call

Requirement: Narrative metric

The narrative SHALL count each run in devops_cli_project_review_narrative_total with the label result.

Scenario: Fallback counted

  • WHEN the stub model returns a refused reply twice
  • THEN the metric counts one with result="fallback" and no count with result="model"

Requirement: Large window projection

The narrative MUST project the input to the headings and the change titles when the projected input goes over the task's context window.

Scenario: Large window

  • WHEN the projected input goes over the context window of the task project_review
  • THEN the model input holds the headings and the change titles and no change body

Requirement: Cost cap

The run MUST refuse a model call whose cost estimate goes over the cap of 0.05 USD.

Scenario: Estimate over the cap

  • WHEN the cost estimate of the call is 0.06 USD
  • THEN the run makes no call and the narrative uses the data-only sentences

Requirement: Spend row

The run MUST record the spend of each accepted call under the stage project_review.

Scenario: Spend recorded on an accepted call

  • WHEN the stub gateway accepts one call with the cost 0.004 USD
  • THEN the spend ledger holds one record with the stage project_review and the cost 0.004 USD

Requirement: Model-written label

The narrative SHALL label each model-written paragraph with model-written and the model name.

Scenario: Lead paragraph from the model

  • WHEN the model writes the lead paragraph
  • THEN the paragraph carries the label model-written with the model name

Requirement: Run cost

A run MUST cost 0.01 USD or less on the DeepSeek route.

Scenario: Cost from the recorded tokens

  • WHEN a run uses 44 K input tokens and 3.8 K output tokens at 0.0784 and 0.2352 USD for each million
  • THEN the cost is 0.0043 USD, at or below 0.01 USD

Requirement: Golden test

A golden test SHALL compare the narrative for the range v0.111.0..v0.112.1 with a recorded reply.

Scenario: Recorded reply replay

  • WHEN the golden test replays the recorded reply for the range v0.111.0..v0.112.1
  • THEN the narrative matches the recorded expected output

Requirement: Model route

The narrative MUST request the model that the settings of task project_review name.

Scenario: Stub gateway records the model

  • WHEN the settings of task project_review name deepseek-v4.1-flash-1017 and the narrative sends a call
  • THEN the stub gateway records the model deepseek-v4.1-flash-1017

Requirement: Prompt files

The narrative MUST load its prompt through load_task_prompt from a file under src/devops_cli/ai/tasks/.

Scenario: Prompt loaded from the task folder

  • WHEN the narrative builds its prompt for the task project_review
  • THEN the loader reads a prompt file under src/devops_cli/ai/tasks/

Existing Tool Considered

pydantic-ai (https://github.com/pydantic/pydantic-ai) is in use; the alternatives check of 2026-11-23 in the issue body gives the verdict and lists what it covers and lacks. The proposal adds the missing project code. That code is the no-digit, fact-id and issue-number validators, the data-only fallback, the metric, the label and the cost cap. That code costs about 150-250 lines with tests, about 0.33-0.50 USD of tokens on the DeepSeek implement path, and 1-2 days elapsed. A run costs about 0.004 USD.

Key questions

  • Which structured-output mechanism does the narrative use?: A pydantic-ai Agent built by build_agent with the output type list[Statement] and an output_validator that raises ModelRetry. Statement holds text, fact_ids and issue_refs. (best practice) [sources: src/devops_cli/ai/client/factory.py, src/devops_cli/roadmap/refine/refine_investigation.py, src/devops_cli/exceptions/ai_pydantic.py]
    • Chosen: pydantic-ai structured output. The repository already uses it, and it gives the typed output, the output validators and the retry path.
    • Rejected: Guardrails AI output validators. It needs the same custom fact and digit validators and adds a dependency.
    • Rejected: Hand-written JSON parsing of a free-text reply. It gives no typed output and no retry path, so the custom code grows.
  • How does the narrative keep every number correct?: The validator refuses a statement whose text holds a digit. Code puts each number into the paragraph from the fact that the statement cites, so the model writes no number and cannot write a wrong one. (best practice) [sources: src/devops_cli/roadmap/project_review/data.py, src/devops_cli/roadmap/project_review/facts.py]
    • Chosen: No-digit rule and code renders the numbers. The model cannot put a wrong number into the prose, because the model writes no number.
    • Rejected: The model writes the numbers and a checker compares them. The checker must parse the prose, and each mismatch costs another call.
    • Rejected: A post-edit pass removes the numbers. It leaves a sentence that lost its number, and the reader sees a gap.
  • What does the narrative do after one refused retry?: It uses the data-only sentences and counts the run in the metric. The one-retry limit follows _retry_once of the refine investigation, which raises ModelRetry for the first refused reply only. (best practice) [sources: src/devops_cli/roadmap/refine/refine_investigation.py, src/devops_cli/ai/client/factory.py, src/devops_cli/exceptions/ai_pydantic.py]
    • Chosen: One retry, then the data-only sentences. It bounds the calls and the spend, and the page still holds the checked facts.
    • Rejected: No retry. A transient reply error loses the prose on every run.
    • Rejected: Three retries. Each retry costs a call and can go over the 0.05 USD cap.
  • Which values does the result label of devops_cli_project_review_narrative_total hold?: model for a reply that passes, and fallback for the data-only sentences. One counter with one result label follows the result label of devops_cli_roadmap_page_fetches_total. (best practice) [sources: src/devops_cli/telemetry/instruments.py, docs/TELEMETRY.md]
    • Chosen: One counter with the label result. One metric name holds both paths, and a query divides one by the other.
    • Rejected: One counter for each path. Two metric names need two queries and drift apart.
  • Which model route does the narrative call?: The DeepSeek flash route through the gateway, deepseek-v4.1-flash-1017. Sonnet stays on hold: the owner holds claude-sonnet-5-5 since 2026-11-11. (best practice) [sources: SECURITY.md, k8s/devops/configmap.example.yaml]
    • Chosen: DeepSeek flash through the gateway. The owner decision of 2026-11-11 sets this route, and the gateway counts the spend.
    • Rejected: Claude Sonnet through the gateway. The owner put Sonnet on hold.
  • What cost cap and spend row does a run use?: A run refuses a call whose estimated cost goes over 0.05 USD. The estimate comes from PricingRegistry.calculate_request_cost on the token estimate. The spend goes under the stage project_review, which the settings declare as the task ai.tasks.project_review. (best practice) [sources: src/devops_cli/ai/spend/pricing.py, src/devops_cli/ai/spend/stage.py, src/devops_cli/config/settings.py]
    • Chosen: A cap of 0.05 USD for each run and the stage project_review. A large window cannot spend without limit, and the row separates this spend from the other AI spend.
    • Rejected: No cap. A long window can spend without limit on a weekly schedule.
  • Where do the prompt files live?: Under src/devops_cli/ai/tasks/, loaded by load_task_prompt. The narrative adds its prompt files there beside the other task prompts. (best practice) [sources: src/devops_cli/ai/task_loader.py]
    • Chosen: Task prompt files under src/devops_cli/ai/tasks/. The prompts sit with the other AI task prompts and stay out of the validator code.
    • Rejected: Inline prompts in the narrative module. It mixes prompt text with the validators and the fallback.
  • What input does a large window send to the model?: The headings and the change titles only, when the projected input goes over the context window of the task project_review. The facts are already aggregated, so the model needs no change body. (best practice) [sources: src/devops_cli/ai/context_packer.py, docs/CONFIGURATION.md, src/devops_cli/roadmap/project_review/facts.py]
    • Chosen: Headings and change titles. It keeps the input inside the window of the model.
    • Rejected: The full change bodies. The input goes over the window and the call fails.
  • How does a model-written paragraph carry the label?: Each model-written paragraph holds the label model-written and the model name. The review's model analysis section already marks its text Model-Written and keeps it apart from the tool-verified findings. (best practice) [sources: src/devops_cli/review/analysis.py]
    • Chosen: A label field on each paragraph. A reader tells which paragraph the model wrote.
    • Rejected: One footnote at the end of the page. A reader cannot tell which paragraph the model wrote.

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

#4720

Activity

  1. added
    source/agent
    on Nov 11, 2026
  2. added
    type/feature
    scope/roadmap
    status/new
    value/medium
    effort/medium
    on Nov 11, 2026
  3. dan-petty commented on Nov 11, 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/feature. The feat prefix of the title gives the type.
    • Scope: scope/roadmap. The (roadmap) scope of the title gives the scope.
    • Priority: P2-Medium. Improves review narrative quality but is not critical and depends on #4720.
    • Value: Medium. Adds model-written narrative to project reviews but is not essential to core functionality.
    • Effort: Medium. Estimated 150-250 lines with tests and 1-2 days of work.
    • Related: feat(review): evidence-first code review — tools find, version-pinned facts ground, models explain and plan, fixes are re-checked #1137, which asks for a different change. Both ground model output in verified facts, but the candidate writes roadmap project review prose while 1137 reviews code changes.
  4. added
    priority/p2-medium
    on Nov 11, 2026
  5. dan-petty commented on Nov 12, 2026

    @dan-petty
    OwnerAuthor

    Scope change for #5219 (the self-improvement section of the project review):

    #5219 does not wait for this item.

  6. devops-cli-service commented on Nov 14, 2026

    @devops-cli-service
    Contributor

    Alternatives check: in-use

    Reason: Searches found no tool that grounds narrative prose in checked facts; the closest, pydantic-ai (already in use here), gives the structured Statement output, output validators and a retry path, so it covers the model call and refusal half of the item. The checks against data.json, the no-digit rule, the fallback after one retry, the metric and the cost cap remain custom code; Guardrails AI also offers output validators with re-asks but needs the same custom fact and digit validators.

    The repository already uses this tool.

    Tool: pydantic/pydantic-ai (https://github.com/pydantic/pydantic-ai)

    • License: MIT
    • Last activity: 2026-11-14
    • Maintained: yes

    Covers:

    • Typed structured output for the list of Statement{text, fact_ids, issue_refs} through pydantic models
    • Output validators that refuse a reply and send it back to the model for another attempt
    • Control of the agent's retries, which the one-retry rule can build on

    Lacks:

    • No built-in rule that a text holds no digit
    • No built-in check that a fact id or issue number exists in data.json; those validators are custom code
    • No fallback to data-only sentences, no narrative_total metric and no model-written label
    • No per-run cost cap or project_review spend row; that stays with the gateway and the calling code

    Tools considered:

    Searches run:

    • pydantic-ai output validators structured output retry, results: 5
    • python library validate LLM generated text citations against source facts, results: 5
    • pydantic-ai github repository, results: 5
    • guardrails-ai validate LLM output custom validator, results: 5
    • instructor structured outputs LLM pydantic validation retries, results: 5
    • pydantic-ai, results: 5
    • LiteLLM budget cap per request spend tracking, results: 5
    • factscore LLM factual precision library, results: 5
    • BAML structured output validation retries, results: 5
    • verifiable generation citations library python ground LLM text in source facts, results: 5

    Sources:

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

  7. devops-cli-service commented on Nov 15, 2026

    @devops-cli-service
    Contributor

    Refine cannot set this item to Ready at a8a99cb while the items below stay open. The next refine round looks at the item again when they close, or when a person replies in a comment or edits the body.

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

    @devops-cli-service
    Contributor

    Next step: the open items close, or a person replies in a comment or edits the body. See Dependencies in 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/mediumpriority/p2-mediumscope/roadmapsource/agentstatus/newtype/featurevalue/medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions