Skip to content

chore(roadmap): inventory the start context of a step #4385

Open

Description

@dan-petty

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.md is 8,240 bytes, about 2,060 tokens.
  • The step message holds the plan inputs of the group. src/devops_cli/roadmap/implement/implement_step.py counts the instructions and the message in tokens, 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

  1. 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.
  2. The same table for at least five implement runs of different size, and for a stage plan run.
  3. 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?
  4. 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 docs runs 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/main runs on the pull request branch
  • THEN The command prints no path under src/ or tests/.

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

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_tokens at src/devops_cli/ai/context_budget.py:90 counts the tokens of one text with tiktoken. TokenCountError at src/devops_cli/exceptions/ai.py:36 reports 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_file at src/devops_cli/roadmap/step_files.py:252 packs one named file as the step reads it. implement_step.py and stage_change.py call 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_groups at src/devops_cli/roadmap/stage/stage_change.py:3708 counts each group with parse_task_groups and packed_file. It adds CREATED_LINE_TOKENS, 32 tokens, for each line of a file that the group creates, at stage_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 from tools_table() in src/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, beside docs/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 beside docs/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.
  • Which inputs give the five implement sizes and the stage plan?: Five archived change folders of different size under openspec/changes/archive/, each measured with parse_task_groups and packed_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 -c calls and one scratch script under .data/agent/scratch/ that runs a fake model. The script captures the first request, counts each part with count_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_tokens on 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 roadmap subcommand. It adds permanent code, and the issue asks for none.
    • Rejected: No fake model. It misses the tool definitions that the first request carries.
  • 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 gh call, 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

  1. added
    source/agent
    type/chore
    scope/roadmap
    status/new
    value/medium
    effort/medium
    on Nov 10, 2026
  2. dan-petty commented on Nov 10, 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/chore. The chore prefix of the title gives the type.
    • Scope: scope/roadmap. The (roadmap) scope of the title gives the scope.
    • Priority: P2-Medium. It is a diagnostic task that informs future fixes but blocks no user workflow.
    • Value: Medium. It produces a ranked table that guides which token-saving fix to file next.
    • Effort: Medium. It needs several trace runs, per-part token counts, and a short recommendation, but uses existing tooling.
    • Related: feat(roadmap): a step stops at its budgets and records attempts #3568, which asks for a different change. 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.
  3. added
    priority/p2-medium
    on Nov 10, 2026
  4. added
    status/ready
    and removed
    status/new
    on Nov 10, 2026
  5. devops-cli-service commented on Nov 10, 2026

    @devops-cli-service
    Contributor

    Status set to Ready at 84d1570 (see proposed design in issue body).

  6. added
    owner/agent
    on Nov 11, 2026
  7. devops-cli-service commented on Nov 11, 2026

    @devops-cli-service
    Contributor

    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.

  8. dan-petty commented on Nov 13, 2026

    @dan-petty
    OwnerAuthor

    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:

    Block Median p90 Max
    Instructions (template 2.3k, shell tables 0.6k) 2.9k
    AGENTS.md 1.1k
    Routed rule sections 12.9k 18.6k 22.4k
    Change documents 4.2k
    Group 0.4k
    Named-file signatures 5.6k 14.5k
    Total 25.5k 40.5k 50.8k

    The 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.md 43 calls in 20 runs; the change folder 69 calls in 26 runs. read_file paths are not logged, so reads through read_file are 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:

    1. Before any ablation, add measures of rule compliance: hook failures and repair turns for each step (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.
    2. Then an ablation on the implement fixtures. Arm A: the full routed sections. Arm B: sections selected by relevance (or a heading index that the agent reads on demand). Decision rule: arm B only if no compliance measure falls. 14 fixtures detect only large effects.
    3. feat(roadmap): the implement step message states the checkout state #5468 adds the checkout state (at most 300 tokens) after the change documents. It gives no tick list, because tasks.md is already in the message.

    Two experiments for the E2 arms of the pipeline split:

    • Neighbor manifest. Paths only: for each named module, the test files that import it, the conftest.py of that folder, and the message keys that it uses. At most 200 tokens. Evidence: shell grep in tests 253 calls in 29 of 45 runs, in src 302 calls in 29 runs; the grep tool 570 calls (paths not logged); test-folder listings 53 calls in 21 runs; devops ai repomap and devops ai ast in 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.
    • Repair turn: check first. ls .ast-grep/rules, cat sgconfig.yml and 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 last CONST_ROADMAP_IMPLEMENT_LOG_CHARS characters 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.
  9. 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.

  10. added
    status/new
    and removed
    status/ready
    on Nov 26, 2026
  11. 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 and Watched files in the section Proposed design of the body.

  12. added
    status/ready
    and removed
    status/new
    on Nov 30, 2026
  13. devops-cli-service commented on Nov 30, 2026

    @devops-cli-service
    Contributor

    Next step: the stage job writes the plan of the item. See 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/mediumowner/agentpriority/p2-mediumscope/roadmapsource/agentstatus/readytype/chorevalue/medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions