Skip to content

Run pipeline: cost only added, security findings kept, early failures said, multi-repo PRs paired - #346

Merged
nambok merged 4 commits into
mainfrom
fix/run-pipeline-audit
Oct 28, 2026
Merged

nambok merged 4 commits into
mainfrom
fix/run-pipeline-audit

Conversation

@nambok

@nambok nambok commented Oct 27, 2026

Copy link
Copy Markdown
Collaborator

Summary

Four run-pipeline bugs from the production audit, each checked against the code first: a run's cost written back to near zero, security findings dropped under time pressure, a ticket run that failed early without saying so, and multi-repo run records that paired one repository with another repository's PR.

Changes

  • Run cost is only ever added to. Resume, CI-fix and continuation invocations wrote SET cost_usd = :c (and the token fields) from a usage restored from the checkpoint, which has a 7-day TTL, or started at zero. Once the checkpoint expired, the earlier cost was overwritten. Now every run write ADDs what this invocation used since its last write (TokenUsage::unrecorded / mark_recorded), and failures add through RunFailure::add. Resume no longer restores usage from the checkpoint. A source test refuses any SET of tokens_in, tokens_out, cache_read_tokens, cache_write_tokens or cost_usd on run records.
  • Security findings are never dropped.
    • The check's result is now a verdict: passed, findings, or not checked. An error never counts as a pass.
    • Findings with too little time left park a security fix round (run_round::Kind::SecurityFix). It runs on the PR in a fresh invocation with its own budget (the rounds from Plan check before the PR; fix rounds get their own budget, push once, and never fail an open PR; generated files are regenerated #331) and re-checks security when it lands.
    • Whatever is still open goes under the PR's Open items, each line marked. While a marked line is listed, the PR is never marked ready, and the re-check removes the lines once it passes.
    • The multi-repo path re-checks after its fix and lists what's left the same way.
    • The comment that claimed findings were posted elsewhere is corrected.
  • A ticket run that can't start, or panics, fails at once.
    • When the run record write is refused (too large a ticket, for example), a minimal record is kept. The run is failed with a plain reason that includes the SDK cause, and the ticket is told either way. The ticket lock is released.
    • A panic in the passes becomes an ordinary failure with a reason, instead of a run left "running" until the stuck-run sweep. A test holds orchestrate_ticket to failing the run before any error return.
  • Multi-repo run records pair each repository with its own PR.
    • Each PR is stored whole (prs: repo, pr_number, branch, url), and the record's repo, pr_number, branch and pr_url all come from one of them: the PR in the run's own repo when there is one.
    • Lookups by PR number or branch use repository-qualified keys (pr_keys, branch_keys). Older records are matched by URL.
    • When a multi-repo run's other PR closes or merges, its outcome goes on its own entry, and it is scored and learned from with its own repository.
    • The runs API returns prs, and docs/api/openapi.json is regenerated.

Verification

  • cargo fmt --all --check, cargo clippy --workspace --all-targets -D warnings and cargo test --workspace (common 1223, gateway 585, worker 770) pass on this branch.
  • New tests:
    • run_writes_add_only_what_is_not_yet_recorded, run_records_never_set_tokens_or_cost, a_failure_adds_its_usage_to_the_record
    • security: an_error_is_not_a_pass, findings_become_open_items, the_description_holds_security_items_until_they_are_fixed
    • start failures: a_start_failure_is_plain_and_keeps_the_cause, a_panic_becomes_a_reason, orchestrate_ticket_fails_the_run_before_returning_an_error
    • run_prs: primary pairing, repository-qualified keys, reading older records, finding a PR by repository and number
  • python3 -m unittest discover -s lambda/tests passes. The stuck-run sweep knows the new round kind.
  • No production data was read or changed.

…ever sets a total

Resume, CI-fix and continuation invocations SET the run's tokens and cost
from a usage restored from the checkpoint (7-day TTL) or started at zero,
wiping what earlier invocations recorded. Each run write now ADDs what this
invocation used since its last write (TokenUsage::unrecorded/mark_recorded);
failures add through RunFailure::add. A source test refuses a SET of these
fields on run records.
…tells the ticket

A run record write that was refused left the ticket with no answer, and a
panic in the passes left the run 'running' until the stuck-run sweep called
it a timeout during an unknown pass. The start failure now keeps a minimal
record when the full one is refused, fails it with a plain reason and the
SDK cause, tells the ticket either way and releases the ticket lock; a panic
becomes an ordinary failure with a reason.
… and the PR held a draft

With under four minutes left the security fix was skipped and the PR opened
with the findings nowhere, and a security check that errored counted as a
pass. The check's result is now a verdict (passed, findings, not checked);
an error is never a pass. Findings there's no time to fix park a security
fix round (run_round::Kind::SecurityFix) that runs on the PR in a fresh
invocation with its own budget and re-checks when it lands. Whatever is
still open is listed under the PR's Open items with a mark, and the PR is
not marked ready while one is listed; the re-check removes them when it
passes. The multi-repo path re-checks after its fix and lists what's left.
… and number always pair

The run record kept repo as the run's repository but pr_number and branch
from the first pull request, which could be in another repository, and the
fallback lookups matched a PR number or branch name in any repository. Each
pull request is now stored whole (prs: repo, pr_number, branch, url), the
record's repo/pr_number/branch/pr_url come from one of them (the one in the
run's repository when there is one), and lookups use repository-qualified
keys (pr_keys, branch_keys), with older records matched by URL. A closed or
merged PR of a multi-repo run that isn't the record's own gets its outcome
on its entry, and is scored and learned from with its own repository. The
runs API returns prs.
@nambok
nambok merged commit a24df10 into main Oct 28, 2026
8 checks passed
@nambok
nambok deleted the fix/run-pipeline-audit branch October 28, 2026 02:33
nambok added a commit that referenced this pull request Nov 13, 2026
…st tokens per merged PR (#662)

* Analytics: token use by part, kind of work and model, and a tokens-per-merged-PR that counts only the runs behind merged PRs

The tile divided every run's tokens by the merged pull requests and compared
periods whose run records were written differently (before #346 a resume or
CI fix SET a run's tokens, dropping earlier invocations).

- common::usage: UsageSlice, slices() over the per-feature and per-model
  counters every call already writes, total_slice(), price_known(), and
  work_kind (each feature in exactly one kind of work).
- GET /api/analytics gains token_use: the period's total by part with cost,
  by day, by kind of work and by model (cache hit share, whether the price
  is known) from the meter's DAY# items; tokens per merged PR from the runs
  started in the period whose pull request merged, also by kind of run; run
  tokens by how the work ended; the heaviest runs and pull requests.
- A number is compared with the period before only when both were recorded
  the same way: the metered total when per-call counting covers the period
  before, run-record numbers when it starts on or after the day run records
  became complete. Otherwise previous is null and the reason is named.
- GET /api/usage gains models (tokens by part and cache hit rate per model);
  docs/api/openapi.json regenerated.

No new storage: the breakdown counters already exist per day, and nothing
before per-call counting can be split by kind or model.

* Token use: tokens only in the analytics answer

Analytics shows tokens, not money, so token_use carries no cost: the cost
fields, the per-PR cost, the previous cost and price_known are gone, with
common::usage::price_known, which nothing else used. A test asserts the
answer names no cost or price.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant