Skip to content

fix(roadmap): write each stage and alternatives item when done #4034

Closed

Description

@dan-petty

Problem

The stage row plans every selected item first and writes after. plan_stage (stage.py, _plan_each) plans all items. apply_stage then writes them. It is called from _run_stage_adapter in run.py.

The alternatives row does the same. plan_alternatives checks up to limit items. apply_alternatives writes after the loop (run.py, _run_alternatives_adapter).

A stop of the Service loses all finished work of the round. A stage plan can use up to 1.5 million input tokens (#3880, closed). Refine had the same fault. #3631 (closed) removed it for refine only, with RefineWriter (refine.py).

How the loss happens, from the code:

  • The Service drains for drain_timeout_seconds and then kills the worker. A long plan ends in a kill, and no handler runs.
  • The planner tools read GitHub through the admitted client. RequestBudget.admit raises ServiceStoppingError when the request window is full and the budget is closed (server/service.py). _plan_each does not catch it. The error leaves the loop, and every finished plan is lost.
  • apply_alternatives has no handler for a rate-limit error. A GitHubRateLimitError in check 2 loses the checks after it. Check 1 stays, because it is written before.

Expected

Give the stage and alternatives rows the shape of RefineWriter. Plan one item, write it at once, then plan the next.

Build two adapter classes, one in stage.py and one in alternatives.py. A shared Protocol does not fit. RefineWriter is tied to RefinedItem, RefineFailure and _apply_result.

Stage phase order. Today apply_stage runs five phases for the whole plan: removals, rejections, blocked, ready, over budget. The new order is:

  1. Write the removals and the rejections of items that the round does not select, before the first plan call. They need no model.
  2. For each selected item, plan it and write its result at once.
    • Write a rejection of a selected item after its plan and before its publish. The flag staged of _reject_plan is then true only when the plan is ready.
    • Then write the publish (ready), the return (blocked) or the stop comment (over budget).
  3. Record the failure of an item as soon as the item fails.
    • A stop (ServiceStoppingError, GitHubUnauthenticatedError) ends the round.
    • The round first records the failures of the finished items, as apply_stage does today.

Moved paths. _moved_by_item fetches the default branch once and compares every ready change. In the new shape, the fetch and the comparison run for one item, just before its publish. This is git only. It sends no GitHub request.

Alternatives. Write the body block, the record and the comment of check N before check N+1 starts. A write error that is a rate limit ends the round. The checks that are written stay.

--plan passes no writer and makes no GitHub write. The CLI commands devops roadmap stage --confirm and devops roadmap alternatives --confirm (commands/roadmap.py) use the same writers. Selection and gates do not change.

Acceptance criteria

  • uv run pytest tests/roadmap/stage/test_roadmap_stage_item_writes.py -k killed_between_items - a stub model kills the worker between item 1 and item 2 of 3. The kill is a BaseException that no handler catches. The test finds the draft PR and the record of item 1, and nothing for items 2 and 3.
  • uv run pytest tests/roadmap/stage/test_roadmap_stage_item_writes.py -k service_stopping - an injected ServiceStoppingError in item 2 keeps item 1 written. The failures of the finished items are recorded.
  • uv run pytest tests/roadmap/stage/test_roadmap_stage_item_writes.py -k phase_order - removals and rejections of unselected items are written before the first plan call. A rejection of a selected item is written between its plan and its publish. The moved paths are read once for each item.
  • uv run pytest tests/roadmap/stage/test_roadmap_stage_item_writes.py -k plan_only - --plan makes zero GitHub write calls. The test counts the store writes.
  • uv run pytest tests/roadmap/test_roadmap_alternatives_item_writes.py -k killed_between_items - a kill in item 3 of 5 leaves checks 1 and 2 written.
  • uv run pytest tests/roadmap/test_roadmap_alternatives_item_writes.py -k rate_limit - a GitHubRateLimitError in item 2 keeps the written check of item 1.

Every command above fails on the unchanged code. The pull request cites the fail-to-pass run (devops ci fail-to-pass --base <ref>).

Feasibility

I read these files at origin/main:

  • stage.py: _plan_each, plan_stage, apply_stage, _moved_by_item, _reject_plan.
  • alternatives.py: plan_alternatives, apply_alternatives.
  • run.py: _run_stage_adapter, _run_alternatives_adapter.
  • commands/roadmap.py: the two --confirm paths.
  • server/service.py: RequestBudget.admit.
  • refine.py: RefineWriter.

I did not run the Service or any test. The order "plan all, then write" is visible in the code. _plan_each catches AIClientError, RoadmapStageError and ToolExecutionError only. So ServiceStoppingError leaves the loop.

The kill test must fail on the unchanged code. Item 1 has no draft PR there. No premise is false.

Existing tool considered

RefineWriter.settle (roadmap/refine/refine.py), the pattern of #3631. No library is needed.

Cost

Build: about 80k tokens and 3.5 h (two adapter classes, the phase order, 7 tests). Keep: about 3k tokens per quarter. Fix: about 20k tokens and 1 h per fault.

Related

Part of #3027. #3631 (closed, refine only), #3880 (closed, plan cost), #3793, #3066. Order: this issue comes first in stage.py.

Proposed design

Problem statement

The stage and alternatives rows plan every selected item first and write the results after. A Service stop during the round loses every finished plan, and a stage plan can use up to 1.5 million input tokens. #3631 fixed this loss for refine only, with RefineWriter. This item gives the stage and alternatives rows the same write-as-you-go shape.

Acceptance criteria

Requirement: Stage writes each item at once

The stage row SHALL write the result of each selected item before it plans the next item.

Scenario: Kill between item 1 and item 2

  • WHEN A stub model raises a BaseException that no handler catches between item 1 and item 2 of 3 selected items.
  • THEN The test finds the draft pull request and the record of item 1, and no draft pull request and no record for items 2 and 3.

Requirement: Stage keeps finished items on a stop

The stage row MUST record the result of each finished item before a round-ending error leaves the loop.

Scenario: ServiceStoppingError in item 2

  • WHEN The planner raises ServiceStoppingError for item 2 of 3 selected items.
  • THEN Item 1 stays written, the round ends at item 2, and the store holds the failure record of each item that failed before the stop.

Requirement: Stage phase order

The stage row SHALL write the removals and the rejections of unselected items before the first plan call.

Scenario: Unselected items and a selected rejection

  • WHEN The round selects some items and leaves others out, and a selected item gets a rejection.
  • THEN The store receives the removals and the rejections of the unselected items before the first plan call, the rejection of the selected item between its plan and its publish, and one read of the moved paths for each item.

Requirement: Plan only writes nothing

The stage command with --plan MUST make no GitHub write call.

Scenario: Plan-only run

  • WHEN A person runs devops roadmap stage --plan.
  • THEN The store records zero write calls.

Requirement: Alternatives writes each check at once

The alternatives row SHALL write the body block, the record and the comment of each check before the next check starts.

Scenario: Kill in item 3 of 5

  • WHEN A BaseException that no handler catches kills the worker at check 3 of 5.
  • THEN The store holds the written checks 1 and 2, and nothing for checks 3, 4 and 5.

Requirement: Alternatives keeps written checks on a rate limit

The alternatives row MUST end the round when a write raises GitHubRateLimitError and keep the checks that it already wrote.

Scenario: Rate limit in item 2

  • WHEN A write raises GitHubRateLimitError at check 2 of 5.
  • THEN The round ends and the written check of item 1 stays in the store.

Existing Tool Considered

No alternatives check has run. The need is an in-repo change of the write order in two roadmap rows. No open, maintained library covers that need. The nearest existing code is RefineWriter in src/devops_cli/roadmap/refine/refine.py, which #3631 added for refine only. It is tied to RefinedItem, RefineFailure and _apply_result, so it does not cover stage or alternatives. Written cost: about 80k tokens and 3.5 h to build, about 3k tokens per quarter to keep, and about 20k tokens and 1 h per fault to fix.

Key questions

  • Do the stage and alternatives rows get two adapter classes, a shared Protocol, or an extension of RefineWriter?: Build two adapter classes, one in stage.py and one in alternatives.py. Do not build a shared Protocol and do not extend RefineWriter. (best practice) [sources: src/devops_cli/roadmap/refine/refine.py, src/devops_cli/roadmap/stage/stage.py, src/devops_cli/roadmap/alternatives.py]
    • Chosen: Two adapter classes. Each row writes a different set of artifacts, so a shared type would carry the union of both.
    • Rejected: One shared Protocol for both rows. RefineWriter is tied to RefinedItem, RefineFailure and _apply_result, so the shared type does not fit stage or alternatives.
    • Rejected: Extend RefineWriter to all three rows. Stage writes publish, return, rejection and moved paths, and alternatives writes a body block, a record and a comment. RefineWriter holds none of these.
  • Which order do the stage writes take?: Write the removals and the rejections of unselected items before the first plan call. Then plan each selected item and write its result at once. Write a rejection of a selected item after its plan and before its publish. (best practice) [sources: src/devops_cli/roadmap/stage/stage.py, src/devops_cli/roadmap/run.py]
    • Chosen: Unselected writes first, then plan and write each selected item. A stop then loses only the unfinished items, and the unselected writes need no model.
    • Rejected: Keep the five-phase apply_stage after all plans. A stop loses every finished plan of the round.
    • Rejected: Plan and write every item, including the unselected ones. Removals and rejections need no model, so a plan call for them wastes tokens and time.
  • Which errors end a stage round?: End the round on ServiceStoppingError and GitHubUnauthenticatedError. Record the failure of each other item and continue with the next item. (best practice) [sources: src/devops_cli/roadmap/stage/stage.py, src/devops_cli/server/service.py, src/devops_cli/exceptions/git.py]
    • Chosen: End on ServiceStoppingError and GitHubUnauthenticatedError only. Both make every later request fail, and every other error affects one item.
    • Rejected: End the round on every error. One item's error would drop the finished items of the round.
    • Rejected: End the round on no error. A stop or a refused identity would waste the rest of the request window.
  • Which order do the alternatives writes take, and what does a rate-limit write error do?: Write the body block, the record and the comment of check N before check N+1 starts. End the round on a write error that is a rate limit, and keep the checks that are written. (best practice) [sources: src/devops_cli/roadmap/alternatives.py, src/devops_cli/roadmap/run.py]
    • Chosen: Write check N before check N+1, and end on a rate-limit write error. A kill loses only the check in progress, and a rate limit blocks every later write.
    • Rejected: Keep apply_alternatives after the loop. A kill loses every check of the round.
    • Rejected: Retry the rate-limited write until it succeeds. A rate limit blocks every later write, so the round cannot finish within the request window.
  • How does the --plan path avoid a GitHub write?: --plan passes no writer to the adapter. (best practice) [sources: src/devops_cli/commands/roadmap.py]
    • Chosen: Pass no writer with --plan. The adapter has no write path, so a write cannot occur by accident.
    • Rejected: Pass a no-op writer. A no-op writer hides an accidental write from the test that counts store writes.
    • Rejected: Reuse the writer with a dry-run flag. The flag adds a branch to each adapter for a path that writes nothing.
  • When does the stage row read the moved paths of an item?: Fetch the default branch and compare one item just before its publish. (best practice) [sources: src/devops_cli/roadmap/stage/stage.py]
    • Chosen: Fetch and compare one item just before its publish. The check then uses the branch state at write time, and it sends no GitHub request.
    • Rejected: Fetch once per round and compare every ready change, as today. The comparison then reads a branch state that can differ from the state at write time.
    • Rejected: Skip the moved-path check. A ready change that a person already moved would publish a stale plan.
  • How do the tests simulate a kill between items?: A stub model raises a BaseException between items, in the same process, and the test reads the store after the call. (best practice) [sources: tests/roadmap/stage/test_roadmap_stage_apply.py, tests/roadmap/test_roadmap_alternatives.py]
    • Chosen: A stub model raises BaseException between items. No handler catches it, the test runs offline, and it reads the store after the call.
    • Rejected: Stop the real Service during the test. The test would need a real Service and would not run offline.
    • Rejected: Kill a subprocess with SIGKILL. The test cannot assert the store state after an uncatchable signal in a separate process.

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Activity

  1. added
    source/agent
    type/bug
    scope/roadmap
    status/new
    value/high
    effort/medium
    on Nov 7, 2026
  2. dan-petty commented on Nov 7, 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/bug. The fix prefix of the title gives the type.
    • Scope: scope/roadmap. The (roadmap) scope of the title gives the scope.
    • Priority: P1-High. A Service stop loses all finished work of a round, and a stage plan can use up to 1.5 million input tokens.
    • Value: High. Prevents total loss of a round's finished plans when the Service stops or a rate-limit error occurs.
    • Effort: Medium. Requires two adapter classes, restructured phase order, per-item moved-path fetch, and new tests, but follows the RefineWriter pattern.
    • Related: fix(roadmap): refine writes each item when that item is done #3631, which asks for a different change. Both apply the write-each-item-when-done pattern; #3631 delivered it for refine, this candidate extends it to stage and alternatives.
  3. added
    priority/p1-high
    on Nov 7, 2026
  4. added
    status/ready
    and removed
    status/new
    on Nov 7, 2026
  5. devops-cli-service commented on Nov 7, 2026

    @devops-cli-service
    Contributor

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

  6. added
    owner/agent
    status/in-progress
    status/in-review
    and removed
    status/ready
    status/in-progress
    on Nov 7, 2026
  7. removed
    status/in-review
    owner/agent
    on Nov 7, 2026
  8. devops-cli-service commented on Nov 7, 2026

    @devops-cli-service
    Contributor

    Live since 2026-11-07 22:36 UTC

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/mediumpriority/p1-highscope/roadmapsource/agenttype/bugvalue/high

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions