Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/ROADMAP_WORKFLOW.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ These are all the keys of the job record, by the job that writes them:
| Implement | `implement.group`, `implement.sha`, `implement.branch`, `implement.pr` | The last group that passed, the last commit, the branch and the pull request |
| Implement | `implement.repair_rounds` | The count of repair rounds that the steps of the run spent on failed static checks and scoped checks |
| Implement | `implement.stopped`, `implement.failure`, `implement.spend`, `implement.trace` | The step that stopped the Item, the class of its failure, the spend of its steps with the input tokens of the first request of each step (`steps`, kept apart from the trace, which drops its oldest events), and the trace of its last run. The spend is the sum of all Jobs of the Item: each passed group writes it, and a retry by a person and a return to New clear it |
| Implement | `implement.spend_counted` | The tokens of `implement.spend` that the token counter already holds. The Service writes it when it reports a Job and adds only the tokens above it to the counter, so each Job counts once. A spend below it counts whole, and the count follows it. A retry by a person and a return to New clear it with the spend |
| Implement | `implement.check`, `implement.check_log` | The check that failed in the stop, and the masked end of its log (at most 2000 characters). The next Job reads both before its first record write, gives them to the first step of its run only, and clears both in that write. A clear rule of the Service keeps them |
| Implement | `implement.conflict` | The files that a rebase conflict left unmerged, as a JSON list of masked paths (at most 20). The next Job reads it before its first record write, gives it to the first step of its run only, and clears it in that write. A clear rule of the Service keeps it |
| Implement | `implement.wip` | The work in progress that a step kept: its group, the tasks done, the tasks of the group and the last commit as JSON. The job removes it when the group passes |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
schema: spec-driven
created: 2026-11-09
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Design

## Decisions

- **Record key.** The Service keeps the token total that it has already counted in the job record key `implement.spend_counted`, a decimal integer in text. It lives with `implement.spend`, which it tracks, and the table of record keys documents it.
- **One write.** `_report` computes the total and the delta from the two keys, puts the total in the marks of the `service_release` write that already records `implement.job_state`, and passes the delta to `_emit_implement_metrics`. No extra request is needed.
- **Clears.** Every place that clears `implement.spend` clears `implement.spend_counted` in the same write: the retry start in `implement_run.py` and the return to New. The new Job then starts its spend and its count at zero, and the counter keeps its earlier total.
- **Spend below the count.** When `implement.spend` is below `implement.spend_counted`, the record started again. The whole spend counts once and the counted total takes its value, so no later report goes negative.
- **Older record.** A record with `implement.spend` and no `implement.spend_counted` has counted nothing, so the first report counts the whole spend once. An item in flight at deploy can count its earlier Jobs one more time, once. The record cannot tell whether the old code counted them.

## Rejected alternatives

- **A gauge of the last spend.** The Service holds no series for each item, and a gauge needs one label value for each item number. 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`. 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: one write holds the total with `implement.job_state`.
- **Read the counter back from the registry.** `prometheus_client.Counter` keeps one value for each label set (`repo`, `item_class`), not for each item, and it cannot subtract (`src/devops_cli/telemetry/metrics.py:88`). Rejected.
- **Add the negative difference when the spend fell.** A counter cannot decrease. Rejected.
- **Count nothing when the spend fell.** The tokens of the new Job are lost. Rejected.
- **Keep `implement.spend_counted` after a retry.** The new spend starts at zero, so a counted total above it hides the first tokens of the new spend. Rejected.
- **Start the counted total at zero without counting, for an older record.** The tokens of the first Job after deploy are lost. Rejected.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Proposal: Count the tokens of each implement Job once

## Why

The Service adds the tokens of a reported runner Job to `devops_cli_service_implement_tokens_total` (#4200). Since #4070 the record `implement.spend` holds the SUM of the spend of all Jobs of an item, not the spend of the last Job. `_emit_implement_metrics` still adds the whole sum at each report. So the tokens of an earlier Job count again at each later report, and the counter is too high for any item that needs more than one Job.

## What Changes

- Add the job record key `implement.spend_counted` (`ImplementRecordKey.SPEND_COUNTED`) in `src/devops_cli/roadmap/stores/store.py`. It holds, as decimal text, the token total that the Service has already added to the counter for the item.
- In `src/devops_cli/roadmap/implement/implement_submit.py`, `_report` reads `implement.spend` and `implement.spend_counted`, passes only the difference (the tokens of this Job) to `_emit_implement_metrics`, and writes the new total to `implement.spend_counted` in the same write that records `implement.job_state`. When the spend is below the counted total, the whole spend counts once and the counted total takes its value.
- In `src/devops_cli/roadmap/implement/implement_run.py`, the retry and the return to New clear `implement.spend_counted` in the same write that clears `implement.spend`.
- Add a row for `implement.spend_counted` to the record table of `docs/ROADMAP_WORKFLOW.md`, and a line for it to `tests/invariants/test_roadmap_implement_record_keys.py`.
- The change carries a spec delta for `roadmap-implement-job` (the token counter), so `skip_specs` is false. No setting, ConfigMap key or `Settings` field is added, removed or tightened.
- A record of an older version has `implement.spend` and no `implement.spend_counted`. It loads (`decode_job_record` reads a missing key as absent), and its first report counts the whole spend once.
- The change fits one pull request.

## Capabilities

### New Capabilities

None.

### Modified Capabilities

- `roadmap-implement-job`: Token counter, A retry and a return to New clear the counted spend

## Impact

- Code: `src/devops_cli/roadmap/stores/store.py`, `src/devops_cli/roadmap/implement/implement_submit.py`, `src/devops_cli/roadmap/implement/implement_run.py`
- Documents: `docs/ROADMAP_WORKFLOW.md`
- Tests: `tests/roadmap/implement/test_roadmap_implement_submit.py`, `tests/roadmap/implement/test_roadmap_implement_phase_marks.py`, `tests/roadmap/implement/test_roadmap_implement_records.py`, `tests/invariants/test_roadmap_implement_record_keys.py`

## Feasibility

- `src/devops_cli/roadmap/implement/implement_submit.py:304` reads `ImplementRecordKey.SPEND` and `:313` adds `input_tokens + output_tokens` of the whole record to `CONST_SERVICE_METRIC_IMPLEMENT_TOKENS`. `src/devops_cli/roadmap/implement/implement_run.py:515` loads the spend of earlier Jobs when the run is not a retry, and `:561` returns the earlier spend plus the spend of this Job. So the record is cumulative and the counter adds it whole.
- The test `test_two_jobs_of_one_item_count_each_token_once` reproduces it on `origin/main`: a first report with a record spend of 1,500 tokens and a second report with 4,000 tokens grow the counter by 5,500, not by 4,000. The run printed `assert ((1500.0, None), (5500.0, None)) == ((1500, '1500'), (4000, '4000'))`.
- A Prometheus counter cannot subtract: the registry wraps `prometheus_client.Counter` and has `increment_counter` only (`src/devops_cli/telemetry/metrics.py:88`), and the token series is an `InstrumentKind.COUNTER` (`tests/telemetry/test_telemetry_instruments.py:90`). So the Service must add a difference, and it must keep the counted total for each item. The job record is where `implement.spend` lives (`src/devops_cli/roadmap/stores/store.py:286`).
- `_JOB_RECORD_KEYS` (`src/devops_cli/roadmap/stores/store.py:613`) is built from the members of `ImplementRecordKey`, so a new member is a known key. `decode_job_record` keeps only known keys, so an older record without the key loads. The test `test_a_record_of_an_older_version_decodes_with_no_counted_spend_and_a_new_one_keeps_it` covers both.
- `implement.repair_rounds` is the sum of `self.outcomes` of one run (`src/devops_cli/roadmap/implement/implement_run.py:1052`), so it is per Job and does not double count. Only the tokens do.
- The `Settings` models are not touched, so no ConfigMap load test applies.

## Existing Tool Considered

The open, maintained `prometheus_client` 0.26.0 covers the counter but not the need: its `Counter` only increments, so it cannot remove tokens that the Service added before. No other open, maintained library keeps a counted total for each item. The change builds one record key, one small helper, two clears 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. The change deletes no internal code.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
## MODIFIED Requirements

### Requirement: Token counter
The roadmap Service SHALL increment `devops_cli_service_implement_tokens_total` with the labels `repo` and `item_class` by the tokens that the reported runner Job spent. The job record holds the spend of all Jobs of the item, so the Service SHALL keep the token total that it has counted in the job record key `implement.spend_counted`, SHALL add only the difference between the spend and that total, and SHALL write the new total in the same write that records the state of the Job. A spend below the counted total SHALL count whole once and become the counted total. A record with no `implement.spend_counted` SHALL count its whole spend once.

#### Scenario: Tokens add to the counter
- **WHEN** A reported runner Job for repository `owner/repo` and item class `feat` records 1,000 tokens.
- **THEN** The series `devops_cli_service_implement_tokens_total{repo="owner/repo",item_class="feat"}` increases by 1,000.

#### Scenario: A second Job counts only its own tokens
- **WHEN** The record of an item holds 1,500 tokens when its first Job is reported, and 4,000 tokens (the sum of both Jobs) when its second Job is reported.
- **THEN** The series increases by 1,500 at the first report and by 2,500 at the second, and `implement.spend_counted` holds `1500` and then `4000`.

#### Scenario: A spend below the counted total counts whole
- **WHEN** The record holds 900 tokens in `implement.spend` and `1500` in `implement.spend_counted` when a Job is reported.
- **THEN** The series increases by 900 and `implement.spend_counted` holds `900`.

#### Scenario: A record of an older version counts its spend once
- **WHEN** The record holds 1,500 tokens in `implement.spend` and no `implement.spend_counted` when a Job is reported.
- **THEN** The series increases by 1,500 and `implement.spend_counted` holds `1500`.

#### Scenario: A Job that spent nothing adds nothing
- **WHEN** The record holds 1,500 tokens in both `implement.spend` and `implement.spend_counted` when a Job is reported.
- **THEN** The series does not increase.

## ADDED Requirements

### Requirement: A retry and a return to New clear the counted spend
The job SHALL clear `implement.spend_counted` in the same write that clears `implement.spend`, at the start of a retry and at a return to New.

#### Scenario: A retry starts the count from zero
- **WHEN** A person retries an item whose record holds `implement.spend_counted`, and the new Job starts.
- **THEN** the record holds no `implement.spend_counted`.

#### Scenario: A return to New clears the count
- **WHEN** The job returns an item to New and its record holds `implement.spend_counted`.
- **THEN** the record holds no `implement.spend_counted`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Tasks

## 1. Count the tokens of each Job once

Kind: behavior. Reads: `src/devops_cli/roadmap/implement/implement_submit.py`, `src/devops_cli/roadmap/implement/implement_run.py`, `src/devops_cli/roadmap/stores/store.py`. Changes: the same files, and the tests below.

- [x] 1.1 Add the failing tests: `test_two_jobs_of_one_item_count_each_token_once`, `test_a_spend_below_the_counted_spend_is_counted_whole_and_becomes_the_counted_spend`, `test_a_record_of_an_older_version_counts_its_whole_spend_once` and `test_a_report_without_new_spend_counts_no_tokens` in `tests/roadmap/implement/test_roadmap_implement_submit.py`; the retry and return tests in `tests/roadmap/implement/test_roadmap_implement_phase_marks.py`; the old-record load test in `tests/roadmap/implement/test_roadmap_implement_records.py`; and the reader line in `tests/invariants/test_roadmap_implement_record_keys.py`. They fail on the base
- Verification: `$ uv run devops ci fail-to-pass --base origin/main`
- [x] 1.2 Add `SPEND_COUNTED = "implement.spend_counted"` to `ImplementRecordKey` in `src/devops_cli/roadmap/stores/store.py`, and add it to the docstring of the class
- Verification: `$ uv run devops ci test tests/roadmap/implement/test_roadmap_implement_records.py`
- [x] 1.3 In `src/devops_cli/roadmap/implement/implement_submit.py`, read the spend and the counted total once in `_report`, write the new total with `implement.job_state`, and pass only the delta to `_emit_implement_metrics`, which no longer reads the spend itself
- Verification: `$ uv run devops ci test tests/roadmap/implement/test_roadmap_implement_submit.py tests/invariants/test_roadmap_implement_record_keys.py`
- [x] 1.4 In `src/devops_cli/roadmap/implement/implement_run.py`, clear `implement.spend_counted` with `implement.spend` at the retry start and at the return to New
- Verification: `$ uv run devops ci test tests/roadmap/implement/test_roadmap_implement_phase_marks.py`

## 2. Document the record key

Kind: documents. Reads: `docs/ROADMAP_WORKFLOW.md`. Changes: `docs/ROADMAP_WORKFLOW.md`.

- [x] 2.1 Add `implement.spend_counted` to the record table of `docs/ROADMAP_WORKFLOW.md`: the token total that the Service has added to the token counter, which a retry and a return to New clear with `implement.spend`
- Verification: `$ uv run devops ci test tests/invariants/test_roadmap_implement_record_keys.py`

## Workflow follow-up

- Pending a person: review the diff in tuicr (tuicr -r <base>..HEAD).
35 changes: 34 additions & 1 deletion openspec/specs/roadmap-implement-job/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -1580,12 +1580,34 @@ The roadmap Service SHALL increment `devops_cli_service_implement_repair_rounds_
- **THEN** The series `devops_cli_service_implement_repair_rounds_total{repo="owner/repo",item_class="feat"}` increases by two.

### Requirement: Token counter
The roadmap Service SHALL increment `devops_cli_service_implement_tokens_total` with the labels `repo` and `item_class` by the token count that the reported runner Job records.
The roadmap Service SHALL increment `devops_cli_service_implement_tokens_total` with the labels `repo` and `item_class` by the tokens that the reported runner Job spent, and not by the tokens of earlier Jobs of the item.

#### Scenario: Tokens add to the counter
- **WHEN** A reported runner Job for repository `owner/repo` and item class `feat` records 1,000 tokens.
- **THEN** The series `devops_cli_service_implement_tokens_total{repo="owner/repo",item_class="feat"}` increases by 1,000.

### Requirement: Counted spend
The job record holds the spend of all Jobs of the item, so the roadmap Service SHALL keep the counted token total in the job record key `implement.spend_counted`. It SHALL add only the difference between the spend and that total to the token counter, and SHALL write the new total in the same write that records the state of the Job.

#### Scenario: A second Job counts only its own tokens
- **WHEN** The record of an item holds 1,500 tokens when its first Job is reported, and 4,000 tokens (the sum of both Jobs) when its second Job is reported.
- **THEN** The series increases by 1,500 at the first report and by 2,500 at the second, and `implement.spend_counted` holds `1500` and then `4000`.

### Requirement: Counted spend edge cases
A spend below the counted total SHALL count whole once and become the counted total. A record with no `implement.spend_counted` SHALL count its whole spend once.

#### Scenario: A spend below the counted total counts whole
- **WHEN** The record holds 900 tokens in `implement.spend` and `1500` in `implement.spend_counted` when a Job is reported.
- **THEN** The series increases by 900 and `implement.spend_counted` holds `900`.

#### Scenario: A record of an older version counts its spend once
- **WHEN** The record holds 1,500 tokens in `implement.spend` and no `implement.spend_counted` when a Job is reported.
- **THEN** The series increases by 1,500 and `implement.spend_counted` holds `1500`.

#### Scenario: A Job that spent nothing adds nothing
- **WHEN** The record holds 1,500 tokens in both `implement.spend` and `implement.spend_counted` when a Job is reported.
- **THEN** The series does not increase.

### Requirement: Consecutive-failure gauge
The roadmap Service SHALL expose `devops_cli_service_implement_consecutive_failures` with the labels `repo` and `class`, and the gauge SHALL hold the number of stops in a row of that class.

Expand Down Expand Up @@ -1770,3 +1792,14 @@ The job SHALL read `implement.check_log` and `implement.conflict` before its fir
#### Scenario: A continuation after a clear rule
- **WHEN** a clear rule of the Service removed the stop and a new Job takes the item
- **THEN** the message of the first step holds the recorded log

### Requirement: A retry and a return to New clear the counted spend
The job SHALL clear `implement.spend_counted` in the same write that clears `implement.spend`, at the start of a retry and at a return to New.

#### Scenario: A retry starts the count from zero
- **WHEN** A person retries an item whose record holds `implement.spend_counted`, and the new Job starts.
- **THEN** the record holds no `implement.spend_counted`.

#### Scenario: A return to New clears the count
- **WHEN** The job returns an item to New and its record holds `implement.spend_counted`.
- **THEN** the record holds no `implement.spend_counted`.
2 changes: 2 additions & 0 deletions src/devops_cli/roadmap/implement/implement_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,7 @@ def _start(self) -> None:
marks[ImplementRecordKey.FAILURE] = None
marks[ImplementRecordKey.CHECK] = None
marks[ImplementRecordKey.SPEND] = None
marks[ImplementRecordKey.SPEND_COUNTED] = None
self.store.set_field(
self._fresh(), ItemField.STATUS, CONST_ROADMAP_STATUS_IN_PROGRESS, marks=marks
)
Expand Down Expand Up @@ -795,6 +796,7 @@ def _return_to_new(self, template: str, evidence: str) -> None:
ImplementRecordKey.PR: None,
ImplementRecordKey.BASE: None,
ImplementRecordKey.SPEND: None,
ImplementRecordKey.SPEND_COUNTED: None,
ImplementRecordKey.CHECKED_SHA: None,
ImplementRecordKey.PUBLISHED_SHA: None,
ImplementRecordKey.RESULT: ImplementResult.RETURNED.value,
Expand Down
Loading
Loading