Repository navigation
Description
Problem
The first model request of an implement step already carries about 98,000 input tokens. An item stopped on this: issue #4004, group 1, first attempt, first request 97,821 tokens (trace from devops roadmap explain 4004 --trace). The step budget roadmap.implement.step_token_budget counts the input tokens of every request, so a start context of 98,000 tokens leaves room for about 80 requests at the default of 8,000,000. We do not know what fills the start context.
What is known
- The instructions file
src/devops_cli/ai/tasks/roadmap_implement_step.mdis 8,240 bytes, about 2,060 tokens. - The step message holds the plan inputs of the group.
src/devops_cli/roadmap/implement/implement_step.pycounts the instructions and the message intokens, but the log keeps no split by part. - The tool definitions of the Coder agent are part of every request. Their size is not measured.
- The stage planner has the same shape: a plan run of feat(roadmap): Stage items whose draft plan fails the check #3387 used 6.5M input tokens in 58 requests, about 112,000 per request.
What to find out
- A table of the start context of an implement step, by part, in tokens: instructions, plan inputs (proposal, design, spec delta, tasks), the group text, kept
wip:diff, retry text, the tool definitions, anything else. - The same table for at least five implement runs of different size, and for a stage plan run.
- For each part: is it needed by the step, can it be a tool call instead (read on demand), can it be shorter, or can it be cached?
- Which part grows with the size of the change, and which is fixed.
How to measure
Count tokens per part with the existing count_tokens. Add no permanent code unless the table needs a log line; if it does, log counts only. Use runs that exist in the trace (implement.trace) and one fresh run with a fake model.
Done when
A table and a short recommendation are in this issue, with the parts ranked by tokens saved against the risk. No code change is part of this issue; file a fix issue for each recommendation.
Related: #4004 (the stop), the step budget (roadmap.implement.step_token_budget).
Alternatives check of 2026-11-30: not-applicable.
Class: chore, no model call.
Tool: none found.
Proposed design
Problem statement
The first model request of an implement step at issue #4004 carried 97,821 input tokens. The step budget counts the input tokens of every request. No record splits that start context by part. This item measures each part in tokens, marks it fixed or growing, ranks each saving against its risk, and gives one fix issue for each recommendation. It adds no code.
Acceptance criteria
Requirement: Start-context record
The change SHALL add docs/roadmap/start-context.md with a table of each start-context part, its tokens, its fixed-or-growing mark, its need, its tool-call option, its shorter option, its cache option, a total row, and recommendations ranked by tokens saved against risk.
Scenario: The docs check passes
- WHEN
uv run devops ci --check --only docsruns on the pull request - THEN The check passes with no error for
docs/roadmap/start-context.md.
Requirement: Table contents
The table SHALL hold at least five implement runs of different size, one stage plan run, a total row, and a section of recommendations ranked by tokens saved against risk.
Scenario: The table holds the required rows
- WHEN
uv run python -c "from pathlib import Path; text=Path('docs/roadmap/start-context.md').read_text(); lines=text.splitlines(); rows=[line for line in lines if line.lower().startswith('| implement')]; assert len(rows) >= 5; lower=text.lower(); assert 'stage plan' in lower; assert 'total' in lower; assert 'ranked recommendations' in lower; assert 'tokens saved' in lower; assert 'risk' in lower; print('ok')"runs - THEN The command prints
ok.
Requirement: Instructions row
The instructions row SHALL hold the token count that count_tokens gives for the rendered step prompt at the measured commit.
Scenario: The prompt count matches its row
- WHEN
uv run python -c "from pathlib import Path; from devops_cli.ai.context_budget import count_tokens; from devops_cli.roadmap.implement.implement_step import step_instructions; text=Path('docs/roadmap/start-context.md').read_text(); lines=[line for line in text.splitlines() if line.startswith('| Instructions |')]; assert lines; cells=[cell.strip() for cell in lines[0].strip('|').split('|')]; assert int(cells[1]) == count_tokens(step_instructions()); print('ok')"runs at the measured commit - THEN The command prints
ok.
Requirement: Tool definitions row
The table SHALL hold a row for the tool definitions of the Coder agent, with a positive token count.
Scenario: The tool definitions row is present
- WHEN
uv run python -c "from pathlib import Path; text=Path('docs/roadmap/start-context.md').read_text(); lines=[line for line in text.splitlines() if line.startswith('| Tool definitions |')]; assert lines; cells=[cell.strip() for cell in lines[0].strip('|').split('|')]; assert int(cells[1]) > 0; print('ok')"runs - THEN The command prints
ok.
Requirement: Measurement commands
The page SHALL record the one-off commands and the scratch fake-model script that give the counts.
Scenario: The page records the measurement commands
- WHEN
uv run python -c "from pathlib import Path; text=Path('docs/roadmap/start-context.md').read_text().lower(); assert 'count_tokens' in text; assert 'fake model' in text; assert 'uv run python' in text; print('ok')"runs - THEN The command prints
ok.
Requirement: No permanent code
The change SHALL add no file under src/ or tests/.
Scenario: The branch changes no source or test file
- WHEN
git diff --name-only origin/mainruns on the pull request branch - THEN The command prints no path under
src/ortests/.
Requirement: Pending a person after the merge
A person SHALL post the table and the recommendations to issue #4385 and file one fix issue for each recommendation after the merge.
Scenario: The issue holds the table and each fix issue exists
- WHEN A person has merged the pull request and read the recommendations
- THEN Issue chore(roadmap): inventory the start context of a step #4385 holds the table, and one open issue exists for each recommendation.
Existing Tool Considered
The alternatives check of 2026-11-30 in the issue body gives not-applicable for this chore and names no tool. The measurement uses the existing count_tokens wrapper of tiktoken at src/devops_cli/ai/context_budget.py:90. That wrapper lacks a split by part, so the documented one-off commands and the fake-model capture give the split. The change builds no code, so it has no build cost.
Key questions
- Which function gives the exact token count of one text?:
count_tokensatsrc/devops_cli/ai/context_budget.py:90counts the tokens of one text with tiktoken.TokenCountErroratsrc/devops_cli/exceptions/ai.py:36reports a missing encoding. [sources: src/devops_cli/ai/context_budget.py:90, src/devops_cli/exceptions/ai.py:36] - Which parts does the start context of an implement step hold?: The rendered instructions hold the step prompt and the
{{tools}}table. The user message holds the routed rules sections, the item, the change documents, the group, the tasks file, the stop of an earlier Job, the fix log and the packed files. The first request also carries the tool definitions of the Coder agent. [sources: src/devops_cli/ai/tasks/roadmap_implement_step.md, src/devops_cli/roadmap/implement/implement_step.py] - Which code packs a named file of a group?:
packed_fileatsrc/devops_cli/roadmap/step_files.py:252packs one named file as the step reads it.implement_step.pyandstage_change.pycall it. [sources: src/devops_cli/roadmap/step_files.py:252, src/devops_cli/roadmap/implement/implement_step.py, src/devops_cli/roadmap/stage/stage_change.py] - Which code measures a task group today?:
measure_groupsatsrc/devops_cli/roadmap/stage/stage_change.py:3708counts each group withparse_task_groupsandpacked_file. It addsCREATED_LINE_TOKENS, 32 tokens, for each line of a file that the group creates, atstage_change.py:144. [sources: src/devops_cli/roadmap/stage/stage_change.py:3708, src/devops_cli/roadmap/stage/stage_change.py:144, src/devops_cli/roadmap/step_files.py:252] - Where do the tool definitions of a step come from?: The
{{tools}}table comes fromtools_table()insrc/devops_cli/roadmap/tools.py. The Coder agent sends the tool definitions that pydantic-ai builds from the same toolset. A fake model captures those definitions in the first request. [sources: src/devops_cli/roadmap/tools.py, src/devops_cli/roadmap/implement/implement_step.py, src/devops_cli/ai/tasks/roadmap_implement_step.md] - Where does the record live?: A new page
docs/roadmap/start-context.md, besidedocs/roadmap/job-success.md. A person posts the same table to issue chore(roadmap): inventory the start context of a step #4385 after the merge. (best practice) [sources: docs/roadmap/job-success.md, AGENTS.md]- Chosen: A new page
docs/roadmap/start-context.md. The pull request delivers a reviewable record besidedocs/roadmap/job-success.md. - Rejected: A comment on issue chore(roadmap): inventory the start context of a step #4385 only. The pull request delivers no record, and no review reads the comment.
- Rejected: A test module that prints the table. It adds permanent code, and the issue asks for none.
- Chosen: A new page
- Which inputs give the five implement sizes and the stage plan?: Five archived change folders of different size under
openspec/changes/archive/, each measured withparse_task_groupsandpacked_file, plus one fresh fake-model run of an implement step. A person adds the counts of the production trace runs. (best practice) [sources: CONTEXT.md, src/devops_cli/roadmap/step_files.py:252, src/devops_cli/roadmap/implement/implement_step.py, src/devops_cli/roadmap/implement/implement_trace.py]- Chosen: Five archived change folders and one fake-model run. The repository keeps the folders, so a reader repeats the count offline. The fake model captures the tool definitions.
- Rejected: Synthetic change folders of set sizes. They do not show the real growth of a plan input.
- Rejected: The production trace runs only. The trace is not in the checkout, so the pull request cannot show it. A person adds those counts.
- Which measure reproduces the start context with no permanent code?: A documented sequence of one-off
uv run python -ccalls and one scratch script under.data/agent/scratch/that runs a fake model. The script captures the first request, counts each part withcount_tokens, and writes the table. The scratch file is not committed. (best practice) [sources: src/devops_cli/ai/context_budget.py:90, src/devops_cli/roadmap/implement/implement_step.py, AGENTS.md]- Chosen: One-off commands and a scratch fake-model script. They call the same
count_tokenson the same parts, and the scratch file is not committed. - Rejected: A new log line in the step. It adds permanent code, and the issue asks for none.
- Rejected: A new
devops roadmapsubcommand. It adds permanent code, and the issue asks for none. - Rejected: No fake model. It misses the tool definitions that the first request carries.
- Chosen: One-off commands and a scratch fake-model script. They call the same
- Which parts are fixed, and which grow with the change?: The rendered instructions and the tool definitions are fixed for one toolset. The routed rules sections, the item, the change documents, the group text, the named files, the stop text and the fix log grow with the item or the change. (best practice) [sources: src/devops_cli/ai/tasks/roadmap_implement_step.md, src/devops_cli/roadmap/step_files.py:252, src/devops_cli/roadmap/implement/implement_step.py]
- Chosen: Instructions and tool definitions fixed; the rest grow. The prompt file and the toolset change with no item.
- Rejected: Every part fixed. The plan inputs and the named files come from the change.
- Rejected: The tool definitions grow. The tools table of one toolset is the same in every step.
- Can the implement step post the table to the issue and file the fix issues?: No. The step makes no push, opens no pull request and makes no
devops ghcall, and its shell has no GitHub credential. A person posts the table and files one issue for each recommendation after the merge. [sources: src/devops_cli/ai/tasks/roadmap_implement_step.md, AGENTS.md] - How does the measurement count the tool definitions?: A scratch script runs an implement step with a fake model and captures the first model request. It serializes the tool definitions and counts them with
count_tokens. The docs page records the count and the command. (best practice) [sources: src/devops_cli/roadmap/implement/implement_step.py, src/devops_cli/ai/context_budget.py:90]- Chosen: Fake-model capture of the first request. It measures the actual request, tool definitions included.
- Rejected: Count the
{{tools}}table only. The table is not the full tool definitions. - Rejected: Estimate from the source files. An estimate gives no exact count.
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 is a diagnostic task that informs future fixes but blocks no user workflow.It produces a ranked table that guides which token-saving fix to file next.It needs several trace runs, per-part token counts, and a short recommendation, but uses existing tooling.Both concern the implement step token budget, but #3568 sets the budget limit while the candidate measures what fills the start context under that limit.devops-cli-service commentedon Nov 10, 2026
Status set to Ready at 84d1570 (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
Start-context table and a recommendation, from the start-context review of the roadmap jobs (2026-11-13). Measured at origin/main for 119 groups of 39 items, o200k tokens, before the issue text and the tool definitions:
AGENTS.mdThe step refuses a start over the window minus 40k. The cache share of the runner Jobs is about 98%, so the cost of the start context is window room and attention, not money.
Observations, 7 days to 2026-11-13, 45 runs: shell reads of rule documents in 2 runs;
tasks.md43 calls in 20 runs; the change folder 69 calls in 26 runs.read_filepaths are not logged, so reads throughread_fileare not visible (#5460 adds a path class). The evidence shows neither a low nor a high value of the routed sections: the agent does not need to look for rules that its message already holds.Recommendation:
implement_hooks), failures of the scoped static rows, and review findings by rule on delivery pull requests (E4 of the pipeline split). The six eval properties (tests_kept,diff_scope,no_scratch,tasks_ticked,tests_pass,verify_passes) do not measure rule compliance.tasks.mdis already in the message.Two experiments for the E2 arms of the pipeline split:
conftest.pyof that folder, and the message keys that it uses. At most 200 tokens. Evidence: shellgrepin tests 253 calls in 29 of 45 runs, insrc302 calls in 29 runs; thegreptool 570 calls (paths not logged); test-folder listings 53 calls in 21 runs;devops ai repomapanddevops ai astin 0 of 45 runs. The search texts are masked, so the share of greps that look for neighbors is not known. Primary measure: grep-in-tests and test-folder listing calls for each step (needs feat(roadmap): run records give the run, step and path class of tool calls #5460). Decision rule: fewer such calls and no fall in the six properties.ls .ast-grep/rules,cat sgconfig.ymland similar: 50 calls in 18 of 45 runs, and 6 calls in 1 of 10 recent runs, after a failed ast-grep hook. The repair turn gets the lastCONST_ROADMAP_IMPLEMENT_LOG_CHARScharacters of the failed-check log. Check one recorded repair turn. If the cut removes the rule id, message and note, give them in the repair turn only, not in the start context.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 and Watched files 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.