Repository navigation
feat(task): a running step's usage reaches the run's ledger before the step ends - #898
Merged
Merged
Conversation
…e step ends Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
earakely-scale
marked this pull request as ready for review
November 23, 2026 22:36
…one is tried again Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…he run's total is partial while any is Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…the step, and the store's running write marks its entries Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…, running) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changes
F2 (FD-5986): a running step's usage reaches the run's ledger before the step ends, so a budget reading the ledger sees a long step's spend, and a worker that dies mid-step keeps it.
Task.runwrites a running step's entries to the ledger, every 15 s in which they changed:async with running_usage(instance_id, step.id, context)around the step'sexecute(task/usage/running.py). It reads the entries the step keeps inmetadata.step_usageand merges them into the ledger alone, marked running (TaskInstanceStore.record_running_usage_sync,store.record_running_usage).running: true, and the run'susage_total.partialis true while any entry is: the turn in flight, and anything since the last write, isn't in them. A run whose worker died keeps them marked, so its total says it's a lower bound.Task.runstops the writer, waiting for a write in flight, before the step's completion write, so a running write never lands after the final one.always_on(each wake) and judges (each call). Nothing changes in the steps.usage.ledger.ahead); a role's turns only grow. So a write a cancel left running in its thread, landing after a newer entry (fromundo_stepsin a retry's drain, say), changes nothing.task/usage/: the P11 ledger moves there fromtask/usage_ledger.py(task/usage/ledger.py, its tests totst/unit/task/usage/ledger_test.py), beside this PR'stask/usage/running.py. The step-side recording,task_step/usage.py, stays: steps never importagent_env.task, whose__init__importsTaskand through it the steps, so moving it would make a cycle.Decision (with Edgar, 2026-11-23)
The issue planned to carry the usage on
heartbeat(details). The hub's worker passes a heartbeat that takes no argument, and a beat that raises is dropped, so that change would stop its beats and Temporal would kill its long steps. So the heartbeat stays a zero-argument liveness signal, andTask.runwrites the ledger itself. A runner that callsstep.execute()itself, as the hub's worker does, gets running usage once it does the same, as with FD-6393. When F6 records a turn's usage mid-turn, it reaches the ledger through the same writes.Tests
tst/unit/task/usage/running_test.py, on a real SQLite store with steps that record usage as a prompt step does:stopwaits for a write in flight;E2E against the real proxy
All on an admin host through a logging pass-through in front of the real proxy, the run's instance read from the store every second (its ledger, its completed steps, the total's
partial). Some runs hadagent-envkilled with SIGKILL mid-step, as a worker dying: no end write, thenagent-env task teardown --instance <id> --force. Faults are injected 500s on 0%, 20% or 35% of calls, or, to force a step to fail, on every call of one model after its first few.On the head (bf49d99), three rounds, 55 runs:
running, totalpartial; completed runs' ledgers equal the proxy per model; killed runs keep what was writtenrunning; ledger per model equals the proxy exactly (e.g. 55,845 + 114,748 = 170,593)running; equal to the proxy when the drain cut no solver turn, otherwise short by exactly the cut-off turn, markedpartialrunning, entries markedrunning, totalpartial; teardown removed every sandboxEvery run held every rule the checker applies: no entry ever went back (turns and tokens never fell between two states), no ended run kept a
runningentry, the total'spartialmatched its entries, each model's ledger was at most the proxy's and equal for a completed run with nothing partial. The remaining verdicts were scenarios that didn't trigger (a solver that skipped the slow command, Codex retrying through the injected faults), whose ledgers were exact all the same.Earlier, on the first version (9fff0f9): 6 e2e and 29 chaos runs of the long chat (normal, killed after one or two turns, faults up to 35%), all exact or kept as written.
Model spend: about $3.60 at list prices for the PR.
Docs
AGENTS.md's ledger paragraph says when a running step's entries are written. For FD-6263 and FD-6442: a run'susage_totalincludes a running step's usage so far, at most 15 s old, for runsTask.rundrives; on the hub, once its worker does the same.🤖 Generated with Claude Code


Confidence Score: 5/5
The PR appears safe to merge.Summary
Running steps save their usage to the run’s ledger before they finish. The latest changes group this code under
task/usage/and update its imports and documentation.Diagram
%%{init: {'theme': 'neutral'}}%% flowchart LR A[Step records usage] --> B[Writer checks every 15 seconds] B --> C[Changed entries saved as running] C --> D[Run total marked partial] A --> E[Step ends] E --> F[Writer stops] F --> G[Final entries replace running entries]Reviews (8) · Last reviewed commit: "refactor(task): a run's usage code is on..." · Reviewed by Greptile