Skip to content

feat(task): a running step's usage reaches the run's ledger before the step ends - #898

Merged
earakely-scale merged 5 commits into
mainfrom
edgararakelyan/fd5986-heartbeat-usage
Nov 24, 2026
Merged

earakely-scale merged 5 commits into
mainfrom
edgararakelyan/fd5986-heartbeat-usage

Conversation

@earakely-scale

@earakely-scale earakely-scale commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

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.run writes 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's execute (task/usage/running.py). It reads the entries the step keeps in metadata.step_usage and merges them into the ledger alone, marked running (TaskInstanceStore.record_running_usage_sync, store.record_running_usage).
  • Running entries are marked running: true, and the run's usage_total.partial is 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.
  • The step's completion replaces them. The ledger keeps an entry by attempt, step and role, and a step's entry is its sum so far, so its final entry replaces the running one and nothing counts twice. Task.run stops the writer, waiting for a write in flight, before the step's completion write, so a running write never lands after the final one.
  • Every step that records usage gets it: prompt steps (each finished turn), always_on (each wake) and judges (each call). Nothing changes in the steps.
  • A running write never takes the ledger back. It merges an entry only when it has more turns than the ledger's entry of the same attempt, step and role (usage.ledger.ahead); a role's turns only grow. So a write a cancel left running in its thread, landing after a newer entry (from undo_steps in a retry's drain, say), changes nothing.
  • The run's usage code is one package, task/usage/: the P11 ledger moves there from task/usage_ledger.py (task/usage/ledger.py, its tests to tst/unit/task/usage/ledger_test.py), beside this PR's task/usage/running.py. The step-side recording, task_step/usage.py, stays: steps never import agent_env.task, whose __init__ imports Task and through it the steps, so moving it would make a cycle.
  • A write that failed is tried again at the next interval, even when nothing changed since; a store error is logged, not raised, and the step's completion writes its entries again.

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, and Task.run writes the ledger itself. A runner that calls step.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:

  • a long step's turns are in the ledger before the step ends, each as it comes, marked running with the total partial, and its completion replaces them with the final entry, the total no longer partial;
  • a worker that dies mid-step (its end writes never come, then the run is cancelled) keeps the turns written before, still marked running, so its total is partial;
  • a write happens only when the entries changed, and stop waits for a write in flight;
  • a snapshot that lands late never takes the ledger back, and one with more turns replaces the entry;
  • a write that failed is tried again though nothing changed.

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 had agent-env killed with SIGKILL mid-step, as a worker dying: no end write, then agent-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:

Scenario Runs Result
Long chat (Codex, 6 turns with a user simulator), normal and killed 11 running entries every ~15 s, marked running, total partial; completed runs' ledgers equal the proxy per model; killed runs keep what was written
Two chats in parallel on one instance (Codex gpt-4.1-mini + gpt-4.1-nano, Hermes claude-haiku + gpt-4o-mini), normal and killed 13 both steps' running writes land beside each other; every completed run's four entries equal the proxy per model
A chat step that fails mid-chat and retries (rolled back, run again) 3 attempt 0 and attempt 1 both kept, nothing left running; ledger per model equals the proxy exactly (e.g. 55,845 + 114,748 = 170,593)
A sibling's retry draining a running chat (B fails at 25 s; the drain cancels A mid-chat; both run again) 7 A's drained attempt 0 and its attempt 1 both kept, nothing running; equal to the proxy when the drain cut no solver turn, otherwise short by exactly the cut-off turn, marked partial
Kills during any of these 18 instance stays running, entries marked running, total partial; teardown removed every sandbox

Every 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 running entry, the total's partial matched 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's usage_total includes a running step's usage so far, at most 15 s old, for runs Task.run drives; on the hub, once its worker does the same.

🤖 Generated with Claude Code

RetriggerView in GreptileConfidence 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.

  • Task runs now add a step’s usage to its ledger while the step is still running.

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]
Loading

Reviews (8) · Last reviewed commit: "refactor(task): a run's usage code is on..." · Reviewed by Greptile

…e step ends

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@earakely-scale
earakely-scale marked this pull request as ready for review November 23, 2026 22:36
@earakely-scale
earakely-scale requested a review from a team as a code owner November 23, 2026 22:36
Comment thread src/agent_env/task/running_usage.py Resolved
Comment thread src/agent_env/task/running_usage.py Resolved
earakely-scale and others added 4 commits November 23, 2026 14:41
…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>
@earakely-scale
earakely-scale merged commit 34bc1e2 into main Nov 24, 2026
35 checks passed
@earakely-scale
earakely-scale deleted the edgararakelyan/fd5986-heartbeat-usage branch November 24, 2026 02:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant