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:

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

log

StepKind

What a step is, for the end-of-run summary and for adapters that treat model steps specially.

Classes

Unrecordable

A step result an adapter could not encode as JSON: the live value, and why.

JournalStep

One position in a run, claimed by DurableRun.claim().

JournalStats

What one task attempt replayed and recorded, by step kind; a review counts each regeneration on its own.

DurableRun

One agent run's steps in the journal.

DurableJournal

One task attempt's view of the durable journal.

Functions

build_task_storage(context)

Return the journal storage for the task instance context belongs to.

journal_scope(journal)

Make journal the active journal for the code in the with block.

find_active_journal()

Return the journal already recording this run, without creating one.

current_journal()

Return the active journal, or None outside an Airflow task.

Module Contents

airflow.providers.common.ai.durable.journal.log[source]
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 to JournalStep.cannot_record() instead of recording it, and returns it to the adapter.

value: Any[source]
error: Exception[source]
class airflow.providers.common.ai.durable.journal.JournalStep[source]

One position in a run, claimed by DurableRun.claim().

When replayed is true, payload holds what the previous attempt recorded and the caller returns it instead of running the step. Otherwise the caller runs the step inside executing() and passes its result to record(), or the exception it raised to fail(); run() does all three.

durable_run: DurableRun[source]
position: int[source]
name: str[source]
kind: StepKind[source]
fingerprint: str | None[source]
replayable: bool = True[source]
replayed: bool = False[source]
payload: Any = None[source]
nested_runs: int = 0[source]
count_as_replayed()[source]

Count this step as replayed, once.

For a step from DurableRun.reserve(), which DurableRun.claim_reserved() does; a step from DurableRun.claim() is already counted.

async run(body, *, to_record=None)[source]

Run a step that was not replayed and record what body returns, 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 with block 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=False is never recorded.

Return type:

bool

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 error goes 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]
not_recorded: list[tuple[StepKind, str]] = [][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.

journal[source]
key[source]
diverged = False[source]
property position: int[source]

The position the next claimed step will take.

peek(position)[source]

Return the entry the previous attempt recorded at position, without claiming it.

None when nothing usable was recorded there, no earlier attempt started the run, or the run has already diverged. The entry is a dict with name, kind, fingerprint and payload.

reserve(calls, *, kind)[source]

Take a position for each of calls in order, and keep them for claim_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 earlier reserve left unclaimed, and a call whose id is already in calls takes no position. Like claim(), call it before the first await.

has_recorded(call_id)[source]

Whether the step reserve() kept for call_id holds a result an earlier attempt recorded.

claim_reserved(call_id, name, *, kind, fingerprint)[source]

Take the step reserve() kept for call_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 first await.

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 None when it cannot be fingerprinted. A step without one replays on a matching name, and only right after a step that replayed.

  • replayable (bool) – False for 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 body and record what it returns.

save(position, entry)[source]

Store entry at position; False when the storage skipped it.

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 error on 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.

cleanup()[source]

Delete this run’s steps. Call only once the run, and anything that consumes it, has succeeded.

reject(step, *, reason)[source]

Run a step that was going to replay live instead, and every step after it.

For an adapter that finds the recorded payload unusable after claim() matched it, such as one written by a version whose types have changed since.

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()). With False the 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.

storage[source]
clean_up_after_run = False[source]
fail_on_unrecordable = True[source]
stats[source]
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 default beside 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’s run_id, such as pydantic-ai-harness SpendLimits and StepPersistence, 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 number and 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.

cleanup()[source]

Delete the steps of every run in this attempt. Call only once the task’s work has succeeded.

log_summary(logger)[source]

Log what this attempt replayed and recorded, and which steps a retry would run again.

airflow.providers.common.ai.durable.journal.build_task_storage(context)[source]

Return the journal storage for the task instance context belongs 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_path is 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 journal the active journal for the code in the with block.

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. None means 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 None outside 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, use find_active_journal().

Was this entry helpful?