Skip to content

feat(roadmap): the stage context gives each block a bound and a cut order #5469

Open

Description

@dan-petty

Problem or Motivation

collect_context (src/devops_cli/roadmap/refine/refine.py) joins its sections and cuts the joined text from the end with truncate_to_token_limit. The stage job caps it at 20,480 tokens (CONST_ROADMAP_STAGE_CONTEXT_MAX_TOKENS):

Proposed Solution & Architectural Design

  1. One allocation table in code. Each block has a name, a bound and a place in the cut order:
  2. Put the fixed blocks first in the user message, before number, title and body: sorted, with no date, from the staged commit. They stay user data. Only text that the program makes may go in the system part.
  3. Send context as a separate plain-text part of the user message, not as a JSON string. Change line 3 of roadmap_stage_change.md to say that this part is data too. Run the prompt-adversary fixtures on the new form.
  4. Count each bound in the text that the model reads. State the o200k factor of the billed tokenizer next to the table.
  5. Log, for each request, the tokens of each block after the allocation, the tokens that each block lost, and the first-request split into the fixed part and the item part. Counts only.
  6. The given set of the trace gate (fix(roadmap): the stage trace gate counts the text that the request gives #5466) is built after the allocation.
  7. The refine proposal call uses the same table with its own cap (fix(roadmap): the refine proposal caps its context with a job constant #5464).

The first request grows by about the size of CONTEXT.md (about 5.5k), because it leaves the item bound. With the cache, that is about 77k cached tokens for each stage run of 14 requests, less than $0.001 on DeepSeek. On a Claude route it waits for #3658.

Experiments for the reserved rows (one pre-registered plan, in the harness of the pipeline split experiments E1 and E5, with the same 51 stage cases and one decision rule):

  • Requirement text (feat(roadmap): Add requirement text to the stage context #3246): arm A requirement names, arm B the text of the requirements that the item cites. Primary measure: read_spec calls for each run.
  • Subject manifest: arm A today, arm B the paths (no docstrings) of the subject package and its test folder, at most 500 tokens, inside the item bound. Primary measure: model requests for each run. Pass rule: at least one fewer request for each run.
  • CONTEXT.md (chore(roadmap): measure the cache share of a stage plan run #4583): arm A full text, arm B a term index of about 400 tokens. Secondary measure: a count of the _Avoid_ aliases of CONTEXT.md in the plan text.
  • Decision rule for each arm: paired tool-call and token counts per case are the primary measure. The eval score is a screen with a non-inferiority margin that is set before the run. 51 cases detect only a change of 21 points or more, so add the live guard of the pipeline split: the first-attempt gate-pass rate and the staged rate over 28 days, with Wilson intervals and a stop rule.

Target Subsystem

SDLC, Git & PR Governance: src/devops_cli/roadmap/refine/refine.py (collect_context), src/devops_cli/roadmap/stage/stage.py (_request, the request message), src/devops_cli/ai/tasks/roadmap_stage_change.md, src/devops_cli/config/constants.py.

Existing Tool Considered

pydantic-ai accepts a user prompt of several parts, so the plain-text part needs no new code path. truncate_to_token_limit stays the cut of each block. No open tool allocates a prompt budget by named blocks for this request shape. Cost to build: about 1.5M tokens and 4 hours (estimate), about 150 lines and 8 tests. Keep cost: one table that each new block must join.

Acceptance Criteria

  • Test first: for a body whose cited code passes the item bound, the fixed blocks are whole, the cited code is cut last, and the log line gives the lost tokens of each block.
  • Test first: the fixed blocks come before number in the user message, and two items of one commit get the same bytes for them.
  • A test: the context part is plain text, and the prompt-adversary fixtures pass.
  • Measure: rebuild the context for the 54 item bodies of the sample. Give the kept cited files (baseline 131 of 323) and the first-request tokens by part. Screen with devops roadmap eval stage, paired by case.
  • Live proof after release: on real Service stage runs for 7 days, the kept cited files, the lost tokens by block, and the cache-read share of the first request (baseline 10,368 of 41,597 for feat(ci): Add ast-grep rules for RLIMIT_NPROC, O_NONBLOCK, os.killpg #1976).

Dependencies

Standards Compliance

  • I confirm this proposal aligns with AGENTS.md engineering principles (cyclomatic complexity <= 10, nesting <= 4 blocks, zero bare exceptions, tests that cover the changed lines).

Alternatives check of 2026-11-26: in-use.
The repository already uses this tool.
Tool: pydantic-ai (https://pypi.org/project/pydantic-ai), MIT, last activity 2026-11-25, maintained.
Reason: Searches found prompt-compression tools (LLMLingua, leanctx) and the project's own pydantic-ai; none allocates a prompt budget by named blocks with per-block bounds and a cut order. pydantic-ai covers only the plain-text multi-part user message.

Covers:

  • Multi-part user message carries the plain-text context part
  • No new code path needed for the plain-text part
  • Agent framework the stage and refine calls already use

Lacks:

  • No named per-block token bounds
  • No cut order across blocks; one cut from the end
  • No per-block lost-token counts in a log
  • No rule keeping fixed blocks outside the item bound

Proposed design

Problem statement

The stage context joins its sections and cuts the joined text from the end. Fixed project text takes about half of the 20,480-token cap, so cited code loses text with no signal. The request sends context as one JSON string, so escaping adds tokens, and the fixed text comes after number, title and body. One allocation table gives each block a bound and a cut order. The table puts the fixed blocks first, sends the context as plain text, and logs the tokens that each block kept and lost.

Acceptance criteria

Requirement: Named allocation table

The stage context MUST take the bound and the cut-order position of each block from one allocation table that names the block.

Scenario: Table holds a bound for each block

  • WHEN A test builds the allocation table of the stage context.
  • THEN The table holds a name, a bound and a cut position for each block of collect_context. The item-block bounds sum to no more than CONST_ROADMAP_STAGE_CONTEXT_MAX_TOKENS, and each reserved row has bound 0.

Requirement: Fixed blocks first and stable

The stage request MUST put the fixed blocks first, sorted, with no date, from the staged commit, before number, title and body.

Scenario: Two items share the fixed bytes

  • WHEN A test builds the stage request of two items of one staged commit.
  • THEN The fixed-block bytes are the same for the two items, hold no date, and come before number in the user message.

Requirement: Fixed blocks whole and cited code cut last

The allocation MUST keep the fixed blocks whole and outside the item bound, and MUST cut the cited code last.

Scenario: Cited code exceeds the item bound

  • WHEN A test gives the stage context a body whose cited code exceeds the item bound.
  • THEN The fixed blocks keep their full text, and the cited code loses text only after the other item blocks.

Requirement: Plain-text context part

The stage request MUST send context as a separate plain-text part of the user message, never as a JSON string.

Scenario: Context part is plain text

  • WHEN A test builds the stage request and runs the prompt-adversary fixtures.
  • THEN The user message holds a plain-text context part, and the system part holds none of the fixed blocks. The prompt-adversary fixtures pass.

Requirement: Per-request token log

The stage job SHALL log, for each request, the token count of each block after the allocation, the tokens that each block lost, and the first request's fixed-part and item-part counts.

Scenario: Log holds counts only

  • WHEN A test builds the stage request and captures the log.
  • THEN The log line holds the tokens of each block after the allocation and the lost tokens of each block. It holds the fixed-part and item-part counts of the first request and no block text.

Requirement: Refine uses the same table

The refine proposal call MUST use the same allocation table, with CONST_ROADMAP_REFINE_CONTEXT_MAX_TOKENS as its cap.

Scenario: Refine cap applies

  • WHEN A test builds the proposal context of refine.
  • THEN The same table names the blocks and applies CONST_ROADMAP_REFINE_CONTEXT_MAX_TOKENS as the cap.

Requirement: Trace gate set after the allocation

The stage context MUST expose the block set after the allocation, so the trace gate of #5466 reads the text that the model reads.

Scenario: Trace gate reads allocated blocks

  • WHEN A test completes the allocation for a request.
  • THEN The returned block set holds each block after its bound is applied.

Requirement: Tokenizer factor

The allocation module MUST hold the billed-tokenizer factor as a named constant next to the table, and MUST count each block with count_tokens.

Scenario: Factor is a named value

  • WHEN A test builds an allocation and patches count_tokens.
  • THEN The module exposes the factor as a named constant next to the table, and count_tokens is called once for each block on the text that the model reads.

Existing Tool Considered

pydantic-ai (https://pypi.org/project/pydantic-ai), MIT, maintained; the alternatives check in the issue body records the verdict in-use and holds what the tool covers and lacks. The proposal uses pydantic-ai for the covered parts. The check found no open tool that gives named per-block bounds or a cut order. The proposal adds the named per-block bounds, the cut order, the rule that keeps the fixed blocks outside the item bound, and the per-block lost-token log that the block lists as missing. Written cost of that code: about 150 lines and 8 tests, about 1.5M tokens and 4 hours to build, keep and fix.

Key questions

  • Which cap bounds the stage context, and where is it defined?: CONST_ROADMAP_STAGE_CONTEXT_MAX_TOKENS at src/devops_cli/config/constants.py:3740 gives the stage cap of 20,480 tokens. src/devops_cli/roadmap/stage/stage.py reads the constant and uses no literal. [sources: src/devops_cli/config/constants.py:3740, src/devops_cli/roadmap/stage/stage.py, tests/roadmap/stage/test_roadmap_stage_window.py]
  • Which cap bounds the refine proposal context, and where is it defined?: CONST_ROADMAP_REFINE_CONTEXT_MAX_TOKENS at src/devops_cli/config/constants.py:3600. src/devops_cli/roadmap/refine/refine.py reads the constant and uses no literal. [sources: src/devops_cli/config/constants.py:3600, src/devops_cli/roadmap/refine/refine.py, tests/roadmap/refine/test_roadmap_refine_proposal.py]
  • Where does the allocation table live?: One table in a new module src/devops_cli/roadmap/context_allocation.py. The stage call and the refine call read it. The module holds the block names, the bounds, the cut-order positions and the tokenizer factor. (best practice) [sources: src/devops_cli/roadmap/refine/refine.py, src/devops_cli/roadmap/stage/stage.py, src/devops_cli/ai/context_budget.py]
    • Chosen: One shared module src/devops_cli/roadmap/context_allocation.py. One table serves the stage call and the refine call, and neither caller couples to the other.
    • Rejected: Table in src/devops_cli/roadmap/stage/stage.py. Refine would import the stage module, and the table would sit in one caller.
    • Rejected: Table in src/devops_cli/ai/context_budget.py. That module counts tokens for every caller, and roadmap block names do not belong there.
  • How are the item-block bounds set?: Each item block gets a fixed bound in the table. The item bounds sum to no more than CONST_ROADMAP_STAGE_CONTEXT_MAX_TOKENS, and the fixed blocks have their own bounds outside it. The first table takes the current section sizes and gives the remainder to the cited code. (best practice) [sources: src/devops_cli/config/constants.py:3740, src/devops_cli/roadmap/refine/refine.py, src/devops_cli/roadmap/stage/stage.py]
    • Chosen: Fixed bound for each block, with a sum at or below the item cap. The cut becomes deterministic, and one offline test can assert it.
    • Rejected: One shared pool with no per-block bound. One large block can take all the room and hide the loss.
    • Rejected: Bounds that the model chooses. No offline test can assert a bound that the model picks.
  • Where do the fixed blocks go in the user message?: First in the user message, before number, title and body. Sort them, add no date, and take them from the staged commit. They stay user data. (best practice) [sources: src/devops_cli/roadmap/stage/stage.py:1348, src/devops_cli/ai/tasks/roadmap_stage_change.md]
    • Chosen: Fixed blocks first in the user message. Items of one commit share these bytes in the prompt prefix, so the provider can cache them.
    • Rejected: Keep the fixed blocks after the body. They stay out of the shared prefix, and the cited code loses text first.
    • Rejected: Move the fixed blocks to the system prompt. They are project data, so the system part must not hold them.
  • How does the stage request carry the context?: As a separate plain-text part of the user message, not as a JSON string. The parts go through StageClient at src/devops_cli/roadmap/stage/stage.py:1348. Change line 3 of src/devops_cli/ai/tasks/roadmap_stage_change.md to say that this part is data too. (best practice) [sources: src/devops_cli/roadmap/stage/stage.py:1348, src/devops_cli/ai/tasks/roadmap_stage_change.md]
    • Chosen: Separate plain-text part of the user message. It removes JSON escaping and keeps the context as user data.
    • Rejected: Keep the JSON string. Escaping adds tokens to the context size.
    • Rejected: Put the context in the system prompt. The context is user data, so it must not go in the system part.
  • Which counter measures a block, and where does the tokenizer factor live?: Count each block with count_tokens from src/devops_cli/ai/context_budget.py on the exact text that the model reads. Hold the billed-tokenizer factor to the o200k count as a named constant next to the table, and assert it in a test. The bounds stay in the o200k count, and the allocation does not apply the factor. (best practice) [sources: src/devops_cli/ai/context_budget.py, src/devops_cli/config/constants.py:3740, src/devops_cli/roadmap/refine/refine.py]
    • Chosen: Named constant next to the table. The table and the counts use one value, and a test can assert it.
    • Rejected: A comment above the table. No test can assert a comment, and the counts could use another value.
    • Rejected: A user setting. The factor is a property of the billed tokenizer, not of one deployment.
  • What does the per-request log record?: The token count of each block after the allocation, the tokens that each block lost, and the first request's fixed-part and item-part counts. Counts only, with no block text. (best practice) [sources: src/devops_cli/roadmap/stage/stage.py, tests/roadmap/stage/test_roadmap_stage_request.py]
    • Chosen: Structured token counts for each block. It gives the missing record of the loss, with no token cost and no risk of block text.
    • Rejected: No per-block log. No record then says how many tokens each block lost.
    • Rejected: Log the block text. It adds token cost and can expose repository text.
  • Does the refine proposal call share the stage allocation table?: Yes. The refine proposal call uses the same table, with CONST_ROADMAP_REFINE_CONTEXT_MAX_TOKENS as its cap. (best practice) [sources: src/devops_cli/roadmap/refine/refine.py, src/devops_cli/config/constants.py:3600, tests/roadmap/refine/test_roadmap_refine_proposal.py]
    • Chosen: Share one table. One table stops the two calls from drifting apart.
    • Rejected: Keep a separate refine allocation. It duplicates the bounds and the cut order.
  • Does a reserved row get a nonzero bound now?: No. Each reserved row keeps bound 0, so this change keeps the current model input. The experiments for the requirement text, the subject manifest, the CONTEXT.md term index and the research references stay with the experiment programme and with feat(roadmap): Add requirement text to the stage context #3246, chore(roadmap): measure the cache share of a stage plan run #4583 and feat(roadmap): link reference implementations in each checked item #4672. A later item sets the bound that they measure. (best practice) [sources: src/devops_cli/config/constants.py:3740, src/devops_cli/roadmap/refine/refine.py, src/devops_cli/roadmap/stage/stage.py]
    • Chosen: Bound 0 until a measurement passes. No evidence sets a safe bound now, and bound 0 changes no model input.
    • Rejected: Set a guessed token bound now. It would change the model input with no measured result.
  • How is the change measured before and after release?: Rebuild the context for the 54 item bodies of the sample and give the kept cited files (baseline 131 of 323) and the first-request tokens by part. Screen with devops roadmap eval stage, paired by case. After release, read 7 days of real Service stage runs for the kept cited files, the lost tokens by block and the cache-read share of the first request (baseline 10,368 of 41,597 for feat(ci): Add ast-grep rules for RLIMIT_NPROC, O_NONBLOCK, os.killpg #1976). (best practice) [sources: src/devops_cli/roadmap/stage/stage_eval_run.py, src/devops_cli/roadmap/stage/stage_eval_compare.py, tests/roadmap/stage/test_roadmap_stage_eval_run.py]
    • Chosen: Eval screen paired by case, then a live check of 7 days. The offline test proves the allocation, and the live check shows the kept files and the cache reads on real runs.
    • Rejected: Offline tests alone. They show no kept-file count and no cache-read share on real runs.
    • Rejected: No measurement. No record then says what the allocation kept and lost.

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

#6521, #5465, #4650

Watched files

A commit on the default branch that changes one of these files starts the next refine round.

  • src/devops_cli/roadmap/refine/refine.py
  • src/devops_cli/roadmap/stage/stage.py

Activity

  1. added
    source/agent
    type/feature
    scope/roadmap
    status/new
    value/high
    effort/high
    on Nov 13, 2026
  2. dan-petty commented on Nov 13, 2026

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

    @devops-cli-service
    Contributor

    Refine cannot set this item to Ready at 9be129e 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.

    The next refine round also looks at the item again when a commit on the default branch changes one of these files: src/devops_cli/roadmap/refine/refine.py.

  5. devops-cli-service commented on Nov 20, 2026

    @devops-cli-service
    Contributor

    The result in the alternatives block of the issue body changed. Before this check: no check. A person decides what the new result means for the item.

    Searches (10)

    Tools considered:

    Searches run:

    • library to allocate prompt token budget across named blocks, results: 5
    • LLM context window budget management python library truncate sections priority, results: 5
    • "context budget" prompt sections truncation python package LLM, results: 5
    • LLMLingua prompt compression token budget library, results: 5
    • LLMLingua github microsoft prompt compression, results: 5
    • tiktoken token counting truncate text to token limit, results: 5
    • langchain prompt template token budget selector, results: 5
    • pydantic-ai message parts user prompt, results: 5
    • github project "prompt budget" allocation blocks LLM context assembly, results: 5
    • LangChain trim_messages max_tokens token counter, results: 5

    Sources:

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

    @devops-cli-service
    Contributor

    Next step: the open items close, or a person replies in a comment or edits the body. See Dependencies and Watched files in the section Proposed design of the body.

  7. dan-petty commented on Nov 20, 2026

    @dan-petty
    OwnerAuthor

    Frozen on 2026-11-20. The stage, implement and fix-checks jobs grade under 60%, so the owner rule asks for a re-architecture before more fixes inside the current design (tracker #6519). This item is incremental work on those jobs, so its Dependencies section now names #6521 first. Stage and implement leave this item while #6521 is open. When the experiments of #6519 decide, a person closes #6521 (this item can then continue) or closes this item as not planned.

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

    @devops-cli-service
    Contributor

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions