Repository navigation
Description
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 intests/invariants/test_roadmap_implement_record_keys.pynames 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.spendand noimplement.spend_counted. - THEN The record holds
implement.spend_countedas1500and 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_countedand 500 inimplement.spend. - THEN The counter grows by 500 and the record holds
implement.spend_countedas500.
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
ImplementRecordKeywith 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_metricsinsrc/devops_cli/roadmap/implement/implement_submit.pyadds them._reportcalls it after the write that recordsimplement.job_state, and the function readsimplement.spendand callsincrement_counterondevops_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 isincrement_counter, which callsCounter.inc, and the token series is anInstrumentKind.COUNTERread throughrateorincrease. 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 withimplement.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 withimplement.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.
- Chosen: Record key
- How do the counted total and the delta reach the record write and the counter?: A pure helper reads
implement.spendandimplement.spend_countedand returns the total and the delta._reportputsSPEND_COUNTEDin the marks of theservice_releasewrite at line 270, and it passes the delta to_emit_implement_metricsat 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.
- Chosen: A pure helper called by
- What happens to
implement.spend_countedwhen a retry clearsimplement.spend?: Every place that clearsimplement.spendclearsimplement.spend_countedin the same write: the retry path in_startand 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_countedafter 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.spendis less thanimplement.spend_counted?: It counts the wholeimplement.spendtotal once, and it setsimplement.spend_countedto 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.spendonce, because the record has noimplement.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
dan-petty commentedon Nov 9, 2026
Intake placed this item to the backlog: intake puts every new item in the backlog. A person plans the milestones.
fixprefix of the title gives the type.(roadmap)scope of the title gives the scope.The counter reports wrong values to operators but does not break the pipeline or block users.Operators see inflated token totals, which distorts cost tracking and capacity planning.The fix needs a new record key, changes to _report and _emit_implement_metrics, continuation and retry tests, and a docs update.#4070 made implement.spend cumulative across Jobs; the candidate fixes the double-counting that results from that change.devops-cli-service commentedon Nov 9, 2026
Status set to Ready at 816cfe0 (see proposed design in issue body).
devops-cli-service commentedon Nov 9, 2026
Released in v0.100.0: https://github.com/dan-petty/devops-cli/releases/tag/v0.100.0
devops-cli-service commentedon Nov 9, 2026
Live since 2026-11-09 10:07 UTC