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,search_code,grep, shell, others). - The size distribution of tool results (median, 90th percentile, largest) and how many are cut by the 8,000-character cap.
- Whether old tool results can leave the history safely: test the open options first (rule 3), for example the history processors and context management features of
pydantic_aiandpydantic_ai_harness, before any new code. State which options exist in the pinned versions and what each does. - 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 4,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 call budget and notice (#4346), whole-line cut (#4341), the start context inventory issue.
Dependencies
- chore(roadmap): hold incremental stage and implement work #6521 (hold: incremental work on the stage, implement and fix-checks jobs waits for the re-architecture)
Alternatives check of 2026-11-20: not-applicable.
Class: chore, no search.
Tool: none found.
Proposed design
Problem statement
Do not start this item before #6521 closes.
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. In a stage plan run of #3387, tool results were about 55 percent of the context at the end. 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. It lists the open options that drop or compact old results with evidence from their source. It 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 a design.md that holds the report, the options and the recommendation.
Scenario: OpenSpec accepts the change folder
- WHEN
uv run devops openspec checkruns on the branch. - THEN The command exits 0, and the change folder holds
proposal.md,design.mdandtasks.md.
Requirement: Context share by kind
The design note SHALL give the input token share by kind at the end of each 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. It gives the count cut at 8,000 characters in the stage run and at 40,000 characters in the implement run.
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 8,000-character cap of the stage planner or the 40,000-character cap of the implement step.
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 fix issue 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 that it files after the merge.
Requirement: Pending a person after the merge
After the change merges, a person SHALL post the summary in issue #4386 and file one fix issue for each recommendation.
Scenario: The issue holds the summary and the fix issues
- WHEN The change merges and a person posts the summary and files the fix issues.
- THEN Issue chore(roadmap): measure how context grows during a step #4386 holds the summary, 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. The change is a measurement document and builds no code. It has no build cost. It reads the pinned pydantic_ai and pydantic_ai_harness compaction options as the subject of the analysis. No open tool reports the token share by kind or the tool result size distribution.
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, docs/TESTING.md, openspec/changes/archive/2026-11-07-3242-design-a-sandboxed-read-only-run/proposal.md, openspec/changes/archive/2026-11-07-3242-design-a-sandboxed-read-only-run/.openspec.yaml, openspec/changes/archive/2026-11-07-3242-design-a-sandboxed-read-only-run/tasks.md]- 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. Readiness requires a pull request to add or modify a file under its 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.59.0 providesProcessHistory, which rewrites the message history before each request. The pinnedpydantic_ai_harness0.59.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 already uses the three harness capabilities. (best practice) [sources: pyproject.toml, src/devops_cli/roadmap/implement/implement_step.py, src/devops_cli/roadmap/stage/stage_agent.py, 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, CHANGELOG.md]- Chosen: pydantic_ai ProcessHistory and pydantic_ai_harness compaction. Both are open, maintained and pinned, and the harness compaction module is already imported and tuned by the step.
- Rejected: Own compaction in
devops_cli.ai.harness. The project deleted it in refactor(ai): delete the unwired harness port and the capability clones nothing imports #1836 because it copied what the libraries ship. - 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 implement trace keeps each model call's
tokens_inandtokens_outand each tool call's name and outcome. It holds no argument and no output. The stageRunTracekeeps each tool call's answer size in characters and the last request's input tokens. The per-request growth is read from these records. The size distribution and the share by kind are measured on the run from the stage character counts and the implement message history. The note names the source of each figure. [sources: src/devops_cli/roadmap/implement/implement_trace.py, src/devops_cli/roadmap/agent_run.py, src/devops_cli/roadmap/stage/stage_plan_record.py, docs/ROADMAP_WORKFLOW.md, openspec/changes/archive/2026-11-10-4341-a-tool-answer-cut-at-the/proposal.md] - 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 8,000-character cap of the stage planner and the 40,000-character cap of the implement step. The note reports the count that each cap cuts. A 4,000-character cap or a compaction is a recommendation for a follow-up issue. (best practice) [sources: src/devops_cli/config/constants.py, src/devops_cli/roadmap/stage/stage_tools.py, src/devops_cli/roadmap/implement/implement_step.py, openspec/changes/archive/2026-11-10-4341-a-tool-answer-cut-at-the/proposal.md]
- Chosen: Keep both caps and measure. The item has no evidence yet for a smaller cap, the stage cap already has its notice from fix(roadmap): a tool answer cut at the character limit has no notice #4341, and the body forbids a code change in this item.
- Rejected: Set the stage cap to 4,000 characters 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 top tools of each job plus one
othergroup. The stage planner groupsread_file,search_code,list_files,git_show,referencesand other. The implement step groupsread_file,grep,shell,list_files,edit_file,write_fileand other. (best practice) [sources: docs/roadmap/stage.md, src/devops_cli/roadmap/stage/stage_tools.py, src/devops_cli/roadmap/implement/implement_step.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, andsearch_codeonly in the stage planner. - 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 summary 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: openspec/changes/archive/2026-11-07-3242-design-a-sandboxed-read-only-run/proposal.md, openspec/changes/archive/2026-11-07-3242-design-a-sandboxed-read-only-run/tasks.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.
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.