a2a_protocol_server/store/delegation_journal/
postgres.rs1use a2a_protocol_types::delegation_journal::{DelegationJournal, DelegationRecord};
20use a2a_protocol_types::error::{A2aError, A2aResult};
21use sqlx::postgres::PgPool;
22
23use super::{Fut, Row, record};
24
25const CREATE_TABLE_SQL: &str = "CREATE TABLE IF NOT EXISTS a2a_delegations (
26 id TEXT PRIMARY KEY,
27 owner TEXT NOT NULL,
28 agent TEXT NOT NULL,
29 tenant TEXT,
30 idempotency_key TEXT,
31 task_id TEXT,
32 last_event_id TEXT,
33 outcome TEXT,
34 started_at_ms BIGINT NOT NULL,
35 updated_at_ms BIGINT NOT NULL
36)";
37
38const CREATE_OPEN_INDEX_SQL: &str = "CREATE INDEX IF NOT EXISTS a2a_delegations_open
40 ON a2a_delegations (owner, started_at_ms, id) WHERE outcome IS NULL";
41
42#[derive(Debug, Clone)]
44pub struct PostgresDelegationJournal {
45 pool: PgPool,
46}
47
48impl PostgresDelegationJournal {
49 pub async fn from_pool(pool: PgPool) -> A2aResult<Self> {
57 crate::store::pg_schema::apply(&pool, &[CREATE_TABLE_SQL, CREATE_OPEN_INDEX_SQL])
58 .await
59 .map_err(|e| A2aError::internal(format!("delegation journal migrate: {e}")))?;
60 Ok(Self { pool })
61 }
62}
63
64fn failed(what: &str, e: &sqlx::Error) -> A2aError {
65 A2aError::internal(format!("delegation journal {what}: {e}"))
66}
67
68impl DelegationJournal for PostgresDelegationJournal {
69 fn begin<'a>(&'a self, rec: &'a DelegationRecord) -> Fut<'a, ()> {
70 Box::pin(async move {
71 crate::store::pg_retry::retry(&crate::metrics::NoopMetrics, || {
74 sqlx::query(
75 "INSERT INTO a2a_delegations
76 (id, owner, agent, tenant, idempotency_key, task_id,
77 last_event_id, outcome, started_at_ms, updated_at_ms)
78 VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
79 ON CONFLICT (id) DO NOTHING",
80 )
81 .bind(&rec.id)
82 .bind(&rec.owner)
83 .bind(&rec.agent)
84 .bind(&rec.tenant)
85 .bind(&rec.idempotency_key)
86 .bind(&rec.task_id)
87 .bind(&rec.last_event_id)
88 .bind(&rec.outcome)
89 .bind(rec.started_at_ms)
90 .bind(rec.updated_at_ms)
91 .execute(&self.pool)
92 })
93 .await
94 .map_err(|e| failed("begin", &e))?;
95 Ok(())
96 })
97 }
98
99 fn progress<'a>(
100 &'a self,
101 id: &'a str,
102 task_id: Option<&'a str>,
103 last_event_id: Option<&'a str>,
104 now_ms: i64,
105 ) -> Fut<'a, ()> {
106 Box::pin(async move {
107 crate::store::pg_retry::retry(&crate::metrics::NoopMetrics, || {
110 sqlx::query(
111 "UPDATE a2a_delegations
112 SET task_id = COALESCE($2, task_id),
113 last_event_id = COALESCE($3, last_event_id),
114 updated_at_ms = $4
115 WHERE id = $1 AND outcome IS NULL",
116 )
117 .bind(id)
118 .bind(task_id)
119 .bind(last_event_id)
120 .bind(now_ms)
121 .execute(&self.pool)
122 })
123 .await
124 .map_err(|e| failed("progress", &e))?;
125 Ok(())
126 })
127 }
128
129 fn settle<'a>(&'a self, id: &'a str, outcome: &'a str, now_ms: i64) -> Fut<'a, ()> {
130 Box::pin(async move {
131 crate::store::pg_retry::retry(&crate::metrics::NoopMetrics, || {
133 sqlx::query(
134 "UPDATE a2a_delegations SET outcome = $2, updated_at_ms = $3
135 WHERE id = $1 AND outcome IS NULL",
136 )
137 .bind(id)
138 .bind(outcome)
139 .bind(now_ms)
140 .execute(&self.pool)
141 })
142 .await
143 .map_err(|e| failed("settle", &e))?;
144 Ok(())
145 })
146 }
147
148 fn open<'a>(&'a self, owner: &'a str) -> Fut<'a, Vec<DelegationRecord>> {
149 Box::pin(async move {
150 let rows: Vec<Row> =
151 crate::store::pg_retry::retry(&crate::metrics::NoopMetrics, || {
152 sqlx::query_as(
153 "SELECT id, owner, agent, tenant, idempotency_key, task_id,
154 last_event_id, outcome, started_at_ms, updated_at_ms
155 FROM a2a_delegations
156 WHERE owner = $1 AND outcome IS NULL
157 ORDER BY started_at_ms, id",
158 )
159 .bind(owner)
160 .fetch_all(&self.pool)
161 })
162 .await
163 .map_err(|e| failed("open", &e))?;
164 Ok(rows.into_iter().map(record).collect())
165 })
166 }
167
168 fn purge_settled(&self, settled_before_ms: i64) -> Fut<'_, u64> {
169 Box::pin(async move {
170 let done = crate::store::pg_retry::checked(
173 sqlx::query(
174 "DELETE FROM a2a_delegations
175 WHERE outcome IS NOT NULL AND updated_at_ms < $1",
176 )
177 .bind(settled_before_ms)
178 .execute(&self.pool),
179 )
180 .await
181 .map_err(|e| failed("purge", &e))?;
182 Ok(done.rows_affected())
183 })
184 }
185}