use chrono::{DateTime, Datelike as _, FixedOffset, NaiveDate};
use serde_json::{Map, Value};
use crate::error::{KernelError, Result};
use crate::proc::{LedgerEntry, LedgerQuery, ObservationQuery};
pub(crate) const REGISTRY_TABLE: &str = "_adk_kernel_ledger_migrations";
#[cfg(feature = "sqlite")]
pub(crate) const SQLITE_MIGRATIONS: &[(i64, &str, &str)] = &[(
1,
"create kernel ledger, observation and pass state tables",
"\
CREATE TABLE IF NOT EXISTS adk_kernel_ledger (\
seq INTEGER PRIMARY KEY AUTOINCREMENT, \
ts_ms BIGINT NOT NULL, \
offset_s INTEGER NOT NULL, \
partition_id TEXT NOT NULL, \
agent TEXT NOT NULL, \
kind TEXT NOT NULL, \
effect TEXT, \
duration_ms BIGINT, \
subject_hash TEXT, \
meta TEXT NOT NULL, \
trace_id TEXT\
);\
CREATE INDEX IF NOT EXISTS adk_kernel_ledger_ts ON adk_kernel_ledger (ts_ms);\
CREATE INDEX IF NOT EXISTS adk_kernel_ledger_partition ON adk_kernel_ledger (partition_id, seq);\
CREATE INDEX IF NOT EXISTS adk_kernel_ledger_kind ON adk_kernel_ledger (kind, seq);\
CREATE TABLE IF NOT EXISTS adk_kernel_observations (\
id TEXT PRIMARY KEY, \
partition_id TEXT NOT NULL, \
as_of BIGINT NOT NULL, \
body TEXT NOT NULL\
);\
CREATE INDEX IF NOT EXISTS adk_kernel_observations_as_of ON adk_kernel_observations (as_of, id);\
CREATE TABLE IF NOT EXISTS adk_kernel_proc_state (\
partition_id TEXT PRIMARY KEY, \
body TEXT NOT NULL\
);",
)];
#[cfg(feature = "postgres")]
pub(crate) const POSTGRES_MIGRATIONS: &[(i64, &str, &str)] = &[(
1,
"create kernel ledger, observation and pass state tables",
"\
CREATE TABLE IF NOT EXISTS adk_kernel_ledger (\
seq BIGSERIAL PRIMARY KEY, \
ts_ms BIGINT NOT NULL, \
offset_s INTEGER NOT NULL, \
partition_id TEXT NOT NULL, \
agent TEXT NOT NULL, \
kind TEXT NOT NULL, \
effect TEXT, \
duration_ms BIGINT, \
subject_hash TEXT, \
meta TEXT NOT NULL, \
trace_id TEXT\
);\
CREATE INDEX IF NOT EXISTS adk_kernel_ledger_ts ON adk_kernel_ledger (ts_ms);\
CREATE INDEX IF NOT EXISTS adk_kernel_ledger_partition ON adk_kernel_ledger (partition_id, seq);\
CREATE INDEX IF NOT EXISTS adk_kernel_ledger_kind ON adk_kernel_ledger (kind, seq);\
CREATE TABLE IF NOT EXISTS adk_kernel_observations (\
id TEXT COLLATE \"C\" PRIMARY KEY, \
partition_id TEXT NOT NULL, \
as_of BIGINT NOT NULL, \
body TEXT NOT NULL\
);\
CREATE INDEX IF NOT EXISTS adk_kernel_observations_as_of ON adk_kernel_observations (as_of, id);\
CREATE TABLE IF NOT EXISTS adk_kernel_proc_state (\
partition_id TEXT COLLATE \"C\" PRIMARY KEY, \
body TEXT NOT NULL\
);",
)];
pub(crate) const APPEND: &str = "\
INSERT INTO adk_kernel_ledger \
(ts_ms, offset_s, partition_id, agent, kind, effect, duration_ms, subject_hash, meta, \
trace_id) \
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) RETURNING seq";
const COLUMNS: &str = "seq, ts_ms, offset_s, partition_id, agent, kind, effect, duration_ms, \
subject_hash, meta, trace_id";
pub(crate) const PUT_OBSERVATION: &str = "\
INSERT INTO adk_kernel_observations (id, partition_id, as_of, body) VALUES ($1, $2, $3, $4) \
ON CONFLICT (id) DO UPDATE SET \
partition_id = excluded.partition_id, as_of = excluded.as_of, body = excluded.body";
pub(crate) const PASS_STATE: &str =
"SELECT body FROM adk_kernel_proc_state WHERE partition_id = $1";
pub(crate) const PASS_STATES: &str = "SELECT body FROM adk_kernel_proc_state ORDER BY partition_id";
pub(crate) const PUT_PASS_STATE: &str = "\
INSERT INTO adk_kernel_proc_state (partition_id, body) VALUES ($1, $2) \
ON CONFLICT (partition_id) DO UPDATE SET body = excluded.body";
pub(crate) type Row = (
i64,
i64,
i32,
String,
String,
String,
Option<String>,
Option<i64>,
Option<String>,
String,
Option<String>,
);
pub(crate) enum Bind {
Text(String),
Int(i64),
}
pub(crate) fn list(query: &LedgerQuery) -> (String, Vec<Bind>) {
let mut conditions = Vec::new();
let mut binds = Vec::new();
let bind = |binds: &mut Vec<Bind>, value: Bind| {
binds.push(value);
format!("${}", binds.len())
};
if let Some(partition) = &query.partition {
let n = bind(&mut binds, Bind::Text(partition.as_str().to_string()));
conditions.push(format!("partition_id = {n}"));
}
if !query.kinds.is_empty() {
let names: Vec<String> =
query.kinds.iter().map(|kind| bind(&mut binds, Bind::Text(kind.clone()))).collect();
conditions.push(format!("kind IN ({})", names.join(", ")));
}
if let Some(agent) = &query.agent {
let n = bind(&mut binds, Bind::Text(agent.clone()));
conditions.push(format!("agent = {n}"));
}
if let Some(from) = query.from {
let n = bind(&mut binds, Bind::Int(from.timestamp_millis()));
conditions.push(format!("ts_ms >= {n}"));
}
if let Some(to) = query.to {
let n = bind(&mut binds, Bind::Int(to.timestamp_millis()));
conditions.push(format!("ts_ms < {n}"));
}
if let Some(after) = query.after {
let n = bind(&mut binds, Bind::Int(i64::try_from(after).unwrap_or(i64::MAX)));
conditions.push(format!("seq > {n}"));
}
let mut sql = format!("SELECT {COLUMNS} FROM adk_kernel_ledger");
if !conditions.is_empty() {
sql.push_str(" WHERE ");
sql.push_str(&conditions.join(" AND "));
}
sql.push_str(" ORDER BY seq");
if let Some(limit) = query.limit {
let n = bind(&mut binds, Bind::Int(i64::try_from(limit).unwrap_or(i64::MAX)));
sql.push_str(&format!(" LIMIT {n}"));
}
(sql, binds)
}
pub(crate) fn observations(query: &ObservationQuery) -> (String, Vec<Bind>) {
let mut conditions = Vec::new();
let mut binds = Vec::new();
if let Some(partition) = &query.partition {
binds.push(Bind::Text(partition.as_str().to_string()));
conditions.push(format!("partition_id = ${}", binds.len()));
}
if let Some(since) = query.since {
binds.push(Bind::Int(day(since)));
conditions.push(format!("as_of >= ${}", binds.len()));
}
let mut sql = "SELECT body FROM adk_kernel_observations".to_string();
if !conditions.is_empty() {
sql.push_str(" WHERE ");
sql.push_str(&conditions.join(" AND "));
}
sql.push_str(" ORDER BY as_of DESC, id");
if let Some(limit) = query.limit {
binds.push(Bind::Int(i64::try_from(limit).unwrap_or(i64::MAX)));
sql.push_str(&format!(" LIMIT ${}", binds.len()));
}
(sql, binds)
}
pub(crate) fn day(day: NaiveDate) -> i64 {
i64::from(day.num_days_from_ce())
}
pub(crate) struct Encoded {
pub(crate) ts_ms: i64,
pub(crate) offset_s: i32,
pub(crate) duration_ms: Option<i64>,
pub(crate) meta: String,
}
impl Encoded {
pub(crate) fn new(entry: &LedgerEntry) -> Result<Self> {
Ok(Self {
ts_ms: entry.ts.timestamp_millis(),
offset_s: entry.ts.offset().local_minus_utc(),
duration_ms: entry.duration_ms.map(|ms| i64::try_from(ms).unwrap_or(i64::MAX)),
meta: serde_json::to_string(&entry.meta)?,
})
}
}
pub(crate) fn decode(backend: &'static str, row: Row) -> Result<LedgerEntry> {
let (
seq,
ts_ms,
offset_s,
partition,
agent,
kind,
effect,
duration_ms,
subject_hash,
meta,
trace_id,
) = row;
let corrupt = |column: &str| KernelError::Store {
backend,
reason: format!("column `{column}` of ledger row {seq} is corrupt"),
};
let offset = FixedOffset::east_opt(offset_s).ok_or_else(|| corrupt("offset_s"))?;
let ts = DateTime::from_timestamp_millis(ts_ms).ok_or_else(|| corrupt("ts_ms"))?;
let meta: Map<String, Value> = serde_json::from_str(&meta).map_err(|_| corrupt("meta"))?;
Ok(LedgerEntry {
seq: u64::try_from(seq).map_err(|_| corrupt("seq"))?,
ts: ts.with_timezone(&offset),
partition: partition.into(),
agent,
kind,
effect,
duration_ms: duration_ms.map(|ms| u64::try_from(ms).unwrap_or(0)),
subject_hash,
meta,
trace_id,
})
}
pub(crate) fn body<T: serde::de::DeserializeOwned>(
backend: &'static str,
table: &str,
body: &str,
) -> Result<T> {
serde_json::from_str(body).map_err(|error| KernelError::Store {
backend,
reason: format!("a row of `{table}` is corrupt: {error}"),
})
}
pub(crate) fn db_error(backend: &'static str, action: &str, error: &sqlx::Error) -> KernelError {
KernelError::Store { backend, reason: format!("could not {action}: {error}") }
}
macro_rules! sql_ledger_store {
($store:ty, $backend:literal) => {
#[async_trait::async_trait]
impl $crate::proc::LedgerStore for $store {
async fn append(
&self,
mut entry: $crate::proc::LedgerEntry,
) -> $crate::error::Result<$crate::proc::LedgerEntry> {
let row = $crate::stores::ledger_sql::Encoded::new(&entry)?;
let (seq,): (i64,) = sqlx::query_as($crate::stores::ledger_sql::APPEND)
.bind(row.ts_ms)
.bind(row.offset_s)
.bind(entry.partition.as_str())
.bind(&entry.agent)
.bind(&entry.kind)
.bind(&entry.effect)
.bind(row.duration_ms)
.bind(&entry.subject_hash)
.bind(row.meta)
.bind(&entry.trace_id)
.fetch_one(&self.pool)
.await
.map_err(|e| {
$crate::stores::ledger_sql::db_error($backend, "append to the ledger", &e)
})?;
entry.seq = u64::try_from(seq).unwrap_or_default();
Ok(entry)
}
async fn list(
&self,
query: &$crate::proc::LedgerQuery,
) -> $crate::error::Result<Vec<$crate::proc::LedgerEntry>> {
let (sql, binds) = $crate::stores::ledger_sql::list(query);
let mut statement =
sqlx::query_as::<_, $crate::stores::ledger_sql::Row>(sqlx::AssertSqlSafe(sql));
for bind in binds {
statement = match bind {
$crate::stores::ledger_sql::Bind::Text(text) => statement.bind(text),
$crate::stores::ledger_sql::Bind::Int(int) => statement.bind(int),
};
}
let rows = statement.fetch_all(&self.pool).await.map_err(|e| {
$crate::stores::ledger_sql::db_error($backend, "read the ledger", &e)
})?;
rows.into_iter()
.map(|row| $crate::stores::ledger_sql::decode($backend, row))
.collect()
}
async fn put_observation(
&self,
observation: $crate::proc::Observation,
) -> $crate::error::Result<()> {
sqlx::query($crate::stores::ledger_sql::PUT_OBSERVATION)
.bind(&observation.id)
.bind(observation.partition.as_str())
.bind($crate::stores::ledger_sql::day(observation.as_of))
.bind(serde_json::to_string(&observation)?)
.execute(&self.pool)
.await
.map_err(|e| {
$crate::stores::ledger_sql::db_error($backend, "write an observation", &e)
})?;
Ok(())
}
async fn observations(
&self,
query: &$crate::proc::ObservationQuery,
) -> $crate::error::Result<Vec<$crate::proc::Observation>> {
let (sql, binds) = $crate::stores::ledger_sql::observations(query);
let mut statement = sqlx::query_as::<_, (String,)>(sqlx::AssertSqlSafe(sql));
for bind in binds {
statement = match bind {
$crate::stores::ledger_sql::Bind::Text(text) => statement.bind(text),
$crate::stores::ledger_sql::Bind::Int(int) => statement.bind(int),
};
}
let rows = statement.fetch_all(&self.pool).await.map_err(|e| {
$crate::stores::ledger_sql::db_error($backend, "read the observations", &e)
})?;
rows.iter()
.map(|(body,)| {
$crate::stores::ledger_sql::body($backend, "adk_kernel_observations", body)
})
.collect()
}
async fn pass_state(
&self,
partition: &$crate::partition::PartitionId,
) -> $crate::error::Result<Option<$crate::proc::PassState>> {
let row: Option<(String,)> = sqlx::query_as($crate::stores::ledger_sql::PASS_STATE)
.bind(partition.as_str())
.fetch_optional(&self.pool)
.await
.map_err(|e| {
$crate::stores::ledger_sql::db_error($backend, "read the pass state", &e)
})?;
row.map(|(body,)| {
$crate::stores::ledger_sql::body($backend, "adk_kernel_proc_state", &body)
})
.transpose()
}
async fn pass_states(&self) -> $crate::error::Result<Vec<$crate::proc::PassState>> {
let rows: Vec<(String,)> = sqlx::query_as($crate::stores::ledger_sql::PASS_STATES)
.fetch_all(&self.pool)
.await
.map_err(|e| {
$crate::stores::ledger_sql::db_error($backend, "read the pass states", &e)
})?;
rows.iter()
.map(|(body,)| {
$crate::stores::ledger_sql::body($backend, "adk_kernel_proc_state", body)
})
.collect()
}
async fn put_pass_state(
&self,
state: $crate::proc::PassState,
) -> $crate::error::Result<()> {
sqlx::query($crate::stores::ledger_sql::PUT_PASS_STATE)
.bind(state.partition.as_str())
.bind(serde_json::to_string(&state)?)
.execute(&self.pool)
.await
.map_err(|e| {
$crate::stores::ledger_sql::db_error($backend, "write the pass state", &e)
})?;
Ok(())
}
}
};
}
#[cfg(feature = "sqlite")]
#[derive(Debug, Clone)]
pub struct SqliteLedgerStore {
pool: sqlx::SqlitePool,
}
#[cfg(feature = "sqlite")]
impl SqliteLedgerStore {
pub fn new(pool: sqlx::SqlitePool) -> Self {
Self { pool }
}
pub fn pool(&self) -> &sqlx::SqlitePool {
&self.pool
}
pub async fn migrate(&self) -> Result<()> {
adk_session::migration::sqlite_runner::run_sql_migrations(
&self.pool,
REGISTRY_TABLE,
SQLITE_MIGRATIONS,
|| async { Ok(false) },
)
.await
.map_err(|error| KernelError::Store { backend: "sqlite", reason: error.message })
}
}
#[cfg(feature = "sqlite")]
sql_ledger_store!(SqliteLedgerStore, "sqlite");
#[cfg(feature = "postgres")]
#[derive(Debug, Clone)]
pub struct PostgresLedgerStore {
pool: sqlx::PgPool,
}
#[cfg(feature = "postgres")]
impl PostgresLedgerStore {
pub fn new(pool: sqlx::PgPool) -> Self {
Self { pool }
}
pub fn pool(&self) -> &sqlx::PgPool {
&self.pool
}
pub async fn connect(database_url: &str) -> Result<Self> {
let pool =
sqlx::PgPool::connect(database_url).await.map_err(|error| KernelError::Store {
backend: "postgres",
reason: format!("could not connect: {error}"),
})?;
Ok(Self::new(pool))
}
pub async fn migrate(&self) -> Result<()> {
super::postgres::migrate_locked(self.pool.clone(), REGISTRY_TABLE, POSTGRES_MIGRATIONS)
.await
}
}
#[cfg(feature = "postgres")]
sql_ledger_store!(PostgresLedgerStore, "postgres");
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn list_numbers_parameters_in_order() {
let query = LedgerQuery::default()
.partition("work")
.kind("tool_call")
.kind("intent")
.agent("harness")
.from(DateTime::from_timestamp_millis(1).unwrap())
.to(DateTime::from_timestamp_millis(2).unwrap())
.after(9)
.limit(5);
assert_eq!(
list(&query).0,
format!(
"SELECT {COLUMNS} FROM adk_kernel_ledger WHERE partition_id = $1 AND kind IN \
($2, $3) AND agent = $4 AND ts_ms >= $5 AND ts_ms < $6 AND seq > $7 ORDER BY \
seq LIMIT $8"
)
);
assert_eq!(
list(&LedgerQuery::default()).0,
format!("SELECT {COLUMNS} FROM adk_kernel_ledger ORDER BY seq")
);
let observed = ObservationQuery::default()
.partition("work")
.since(NaiveDate::from_ymd_opt(2026, 10, 1).unwrap())
.limit(2);
assert_eq!(
observations(&observed).0,
"SELECT body FROM adk_kernel_observations WHERE partition_id = $1 AND as_of >= $2 \
ORDER BY as_of DESC, id LIMIT $3"
);
}
}