Expand description
Retention for the persistent task stores.
§The settled policy
A persistent store deletes nothing unless the operator asks it to.
That is a decision, not an omission, and it is the opposite of what the
in-memory store does. TaskStoreConfig defaults
to a one-hour TTL and a 10,000-task cap, so the default in-process
deployment forgets a task an hour after it finishes. SqliteTaskStore and
PostgresTaskStore never read that config — they took a URL and nothing
else — so the durable deployment kept every task forever. Two opposite
behaviours, neither written down where an operator would look, and the
divergence was the actual defect: not that the table grows, but that
nothing said it would.
Forgetting is right for a cache and wrong for a database. A library that
quietly deleted rows from an operator’s PostgreSQL would be a far worse
surprise than one that grows, and “how long do we keep completed work” is a
question with legal answers, not just engineering ones — retention
schedules, audit obligations, and the customer’s own contracts all have a
say. So the default stays “keep everything”, and this module is the
mechanism for any other answer.
§Using it
purge_expired on each persistent store deletes terminal
tasks older than RetentionPolicy::terminal_max_age, in batches, and
reports what it did. Call it from whatever already schedules work —
a cron, a Kubernetes CronJob, a tokio::spawn loop. It is deliberately
not wired to a timer inside the store: a sweep that fires on its own is a
sweep that fires during your traffic peak, and the store does not know when
that is.
The example is gated on sqlite because the type it needs is: this crate
has no default features, so a doctest naming SqliteTaskStore
unconditionally fails to compile in every build that does not ask for a
backend — which is most of the CI matrix.
use a2a_protocol_server::store::{RetentionPolicy, SqliteTaskStore};
use a2a_protocol_types::error::A2aResult;
use std::time::Duration;
pub async fn sweep(store: &SqliteTaskStore) -> A2aResult<()> {
let policy = RetentionPolicy::new(Duration::from_secs(30 * 24 * 3600));
let report = store.purge_expired(&policy).await?;
println!("purged {} task(s)", report.tasks_deleted);
Ok(())
}§What it will not delete
Terminal tasks — Completed, Failed, Canceled, Rejected — and,
only when the policy says so, parked ones.
A task that is still Working is never eligible, however old it is. One
parked in InputRequired or AuthRequired is a workflow waiting on
someone, not a leak, and by default it is never eligible either: the
in-memory store does evict non-terminal tasks as a last resort under
capacity pressure, because RAM is a hard bound, but disk is not.
An operator whose parked tasks are abandoned by the people they wait on can
opt in with RetentionPolicy::with_parked_max_age (ADR 0056, which
amends ADR 0011). A parked task whose last update is older than that is
then deleted, with its event log, as a terminal one is: a store cannot
write the cancellation a handler would (the event, the push notification,
the audit record), so it does not pretend to. A client that answers it
afterwards is told the task was not found; a stream open on it ends
without a terminal state. A task that a turn is running on right now (a
live claim) is skipped, whatever its age. The count is
PurgeReport::parked_tasks_deleted.
Structs§
- Purge
Report - What one call to
purge_expireddid. - Retention
Policy - How long terminal tasks are kept, and how aggressively they are removed.
Constants§
- DEFAULT_
IDEMPOTENCY_ KEY_ MAX_ AGE - How long an idempotency key is kept when nothing else is asked for.
Functions§
- terminal_
states - The states a purge is allowed to delete.