airflow.providers.common.ai.durable.journal¶
The durable journal: replay an agent’s completed steps when Airflow retries its task.
An Airflow task retry is a fresh process that runs the agent again from the top. The journal records each step an agent run completes (a model response, a tool result, a capability operation) under the step’s position in that run, and on the retry hands back the recorded result instead of running the step again. Steps the previous attempt never reached run live and are recorded in turn.
A step is replayed only when the step at the same position in the previous attempt
had the same name and the same fingerprint. The name says what kind of step it was
(agent__model.request, agent__function_toolset__x.call_tool:query); the
fingerprint, computed by the framework adapter, says what it was asked (the message
history, the tool arguments). The first mismatch means the run took a different path
from the previous attempt, so that step and every step after it run live. So does
everything the previous attempt did after a step that raised, and, when the previous
attempt failed on the model’s answers, everything from the model’s last request on. A
tool call that raised on two attempts in a row counts as such an answer: the next retry
asks the model again from the request that chose the call.
A DurableJournal belongs to one task attempt. Each agent run in it is a
DurableRun, numbered in the order the runs start, with positions of its own,
so a task that runs several agents replays each of them independently.
Nothing here depends on an agent framework. Payloads are JSON-compatible values and
fingerprints are opaque strings.
AirflowDurability drives the
journal for pydantic-ai. Another framework’s adapter starts a run with
DurableJournal.start_run() and calls one of two shapes of API on it:
DurableRun.run()wraps a step:await run.run(name, kind=..., fingerprint=..., body=...).DurableRun.claim()andJournalStep.record()split it in two, for a framework whose hooks see a step’s input and its output in separate callbacks.
An adapter that stops the task to wait for input (request_input()) calls
DurableRun.pause() first, so that the resumed task replays what this attempt recorded.
Attributes¶
What a step is, for the end-of-run summary and for adapters that treat model steps specially. |
Classes¶
A step result an adapter could not encode as JSON: the live value, and why. |
|
One position in a run, claimed by |
|
What one task attempt replayed and recorded, by step kind; a review counts each regeneration on its own. |
|
One agent run's steps in the journal. |
|
One task attempt's view of the durable journal. |
Functions¶
|
Return the journal storage for the task instance |
|
Make |
Return the journal already recording this run, without creating one. |
|
Return the active journal, or |
Module Contents¶
- airflow.providers.common.ai.durable.journal.StepKind[source]¶
What a step is, for the end-of-run summary and for adapters that treat model steps specially.
- class airflow.providers.common.ai.durable.journal.Unrecordable[source]¶
A step result an adapter could not encode as JSON: the live value, and why.
A step body returns it in place of the payload;
JournalStep.run()passes it toJournalStep.cannot_record()instead of recording it, and returns it to the adapter.
- class airflow.providers.common.ai.durable.journal.JournalStep[source]¶
One position in a run, claimed by
DurableRun.claim().When
replayedis true,payloadholds what the previous attempt recorded and the caller returns it instead of running the step. Otherwise the caller runs the step insideexecuting()and passes its result torecord(), or the exception it raised tofail();run()does all three.- durable_run: DurableRun[source]¶
- count_as_replayed()[source]¶
Count this step as replayed, once.
For a step from
DurableRun.reserve(), whichDurableRun.claim_reserved()does; a step fromDurableRun.claim()is already counted.
- async run(body, *, to_record=None)[source]¶
Run a step that was not replayed and record what
bodyreturns, or that it raised.- Parameters:
to_record (collections.abc.Callable[[Any], Any] | None) – Transforms the live result before it is recorded, such as masking secrets in a tool result. The live result is returned unchanged.
- executing()[source]¶
Mark the code in the
withblock as this step’s work.An agent run that starts inside the block, such as an agent a tool calls, is numbered under this step, so the runs of later steps keep their numbers on a retry that replays this step instead of running it.
- record(payload)[source]¶
Record the step’s result so a retry can replay it.
- Parameters:
payload (Any) – A JSON-compatible value.
- Returns:
Whether it was recorded. A step that was not recorded runs again on retry, and one claimed with
replayable=Falseis never recorded.- Return type:
- cannot_record(error)[source]¶
Note that the step’s result has no form the journal can hold, such as a value with no JSON form.
- Raises:
DurableJournalError – When the journal fails on such results, as it does by default. Otherwise the step is left unrecorded, as when storage skips a write, and runs again on retry.
- fail(error)[source]¶
Note that the step raised
error.Nothing is recorded for the step, so it runs again on retry. If
errorgoes on to fail the run,DurableRun.fail()uses this to tell which steps the run had already started when it failed; a run that recovers from the error is unaffected.
- class airflow.providers.common.ai.durable.journal.JournalStats[source]¶
What one task attempt replayed and recorded, by step kind; a review counts each regeneration on its own.
- replayed: collections.Counter[StepKind][source]¶
- recorded: collections.Counter[StepKind][source]¶
- class airflow.providers.common.ai.durable.journal.DurableRun(journal, key, *, started_before=True)[source]¶
One agent run’s steps in the journal.
Positions are handed out in the order steps are claimed. A step must be claimed before its first
await, so concurrent steps take their positions in the order they were started rather than the order they finish. An adapter whose concurrent steps can be delayed before their claim must claim them itself in a deterministic order.Besides its steps, a run keeps one entry about itself: where the last attempt that failed stopped being trustworthy, and the furthest position any attempt reached, so a successful run deletes every step an earlier attempt left behind.
- peek(position)[source]¶
Return the entry the previous attempt recorded at
position, without claiming it.Nonewhen nothing usable was recorded there, no earlier attempt started the run, or the run has already diverged. The entry is a dict withname,kind,fingerprintandpayload.
- reserve(calls, *, kind)[source]¶
Take a position for each of
callsin order, and keep them forclaim_reserved().For an adapter that has to number steps before it knows whether they will run. Each call is
(call_id, name, fingerprint). A replay counts in the stats only once the step is taken. Replaces what an earlierreserveleft unclaimed, and a call whose id is already incallstakes no position. Likeclaim(), call it before the firstawait.
- has_recorded(call_id)[source]¶
Whether the step
reserve()kept forcall_idholds a result an earlier attempt recorded.
- claim_reserved(call_id, name, *, kind, fingerprint)[source]¶
Take the step
reserve()kept forcall_id, counting its replay.A call whose id was not reserved, or whose reservation is already taken or belongs to another call sharing the id (some non-OpenAI adapters), takes a new position as
claim()does. Call it before the firstawait.
- claim(name, *, kind, fingerprint, replayable=True)[source]¶
Take the next position and look up what the previous attempt recorded there.
- Parameters:
name (str) – What kind of step this is. Must be the same on every attempt for the same step, and should differ between steps that are not interchangeable.
kind (StepKind) –
"model","tool"or"other".fingerprint (str | None) – What the step was asked, or
Nonewhen it cannot be fingerprinted. A step without one replays on a matching name, and only right after a step that replayed.replayable (bool) –
Falsefor a step that must always run live, such as a tool whose effect Airflow cannot observe or one its metadata opts out. It still takes a position, so the steps after it keep theirs, but nothing is looked up or recorded for it.
- async run(name, *, kind, fingerprint, body, replayable=True, to_record=None)[source]¶
Replay the step at the next position, or run
bodyand record what it returns.
- fail(error, *, model_at_fault=False)[source]¶
Record that the run failed with
error, so a retry does not replay its error path.Steps the run started before the step that raised
error(tool calls running alongside it, say) still replay on retry; everything from then on, including what error handling recorded, runs again.- Parameters:
model_at_fault (bool) – The run failed because of what the model answered rather than in a step, such as the framework giving up after the model’s answers failed its checks too many times. Those answers were recorded and would replay into the same failure on every retry, so the retry asks the model again from its last request. Ignored when a step raised
error: that step decides where the retry runs live.
A tool call that raises
erroron two attempts in a row, with the same arguments, is treated as a failure the model caused as well: the first retry runs the call again in case its failure was transient, and the next asks the model again from the request that chose the call, rather than replay that choice into the same failure.
- pause()[source]¶
Record that the task is about to pause, so that resuming it replays what this attempt recorded.
An adapter calls this before it stops the task to wait for input. Where an earlier attempt’s failure, or a different path this attempt took, left entries that must not replay, this attempt has run live from there: what it recorded becomes trusted, and the earlier entries at positions it left empty (a call still waiting for approval, a tool that raised) are deleted. Positions from here on still run live after the pause. Otherwise it only notes how far this attempt got, when that is further than any before.
- complete()[source]¶
Note that the run succeeded, for a task that keeps the journal after it, such as for a review.
An earlier attempt that failed left a note saying where a retry stops replaying. Once a run succeeds, a later attempt must replay what this one recorded instead, as for a pause, and not an entry an earlier attempt left at a position this one ran without recording, or past where this one ended. Also notes how far the run went, and where its last model request was, when a step it ran is not in the journal (a tool set to run on every attempt, say), as a later reader cannot find either past such a gap; and rewrites that note when this attempt’s last model request is elsewhere. Writes nothing on a first attempt that recorded every step, or a retry that replayed every step, when no earlier attempt left a note and none went further. A retry that ran a step live always writes the note: an attempt that was killed leaves none, and may have recorded steps past where this run ended. Reads back each stale entry it deletes, so that one the storage failed to delete is not trusted.
- ask_model_again()[source]¶
Make a retry ask the model again for this run’s final answer, which succeeded but was turned down.
For a run whose answer a reviewer rejected, possibly in a later process than the run’s: a retry replays the run’s steps up to its last model request, then asks the model again from there. A run with no model step recorded runs again from its start.
- class airflow.providers.common.ai.durable.journal.DurableJournal(storage, *, clean_up_after_run=False, fail_on_unrecordable=True, first_run=0)[source]¶
One task attempt’s view of the durable journal.
- Parameters:
storage (airflow.providers.common.ai.durable.base.DurableStorageProtocol) – Where steps are stored; see
build_task_storage().clean_up_after_run (bool) – Delete a run’s steps as soon as it succeeds. A journal the operator manages leaves this off and calls
cleanup()itself after the task’s own post-run work has succeeded.fail_on_unrecordable (bool) – Fail the task on a step result the journal cannot hold (see
JournalStep.cannot_record()). WithFalsethe step is logged and runs again on retry instead.first_run (int) – The number of the first top-level run this journal starts, for a task that starts later runs in a later process than the runs before them, such as a regeneration after a review round.
- find_executing_tool_steps()[source]¶
Return the tool calls of this journal whose work is running now, in any thread.
It sees a call from code that runs in another thread without a copy of the call’s context, such as an agent the tool runs with
loop.run_in_executor.
- get_run_id(*, default)[source]¶
Return the id that names the task’s agent run on every attempt.
The first attempt stores
defaultbeside the journal; a retry reads it back. It is deleted with the journal when the task succeeds, so a later clear of the task starts over with a new id. Capabilities that key their own state on the agent’srun_id, such as pydantic-ai-harnessSpendLimitsandStepPersistence, need it to stay the same across a replay.- Parameters:
default (str) – The id to use when no earlier attempt stored one, such as the first attempt’s task instance id.
- start_run()[source]¶
Start an agent run.
A run started while a step of this journal executes (an agent a tool calls) is numbered under that step; any other run takes the next top-level number.
- ask_model_again(number)[source]¶
Make a retry ask the model again for the final answer of top-level run
number.See
DurableRun.ask_model_again(). The top-level runs after it followed from that answer, so they are deleted: a retry starts them over.
- drop_runs_from(number)[source]¶
Delete top-level run
numberand every later one, with the runs started inside them.For runs that followed from an answer that no longer stands, such as the regenerations after a review round the reviewer answered again: a later run with that number starts over.
- property next_run_number: int[source]¶
The number the next top-level run started outside any step takes.
- airflow.providers.common.ai.durable.journal.build_task_storage(context)[source]¶
Return the journal storage for the task instance
contextbelongs to.On Airflow >= 3.3 the journal lives in the AIP-103 task state store, which handles persistence and large-value offload, so
[common.ai] durable_cache_pathis not needed. On older versions it is a JSON file under that ObjectStorage path.
- airflow.providers.common.ai.durable.journal.journal_scope(journal)[source]¶
Make
journalthe active journal for the code in thewithblock.
- airflow.providers.common.ai.durable.journal.find_active_journal()[source]¶
Return the journal already recording this run, without creating one.
A journal set with
journal_scope()wins. Otherwise, inside a running task, this returns the task attempt’s journal if something already created it.Nonemeans no journal is recording: outside a task, or inside one where nothing has used durable execution yet.
- airflow.providers.common.ai.durable.journal.current_journal()[source]¶
Return the active journal, or
Noneoutside an Airflow task.A journal set with
journal_scope()wins. Otherwise, inside a running task, this returns the task attempt’s own journal, created on first use, which deletes the steps of each run as soon as it succeeds. Outside a task there is nothing to replay into, so durable execution is off and the agent runs as it would without it. To read the journal without creating it, usefind_active_journal().