Make `SpendLimits` replay-safe under durable execution

Merged
DO
3mo ago
+392-245

Spun off from #710, which limited itself to four replay-safe capabilities and left SpendLimits out because it needs idempotency work rather than a plain adoption.

Addresses #531. That issue described SpendLimits as flatly unsupported under durable execution, and the capability's own docs said so in three places. This narrows it to a real, documented set of remaining constraints rather than closing it outright, so it stays open until those are settled.

What was actually broken

The clock. before_model_request runs in orchestration code, and the default clock calls datetime.now, which Temporal's workflow sandbox restricts. That was the first thing Temporal stopped, before any of the accrual concerns could even be reached. Making the clock read a durable operation moves it into the activity, which is what makes Temporal viable here at all. The budget window key is derived from that reading, so journaling it also stops the window drifting between an execution and its replay.

The dedup token was derived from a clock. _dedup_token fed ModelResponse.timestamp into the token that is supposed to make a re-executed accrual land once. Core sets that timestamp from the local clock, so a replayed accrual produced a different token and the window counted the same response twice, which is precisely the failure the token exists to prevent. It now digests a canonically-serialized response with timestamp and the later-stamped run_id neutralized, namespaced by ctx.run_id and ctx.run_step. This was found by verifying the brief's assumption rather than by a failing test, so it had no coverage before.

Counter reads and accruals were not journaled at all, so recovery re-entered the store.

What this does

Three durable operations: now, read, and accrue. A journaled accrue returns its recorded totals on replay without touching the store, and SpendEntry.token remains the second layer for recovery that reaches the store without the journal.

SpendLimits also gains a stable default id='spend_limits', using the same KW_ONLY idiom shipped for SummarizingCompaction in #707. This is not cosmetic: collect_capability_operations is a class-level scan, so one @durable_operation method makes every instance contribute durable operations and durability binding rejects id=None. Without the default, this change would break every existing SpendLimits user who attaches a durability capability. An explicit id is only needed to attach more than one instance.

Limits that remain, and are now documented rather than implied

  • Recovery landing on a fresh worker needs a store that survives worker replacement. InMemorySpendStore does not, and says so.
  • The deprecated SpendStore adapter drops SpendEntry.token, because a single-key add has nowhere to put it. That silently removed the protection; it now warns, explaining that dropping the token removes the guard for recovery that cannot consult the journal.
  • exhausted() is still an admission check that reserves nothing: a floor on spend already recorded, not a ceiling on what the run goes on to spend.

Also

_composition.py imported BaseDurabilityCapability from the private pydantic_ai.durable_exec._base with a comment asking for a public route (pydantic-ai#7771). That export is public now, so it imports from pydantic_ai.durable_exec and the note is gone.

Spend tests 221 passed, full suite 4,600 passed / 66 skipped, spend coverage 100% statements and branches, docs parity and snippets green, lint and pyright clean.