Repository navigation
Description
Problem
The context of a step grows with every tool call, and each request sends the whole context again. In the first attempt of group 1 of #4004 the input per request grew from 97,821 to 140,903 tokens in about 45 requests. In a stage plan run of #3387, tool results were about 55% of the context at the end (about 105,000 of 190,000 tokens). Only 3% of the lines that the planner read were read twice, so a guard against repeated reads saves almost nothing. The cost of the growth is high: the whole item used 22.6M input tokens in 211 requests.
What to find out
- The share of the context by kind at the end of a run, for implement and for stage: system and task text, tool definitions, model replies, tool results by tool (
read_file,grep,list_files, the shell tool, others). - The size distribution of tool results (median, 90th percentile, largest) and how many the cap of the tool results cuts (
ToolOutputLimits, withCONST_ROADMAP_IMPLEMENT_TOOL_MAX_CHARSin implement). - Whether old tool results can leave the history safely: test the open options first (rule 3), for example the history processors of
pydantic_aiand the context controls ofpydantic_ai_harness, before any new code.Coderalready givesClearToolResultsandWarnNearLimits, and implement (implement_step.py) and stage (stage_agent.py) set their window to the window of the task. State what these controls do in the pinned versions and which other options exist. - For each option: the effect on the number of tokens, the risk that the agent loses a fact it needs (re-reads, repeated mistakes, failed checks), and a way to measure that risk offline (the golden stage eval, the implement eval).
- Whether a smaller cut for large tool results (for example a value below
CONST_ROADMAP_IMPLEMENT_TOOL_MAX_CHARS, which is 40,000 characters, with a clear notice) is better than a compaction of old results.
Done when
A table of the context share, a list of the open options with evidence from their source or documents, and a ranked recommendation are in this issue. No code change is part of this issue; file a fix issue for each recommendation.
Related: the start context inventory (#4385).
Alternatives check of 2026-11-30: not-applicable.
Class: chore, no model call.
Tool: none found.
Proposed design
Problem statement
The context of a step grows with every tool call, and each request sends the whole context again. In one group 1 run of #4004 the input per request rose from 97,821 to 140,903 tokens in about 45 requests, and in a stage plan run of #3387 tool results were about 55 percent of the context at the end. Only 3 percent of the lines that the planner read were read twice, so a guard against repeated reads saves almost nothing. The whole item used 22.6 million input tokens in 211 requests. This item measures the context share by kind and the tool result sizes for one implement run and one stage plan run, lists the open options that drop or compact old results with evidence from their source, and records a ranked recommendation with an offline risk check. It changes no code.
Acceptance criteria
Requirement: Documentation-only change folder
The change SHALL add the OpenSpec change folder openspec/changes/4386-measure-how-context-grows-during-a-step/ with proposal.md, design.md, tasks.md and .openspec.yaml.
Scenario: OpenSpec accepts the change folder
- WHEN
uv run devops openspec checkruns on the branch. - THEN The command exits 0, and the folder holds the four files and
.openspec.yamlsetsskip_specs: true.
Requirement: Context share by kind
The design note SHALL give the input token share by kind at the end of one implement run and one stage plan run.
Scenario: The shares add to 100 percent
- WHEN A reviewer reads the design note against the recorded run traces.
- THEN The note gives one share for system and task text, tool definitions, model replies, and tool results by tool, and the shares add to 100 percent for each run.
Requirement: Tool result size distribution
The design note SHALL give the median, 90th percentile and largest tool result size, and the count each cap cuts, for each run.
Scenario: Known result sizes
- WHEN A reviewer reads the size table and the recorded tool calls of the two runs.
- THEN The table gives the median, the 90th percentile and the largest size, the count cut at the implement cap of 40,000 characters and at each other cap that a job sets, and each cap value with its source.
Requirement: Options with a source
The design note SHALL list each open option with what it does in the pinned versions and a source.
Scenario: Each option names a source
- WHEN
uv run devops openspec checkruns and a reviewer reads the options section. - THEN The command exits 0, and each row names a repository path or a URL and the pinned version from
pyproject.toml.
Requirement: Ranked recommendation with a risk check
The design note SHALL rank the options and give each its token effect, its risk to needed facts, and an offline risk check.
Scenario: Ranked options with a risk check
- WHEN A reviewer reads the recommendation of the design note.
- THEN The options are ranked, and each names the golden stage eval or the implement eval as its risk check.
Requirement: No code and unchanged caps
The change MUST add documents only and MUST NOT change the 40,000-character cap of the implement step or any cap of the stage planner.
Scenario: The diff holds no source change
- WHEN
git diff --name-only origin/main...HEADanduv run devops openspec checkrun. - THEN The diff lists no path under
src/, the command exits 0, and the change folder.openspec.yamlsetsskip_specs: true.
Requirement: One fix issue per recommendation
The change's tasks.md SHALL list one follow-up task for each recommendation.
Scenario: A recommendation without a follow-up
- WHEN A reviewer reads
tasks.mdand the ranked recommendation. - THEN One task exists for each recommendation, and each task names the recommendation and the fix issue title that it files after the merge.
Requirement: Pending a person after the merge
After the change merges, a person SHALL post the report in issue #4386 and file one fix issue for each recommendation.
Scenario: The issue holds the report and the fix issues
- WHEN The change merges and a person posts the report and files the fix issues.
- THEN Issue chore(roadmap): measure how context grows during a step #4386 holds the table, the options and the ranked recommendation, and each recommendation has one open issue that links this issue and the recommendation.
Existing Tool Considered
The alternatives check in the issue body records not-applicable and Tool: none found. The analysis assesses the open, maintained pydantic_ai 2.77.0 and pydantic_ai_harness 0.77.0 as the options to drop or compact old results. Neither reports the token share by kind or the tool result size distribution. The change builds no measurement code, so it pays no build cost.
Key questions
- How does one pull request deliver an item whose result is an analysis?: The pull request adds a documentation-only OpenSpec change folder. Its
design.mdholds the measured table, the options with sources and the ranked recommendation..openspec.yamlsetsskip_specs: true. The repeatable check isuv run devops openspec check. (best practice) [sources: docs/adr/0010-openspec-is-the-spec-and-change-proposal-layer.md, docs/ROADMAP_WORKFLOW.md, openspec/config.yaml]- Chosen: Documentation-only change folder with a design note. ADR 0010 makes every roadmap item one change folder, and a document-only change sets
skip_specs: true. - Rejected: Analysis in the issue only. The roadmap workflow requires a change folder, so an issue comment leaves no deliverable to check.
- Rejected: A committed report command and its tests. The body says no code change is part of this issue, and a document gets no test.
- Chosen: Documentation-only change folder with a design note. ADR 0010 makes every roadmap item one change folder, and a document-only change sets
- Which library drops or compacts old tool results?: The pinned
pydantic_ai2.77.0 provides history processors that rewrite the message history before each request. The pinnedpydantic_ai_harness0.77.0 providesClearToolResults,WarnNearLimitsandToolOutputLimits.ClearToolResultsclears old tool results against the context window.WarnNearLimitswarns near that window.ToolOutputLimitscuts a result at a character cap. The implement step uses the output cap withCONST_ROADMAP_IMPLEMENT_TOOL_MAX_CHARS. The implement step and the stage agent useClearToolResultsand set the window to the task window. (best practice) [sources: pyproject.toml, src/devops_cli/roadmap/implement/implement_step.py, src/devops_cli/roadmap/stage/stage_agent.py, src/devops_cli/config/constants.py:4039, src/devops_cli/ai/knowledge_base/devops_cli/libraries/pydantic_ai_harness.md, src/devops_cli/ai/knowledge_base/devops_cli/libraries/pydantic_ai.md]- Chosen: pydantic_ai history processors and pydantic_ai_harness compaction. Both are open, maintained and pinned. The harness compaction module is already imported and tuned by the step.
- Rejected: Own compaction in
devops_cli.ai.harness. The body asks to test the open options before any new code, and an own layer adds code to keep and test. - Rejected: No compaction, only a smaller character cap. It cuts each result but keeps every result in the history, so the context still grows with each call.
- Which recorded data does the measurement read?: The measurement reads the recorded run traces and the eval records. It names the source of each figure. When a trace does not hold a figure, the note says so and names the source that does. It does not add a code change. [sources: src/devops_cli/roadmap/agent_run.py:424, src/devops_cli/roadmap/implement/implement_step.py, src/devops_cli/roadmap/stage/stage_agent.py, src/devops_cli/roadmap/implement/implement_eval.py, src/devops_cli/roadmap/stage/stage_eval_gates.py]
- How is the risk of an option measured offline?: The golden stage replay judges the recorded plans with no model and no git. The implement eval checks the four diff properties on recorded results. Each recommendation names one of these offline checks. It states that the live
devops roadmap eval stagerun is the check for the rest. (best practice) [sources: src/devops_cli/commands/roadmap_eval_stage.py, src/devops_cli/roadmap/stage/stage_eval_gates.py, src/devops_cli/roadmap/implement/implement_eval.py, tests/roadmap/stage/test_roadmap_stage_eval_gates.py, tests/roadmap/implement/test_roadmap_implement_eval_properties.py]- Chosen: Golden stage replay and implement eval recorded results. Both run offline on recorded plans and results, so a person can run them again with no model call.
- Rejected: The live
devops roadmap eval stagerun only. It is a model run, so it is not an offline check and it costs the tokens this item measures. - Rejected: No risk check. The item asks for the risk of each option and a way to measure it offline.
- Does the change set a smaller character cap for large tool results?: No. The change keeps the 40,000-character cap of the implement step and any cap of the stage planner. The note reports the count that each cap cuts. A smaller cap or a compaction is a recommendation for a follow-up issue. (best practice) [sources: src/devops_cli/config/constants.py:4039, src/devops_cli/roadmap/implement/implement_step.py, src/devops_cli/roadmap/stage/stage_agent.py]
- Chosen: Keep both caps and measure. The item has no evidence yet for a smaller cap, and the body forbids a code change in this item.
- Rejected: Set a smaller cap now. It changes the planner's input before the measurement exists, and it needs a code change that the body leaves to a fix issue.
- Rejected: Remove the caps. A large result would raise the cost of every later request, which is the growth this item measures.
- Which groups does the report divide the tool results into?: The report uses the tool names in the recorded calls of each job, with one
othergroup. It does not fix a list before it reads the traces. (best practice) [sources: src/devops_cli/roadmap/implement/implement_step.py, src/devops_cli/roadmap/stage/stage_agent.py]- Chosen: Top tools of each job plus
other. The two jobs run different tools, so one fixed list would hide the tools that dominate each job. - Rejected: One fixed list for both jobs.
grepand the shell exist only in the implement step, and the stage planner has its own tools. - Rejected: One group for all tool results. It would not show which tool causes the growth, which is the question of the item.
- Chosen: Top tools of each job plus
- How are the fix issues for the recommendations delivered?: The change's
tasks.mdlists one pending person task for each recommendation. After the merge, a person posts the report in issue chore(roadmap): measure how context grows during a step #4386 and files one new issue for each recommendation. Each issue names this issue and the recommendation. (best practice) [sources: docs/ROADMAP_WORKFLOW.md, docs/adr/0010-openspec-is-the-spec-and-change-proposal-layer.md]- Chosen: One issue per recommendation. Each recommendation is built and closed on its own, and the body asks for a fix issue for each one.
- Rejected: One issue for all recommendations. A single issue cannot be closed when one recommendation ships and another waits, and it hides the ranking.
- Rejected: No follow-up issue. The body says a fix issue follows each recommendation, so the analysis would change nothing.
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.
choreprefix of the title gives the type.(roadmap)scope of the title gives the scope.It produces the data needed to choose a context-management fix, but it is research, not a user-facing change.It delivers a ranked recommendation that guides the next fix issues for context growth.It requires running experiments, collecting token distributions, testing pydantic_ai options, and writing a table, but no code change.Both measure step context, but #4385 inventories the first request while this issue measures growth and end-of-run share by kind.devops-cli-service commentedon Nov 10, 2026
Status set to Ready at f29d202 (see proposed design in issue body).
devops-cli-service commentedon Nov 11, 2026
Alternatives check: not-applicable
Class: chore. The check makes no search and no model call for an item of this class.
Tool: none found
Checked on 2026-11-11. A person decides what this means for the item. The check changes no label and closes no issue.
dan-petty commentedon Nov 13, 2026
Two experiments for the stage planner that use this study, from the start-context review of the roadmap jobs (2026-11-13).
Evidence (stage comments of 54 items, 2026-11-06 to 2026-11-13, 8,213 calls):
read_file: 3,290 calls for 991 distinct item and path pairs (70% repeats).previousandproblem(stage.py_Refused.returned). The new run has no tool results and starts at call 1.stage reply refusedlines over 47 items (max 20, for ci(image): move the image checks to one path-filtered workflow #1858). Item attempts reach 14-17M tokens.Experiment A: a gate return continues the plan run (pydantic-ai
message_history)._trace_factsalready joins the plan runs, so the gate accepts these reads.RunTrace.combinedcounts them twice. A trim that clears tool answers lets the gate accept claims about text that the model no longer has. Needs the run index of feat(roadmap): run records give the run, step and path class of tool calls #5460. A review read manifest is a separate arm, only after fix(roadmap): a review finding must name a part of the plan to block it #5410 and fix(roadmap): only blocker and major review findings block a plan #5411 merge.Experiment B: a pointer answer for a repeat call inside one run (stage toolbox only; it reads one fixed commit).
git statusrepeats follow edits, andClearToolResultsremoves old answers.dan-petty commentedon Nov 20, 2026
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.
devops-cli-service commentedon Nov 26, 2026
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.
devops-cli-service commentedon Nov 30, 2026
Next step: the stage job writes the plan of the item. See the section Proposed design of the body.