Skip to content

fix(roadmap): count the tokens of each implement Job once #4200

Closed

Description

@dan-petty

Problem

_emit_implement_metrics in src/devops_cli/roadmap/implement/implement_submit.py adds the tokens of implement.spend to the counter devops_cli_service_implement_tokens_total at the end of each Job. Since #4070, implement.spend is the sum of all Jobs of the item. A continuation Job therefore adds the tokens of the earlier Jobs again, and the counter counts them twice or more.

Expected

Each Job adds only its own tokens to the counter.

Acceptance criteria

  • A test runs two Jobs for one item (the first killed, the second a continuation) and checks that the counter grows by the tokens of each Job once.
  • A retry by a person, which clears implement.spend, does not make the counter step back or lose tokens.
  • The record keys stay documented in docs/ROADMAP_WORKFLOW.md, and the invariant test in tests/invariants/test_roadmap_implement_record_keys.py names a reader for any new key.

Existing tool considered

A Prometheus counter cannot subtract. The service needs the spend that it counted at the last report. A record key such as implement.spend_counted, written by _report and cleared with implement.spend, gives the difference. No library covers this.

Related

#4070 (the change that made the spend cumulative), #3069 (the metrics).

Proposed design

Problem statement

_emit_implement_metrics adds the whole of implement.spend to devops_cli_service_implement_tokens_total when a round reports a Job. Since #4070, implement.spend holds the tokens of every Job of the item. A continuation Job therefore adds the tokens of the earlier Jobs again, and the counter counts them twice or more. The change records the token total that the Service counted and adds only the difference, so each Job's tokens reach the counter once.

Acceptance criteria

Requirement: One count for each Job

The Service SHALL add to devops_cli_service_implement_tokens_total only the tokens that the reported Job spent, as the difference between implement.spend and implement.spend_counted.

Scenario: A continuation Job

  • WHEN A test reports the first Job of an item, then the continuation Job of the same item, and the record holds the cumulative spend of both Jobs.
  • THEN The counter grows by the tokens of the first Job once and by the tokens of the second Job once.

Requirement: The counted total is on record

The Service SHALL write the token total that it counted to implement.spend_counted in the same write that records implement.job_state when implement.spend holds a token total.

Scenario: The first report of an item

  • WHEN A round reports a Job of an item whose record holds 1,500 tokens in implement.spend and no implement.spend_counted.
  • THEN The record holds implement.spend_counted as 1500 and the counter grows by 1,500.

Requirement: A retry keeps the counted tokens

The implement job SHALL clear implement.spend_counted in the same write that clears implement.spend on a retry and on a return to New, and the Service SHALL NOT subtract from the counter.

Scenario: A retry by a person

  • WHEN A person comments implement: retry, the next Job starts with both keys cleared, and that Job reports 800 tokens.
  • THEN The counter keeps its earlier total and grows by 800.

Requirement: A total below the counted total counts once

The Service MUST count the whole implement.spend total when that total is less than implement.spend_counted, and MUST set the counted total to it.

Scenario: A cleared spend with a kept counted total

  • WHEN The record holds 2,000 in implement.spend_counted and 500 in implement.spend.
  • THEN The counter grows by 500 and the record holds implement.spend_counted as 500.

Requirement: The new key is documented and read

The change SHALL document implement.spend_counted in docs/ROADMAP_WORKFLOW.md and SHALL name its reader in tests/invariants/test_roadmap_implement_record_keys.py.

Scenario: The invariant test of the record keys

  • WHEN The test compares the members of ImplementRecordKey with its table of readers and the workflow document with the record keys.
  • THEN The tests pass and the table names the reader of implement.spend_counted.

Existing Tool Considered

The open, maintained prometheus_client 0.26.0 (pyproject.toml:73) covers the counter but not the need: its Counter only increments (src/devops_cli/telemetry/metrics.py:88) and has no subtract or set operation, and the token series is an InstrumentKind.COUNTER read through rate or increase (tests/telemetry/test_telemetry_instruments.py:90, :204). No other open, maintained library covers a per-item counted total. The change builds one record key, one pure helper, the two clears in implement_run.py and the tests, about 40 lines. The written cost is about 8,000 tokens to build, about 2,000 tokens for each later fix, and about half a day of elapsed time.

Key questions

  • Where does the code add the tokens of a reported Job to the counter?: _emit_implement_metrics in src/devops_cli/roadmap/implement/implement_submit.py adds them. _report calls it after the write that records implement.job_state, and the function reads implement.spend and calls increment_counter on devops_cli_service_implement_tokens_total. [sources: src/devops_cli/roadmap/implement/implement_submit.py:281, src/devops_cli/roadmap/implement/implement_submit.py:284, src/devops_cli/roadmap/implement/implement_submit.py:304, src/devops_cli/roadmap/implement/implement_submit.py:313]
  • Can a Prometheus counter subtract a value that a client added?: No. The Service's registry wraps prometheus_client.Counter (prometheus-client==0.26.0); its only write is increment_counter, which calls Counter.inc, and the token series is an InstrumentKind.COUNTER read through rate or increase. A counter is monotonic, so the Service cannot subtract the tokens of an earlier Job; it records the total that it counted and adds the difference. [sources: src/devops_cli/telemetry/metrics.py:88, src/devops_cli/telemetry/metrics.py:99, src/devops_cli/telemetry/metrics.py:101, tests/telemetry/test_telemetry_instruments.py:90, tests/telemetry/test_telemetry_instruments.py:204, pyproject.toml:73]
  • Where does the Service keep the token total that it counted at the last report?: In the job record key implement.spend_counted, a decimal integer. It lives with implement.spend, which it tracks. (best practice) [sources: docs/ROADMAP_WORKFLOW.md:135, tests/invariants/test_roadmap_implement_record_keys.py:25, tests/invariants/test_roadmap_implement_record_keys.py:67, src/devops_cli/roadmap/implement/implement_submit.py:270]
    • Chosen: Record key implement.spend_counted. It lives with the spend that it tracks, the record-key table documents it, and one write holds it with implement.job_state.
    • Rejected: A Prometheus gauge of the last spend. The Service holds no series for each item, and a gauge needs one label value for each item number. The code reads no value back from the server.
    • Rejected: A file in the data directory. It adds a second store that a retry and a return to New must clear, and the record already holds implement.spend.
  • How do the counted total and the delta reach the record write and the counter?: A pure helper reads implement.spend and implement.spend_counted and returns the total and the delta. _report puts SPEND_COUNTED in the marks of the service_release write at line 270, and it passes the delta to _emit_implement_metrics at line 281. (best practice) [sources: src/devops_cli/roadmap/implement/implement_submit.py:270, src/devops_cli/roadmap/implement/implement_submit.py:281, src/devops_cli/roadmap/claim.py:176, docs/ROADMAP_WORKFLOW.md:135]
    • Chosen: A pure helper called by _report. One read gives both values, and the total lands in the write that already exists, so no extra request is needed.
    • Rejected: A separate write before the counter. Two writes can fail apart, and the record would then hold a total that the counter did not count.
    • Rejected: Read the counter from the registry. The in-process counter holds no value for each item, and the code cannot subtract from it.
  • What happens to implement.spend_counted when a retry clears implement.spend?: Every place that clears implement.spend clears implement.spend_counted in the same write: the retry path in _start and the return to New in _return_to_new. The new Job then starts its count at zero, and the counter keeps its earlier total. (best practice) [sources: src/devops_cli/roadmap/implement/implement_run.py:672, src/devops_cli/roadmap/implement/implement_run.py:792, docs/ROADMAP_WORKFLOW.md:135]
    • Chosen: Clear both keys together. The new Job starts its spend at zero, and a baseline of zero counts its tokens once.
    • Rejected: Keep implement.spend_counted after a retry. The new Job's spend starts at zero, so a counted total above it hides the first part of the new spend once the new spend passes the old total.
  • What does the Service do when implement.spend is less than implement.spend_counted?: It counts the whole implement.spend total once, and it sets implement.spend_counted to that total. (best practice) [sources: src/devops_cli/roadmap/implement/implement_submit.py:304, src/devops_cli/telemetry/metrics.py:88, docs/ROADMAP_WORKFLOW.md:135]
    • Chosen: Count the whole total once. It keeps every token of the new Job and adds no token twice.
    • Rejected: Add nothing. The tokens of the new Job are lost.
    • Rejected: Add the negative difference. The counter cannot decrease, and the increment would be wrong.
  • What does the first report after the change do for an item whose earlier Jobs the old code already counted?: It counts the whole cumulative implement.spend once, because the record has no implement.spend_counted. An item in flight at deploy can therefore be counted once more; the change accepts that one-time over-count rather than lose the new Job's tokens or block the release. (best practice) [sources: src/devops_cli/roadmap/implement/implement_submit.py:304, src/devops_cli/roadmap/implement/implement_run.py:565, src/devops_cli/roadmap/implement/implement_run.py:579, src/devops_cli/telemetry/metrics.py:88]
    • Chosen: Count the whole total on the first report. The record cannot tell whether the old code counted the earlier spend, so this keeps every token of the new Job and costs at most one over-count for an item in flight.
    • Rejected: Start the counted total at zero without counting. The tokens of the first Job after deploy are lost.
    • Rejected: Backfill from the counter. The counter is aggregated by repo and item class, not by item, and it cannot be read back per item.

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Activity

  1. added
    source/agent
    type/bug
    scope/roadmap
    status/new
    value/medium
    effort/medium
    on Nov 9, 2026
  2. dan-petty commented on Nov 9, 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: P2-Medium. The counter reports wrong values to operators but does not break the pipeline or block users.
    • Value: Medium. Operators see inflated token totals, which distorts cost tracking and capacity planning.
    • Effort: Medium. The fix needs a new record key, changes to _report and _emit_implement_metrics, continuation and retry tests, and a docs update.
    • Related: feat(roadmap): implement keeps phase marks and the spend of a killed Job #4070, which asks for a different change. #4070 made implement.spend cumulative across Jobs; the candidate fixes the double-counting that results from that change.
  3. added
    priority/p2-medium
    on Nov 9, 2026
  4. added
    status/ready
    and removed
    status/new
    on Nov 9, 2026
  5. devops-cli-service commented on Nov 9, 2026

    @devops-cli-service
    Contributor

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

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

    @devops-cli-service
    Contributor
  8. devops-cli-service commented on Nov 9, 2026

    @devops-cli-service
    Contributor

    Live since 2026-11-09 10:07 UTC

  9. removed
    status/in-review
    owner/agent
    on Nov 22, 2026
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/p2-mediumscope/roadmapsource/agenttype/bugvalue/medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions