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,842 bytes, about 2,200 tokens.
  • The step message holds the plan inputs of the group. 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).

Dependencies

Alternatives check of 2026-11-20: not-applicable.
Class: chore, no search.
Tool: none found.

Proposed design

Problem statement

The first request of an implement step carried 97,821 input tokens at issue #4004. A review on 2026-11-13 measured 25.5k median tokens for 119 groups, before the item text and the tool definitions. The step budget roadmap.implement.step_token_budget counts these tokens. No record splits the start context by part. This item measures each part, marks it fixed or growing, ranks it by tokens saved against risk, and gives one fix issue for each recommendation.

Acceptance criteria

Requirement: Start context document

The change SHALL add docs/ROADMAP_START_CONTEXT.md with a table of the start context by part, one total row and a recommendation section.

Scenario: The docs check passes

  • WHEN uv run devops ci --check --only docs runs on the pull request
  • THEN The check passes and reports no error for docs/ROADMAP_START_CONTEXT.md.

Requirement: Instructions row

The instructions row MUST hold the count that count_tokens gives for src/devops_cli/ai/tasks/roadmap_implement_step.md.

Scenario: The prompt file count matches its row

  • WHEN uv run python -c "from pathlib import Path; from devops_cli.ai.context_budget import count_tokens; print(count_tokens(Path('src/devops_cli/ai/tasks/roadmap_implement_step.md').read_text()))" runs
  • THEN The command prints the token count that the instructions row holds.

Requirement: Tool definitions row

The tool definitions row MUST hold the count that count_tokens gives for the tools table of the prompt.

Scenario: The tools table count matches its row

  • WHEN uv run python -c "from devops_cli.ai.context_budget import count_tokens; from devops_cli.roadmap.tools import tools_table; print(count_tokens(tools_table()))" runs
  • THEN The command prints the token count that the tool definitions row holds.

Requirement: Six input columns

The table MUST give one column for each of five archived change folders of different size and one column for a stage plan input.

Scenario: The document names the six inputs

  • WHEN git grep -n -E 'openspec/changes/archive/|stage_change' -- docs/ROADMAP_START_CONTEXT.md runs
  • THEN The command prints five different archived change paths and the stage plan input.

Requirement: Fixed and growing parts

The document SHALL mark each part as fixed or as growing with the size of the change.

Scenario: The growing mark names only the parts that grow

  • WHEN git grep -n -F 'grows with the change' -- docs/ROADMAP_START_CONTEXT.md runs
  • THEN The command prints a line for the item text, the plan documents, the group text, the named-file signatures and the kept wip: diff, and prints no line for the instructions and the tool definitions.

Requirement: Gap to the measured stop

The document SHALL state the difference between the total row and the 97,821 input tokens of issue #4004.

Scenario: The document names the gap

  • WHEN git grep -n -F '97,821' -- docs/ROADMAP_START_CONTEXT.md runs
  • THEN The command prints the line that gives the difference to the total row.

Requirement: Ranked recommendation

The recommendation SHALL rank each part by the tokens that a change saves against the risk of the change.

Scenario: Each ranked part has a saving and a risk

  • WHEN git grep -n -E '^\| *[0-9]+ *\|' -- docs/ROADMAP_START_CONTEXT.md runs
  • THEN The command prints one row for each ranked part, each with a token saving and a risk, and the largest saving comes first.

Requirement: Fix issue for each recommendation

The document MUST give a Conventional Commit title and a body for each recommendation.

Scenario: Each recommendation has a title of the commit form

  • WHEN git grep -n -E '^### [a-z]+\([a-z-]+\): ' -- docs/ROADMAP_START_CONTEXT.md runs
  • THEN The command prints one title for each recommendation, and each printed title has the form <type>(<scope>): <summary>.

Requirement: No permanent code

The change SHALL add no file under src/ or tests/.

Scenario: The pull request changes only documents

  • WHEN git diff --name-only origin/main runs on the pull request branch
  • THEN The command prints only paths under docs/.

Existing Tool Considered

The alternatives check of 2026-11-20 in the issue body gives not-applicable for this chore and names no tool. tiktoken, through count_tokens in src/devops_cli/ai/context_budget.py, covers the token count of each part. It lacks a split by part, and the one-off measurement of this item gives that split. The change builds no code, so it has no build cost.

Key questions

  • Which function gives the exact token count of a text, offline?: count_tokens in src/devops_cli/ai/context_budget.py gives the exact count with tiktoken. It raises TokenCountError when the encoding is missing, and it never estimates a count. [sources: src/devops_cli/ai/context_budget.py:90]
  • Which parts of the start context does the implement step count today?: StepInputs holds the instructions, the user message and their token counts. The step log keeps no split by part. [sources: src/devops_cli/roadmap/implement/implement_step.py]
  • Which inputs give the five implement sizes of different size and the stage plan input?: Pack five archived change folders of different size from openspec/changes/archive/ with ContextPacker. Take the stage plan input from the stage change builder. (best practice) [sources: src/devops_cli/ai/context_packer.py:395, src/devops_cli/roadmap/step_files.py, src/devops_cli/roadmap/stage/stage_change.py]
    • Chosen: Five archived change folders under openspec/changes/archive/. The repository keeps them, so the measurement runs offline and a reader can repeat it.
    • Rejected: The implement.trace records of production runs. The records are not in the repository at the refined commit, so a step cannot read them.
    • Rejected: Synthetic change folders of set sizes. They do not show the real growth of a plan input.
  • Where does the inventory live?: Write the table and the recommendation in a new document docs/ROADMAP_START_CONTEXT.md. (best practice) [sources: docs/ROADMAP_WORKFLOW.md, AGENTS.md]
    • Chosen: A new document docs/ROADMAP_START_CONTEXT.md. The pull request delivers it, a person reviews it, and the docs check reads it.
    • Rejected: A comment on the issue. The pull request delivers no record, and no review reads the comment.
    • Rejected: A test module that prints the table. The issue forbids permanent code.
  • Which measure reproduces the start context without permanent code?: Give the one-off commands in the document. Each command calls count_tokens on one part, and the reader can run it again. (best practice) [sources: src/devops_cli/ai/context_budget.py:90, src/devops_cli/roadmap/implement/implement_step.py]
    • Chosen: A documented sequence of one-off uv run python -c calls. The step counts the same parts before its first request, so a one-off call gives the same count.
    • Rejected: A new log line in the step. It adds code, and the issue asks for none.
    • Rejected: A new devops roadmap subcommand. It adds permanent code, and the issue asks for none.
  • Which parts are fixed, and which grow with the change?: The instructions and the tool definitions are fixed. The item text, the plan documents, the group text, the named-file signatures and the kept wip: diff grow with the change. [sources: src/devops_cli/ai/tasks/roadmap_implement_step.md, src/devops_cli/roadmap/implement/implement_step.py, CONTEXT.md]
  • Can the implement step file the fix issues itself?: No. The step writes nothing to GitHub, so a person files each fix issue from the title and the body in the document. [sources: src/devops_cli/ai/tasks/roadmap_implement_step.md, src/devops_cli/roadmap/implement/implement_step.py]
  • Which part does the recommendation rank first, and what does a cut risk?: The routed rule sections rank first: the review of 2026-11-13 measured 12.9k median tokens, the largest part. A cut risks rule compliance, so the document ranks a compliance measure before the cut. The item text and the tool definitions rank next. (best practice) [sources: src/devops_cli/roadmap/step_rules.py, CONTEXT.md, src/devops_cli/ai/tasks/roadmap_implement_step.md]
    • Chosen: Rank the routed rule sections first, with a compliance measure before the cut. They are the largest measured part, and the evidence does not show their value.
    • Rejected: Rank the change documents first. They measured 4.2k median tokens, less than the routed sections.
    • Rejected: Cut the routed sections with no measure. No measure would show a fall in rule compliance.
  • For each part, is it needed by the step, can it be a tool call, can it be shorter, or can it be cached?: The change documents and the named files can be read on demand with the step's read tools. The retry text and the kept diff can be shorter. A cache lowers the cost only, because it frees no window room. (best practice) [sources: src/devops_cli/ai/tasks/roadmap_implement_step.md, src/devops_cli/roadmap/tools.py, tests/ai/spend/test_ai_spend_ledger_cache_tokens.py]
    • Chosen: Read the change documents and the named files on demand. It frees window room, and the step has read tools for the files that a group names.
    • Rejected: Send every plan document in the first request. This part grows with the change and keeps window room.
    • Chosen: Shorten the retry text and the kept diff. A stop gives the end of the log, not the whole log.
    • Rejected: Rank a prompt cache as a token saving. A cache lowers the cost, and it frees no window room.
  • What waits this item?: The item waits on chore(roadmap): hold incremental stage and implement work #6521. Incremental work on the stage, implement and fix-checks jobs stops until the re-architecture of refactor(roadmap): re-architect stage and implement on Coder #6519 decides. A person then closes chore(roadmap): hold incremental stage and implement work #6521 or closes this item as not planned. (best practice) [sources: CONTEXT.md]

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

#6521

Watched files

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

  • src/devops_cli/roadmap/implement/implement_step.py
  • src/devops_cli/roadmap/stage/stage.py

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.

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/newtype/chorevalue/medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions