Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
use anyhow::{Context, Result, bail};
use chrono::{DateTime, Utc};
use chrono_tz::Tz;
use entity_registry::BotFacts;
use entity_registry_db_utils::{
InsertOutcome, NewEntityRecord, OwnedEntityRegistrar, RegisteredEntityType, WriteOutcome,
delete_entity,
};
use macro_user_id::user_id::MacroUserIdStr;
use macro_uuid::{Uuid, generate_uuid_v7};
use model_entity::EntityType;
use model_owner::Owner;
use serde_json::Value;
use sqlx::PgPool;
use std::collections::HashMap;
use std::str::FromStr;
use crate::domain::event_runs::{ClaimToken, ConfigurationRevision};
use crate::domain::event_trigger::ActionTrigger;
use crate::domain::models::{
ActionExecutionRecord, ActionKind, ActionPolicyError, AlreadyRunningError, MAX_ACTION_TIME,
RunHistory, Schedule, ScheduledAction,
};
use crate::domain::ports::ScheduledActionRepo;
#[cfg(test)]
pub(crate) mod test;
/// PostgreSQL scheduled-action store.
///
/// Create registers the recorded owner and that owner's direct grants through
/// `registrar`. Lookup, update, and delete address a row by id; `owner` is who
/// the routine runs as, not an authorization predicate.
pub struct PgScheduledActionRepo<B> {
pool: PgPool,
registrar: OwnedEntityRegistrar<B>,
}
impl<B: BotFacts> PgScheduledActionRepo<B> {
/// Persist actions in `pool`, registering each created owner through `registrar`.
pub fn new(pool: PgPool, registrar: OwnedEntityRegistrar<B>) -> Self {
Self { pool, registrar }
}
}
fn parse_timezone(s: &str) -> Result<Tz> {
Tz::from_str(s).map_err(|e| anyhow::anyhow!("invalid timezone: {e}"))
}
fn parse_kind(s: &str) -> Result<ActionKind> {
match s {
"Agent" => Ok(ActionKind::Agent),
other => bail!("unknown action kind: {other}"),
}
}
fn kind_to_str(kind: &ActionKind) -> &'static str {
match kind {
ActionKind::Agent => "Agent",
}
}
/// One mapping for every action query, including management and dispatch.
struct ActionRow {
id: Uuid,
owner: String,
name: String,
schedule: Option<String>,
kind: String,
timezone: Option<String>,
task: Value,
claimed: Option<DateTime<Utc>>,
created_at: DateTime<Utc>,
updated_at: DateTime<Utc>,
next_run_at: Option<DateTime<Utc>>,
enabled: bool,
trigger_type: String,
event_filters: Option<Value>,
trigger_config: Option<Value>,
configuration_revision: i64,
event_activated_at: Option<DateTime<Utc>>,
run_visibility: String,
template_id: Option<Uuid>,
description: String,
}
impl TryFrom<ActionRow> for ScheduledAction {
type Error = anyhow::Error;
fn try_from(row: ActionRow) -> Result<Self> {
let trigger = match row.trigger_type.as_str() {
"cron" => ActionTrigger::Cron {
schedule: Schedule::from_cron(row.schedule.context("cron schedule missing")?)?,
timezone: parse_timezone(&row.timezone.context("cron timezone missing")?)?,
},
"events" => ActionTrigger::Events {
filters: serde_json::from_value(
row.event_filters.context("event filters missing")?,
)?,
},
"multiple" | "once" => {
serde_json::from_value(row.trigger_config.context("routine triggers missing")?)?
}
other => bail!("unknown action trigger: {other}"),
};
Ok(Self {
id: Some(row.id),
owner: Owner::from_principal_str(&row.owner)?,
name: row.name,
trigger,
kind: parse_kind(&row.kind)?,
created_at: row.created_at,
updated_at: row.updated_at,
task: row.task,
claimed: row.claimed,
next_run_at: row.next_run_at,
enabled: row.enabled,
run_visibility: row.run_visibility.parse().map_err(|_| {
anyhow::anyhow!(
"unknown scheduled_action run_visibility {:?}",
row.run_visibility
)
})?,
configuration_revision: ConfigurationRevision::try_from(row.configuration_revision)?,
event_activated_at: row.event_activated_at,
template_id: row.template_id,
description: row.description,
})
}
}
struct TriggerColumns {
trigger_type: &'static str,
schedule: Option<String>,
timezone: Option<String>,
event_filters: Option<Value>,
trigger_config: Option<Value>,
}
impl TryFrom<&ActionTrigger> for TriggerColumns {
type Error = anyhow::Error;
fn try_from(trigger: &ActionTrigger) -> Result<Self> {
match trigger {
ActionTrigger::Cron { schedule, timezone } => Ok(Self {
trigger_type: "cron",
schedule: Some(schedule.as_str().to_owned()),
timezone: Some(timezone.to_string()),
event_filters: None,
trigger_config: None,
}),
ActionTrigger::Events { filters } => Ok(Self {
trigger_type: "events",
schedule: None,
timezone: None,
event_filters: Some(serde_json::to_value(filters)?),
trigger_config: None,
}),
ActionTrigger::Once { .. } => Ok(Self {
trigger_type: "once",
schedule: None,
timezone: None,
event_filters: None,
trigger_config: Some(serde_json::to_value(trigger)?),
}),
ActionTrigger::Multiple { .. } => Ok(Self {
trigger_type: "multiple",
schedule: None,
timezone: None,
event_filters: trigger
.event_filters()
.map(serde_json::to_value)
.transpose()?,
trigger_config: Some(serde_json::to_value(trigger)?),
}),
}
}
}
impl<B: BotFacts + 'static> ScheduledActionRepo for PgScheduledActionRepo<B> {
async fn create_action(&self, action: ScheduledAction) -> Result<ScheduledAction> {
let id = generate_uuid_v7();
let owner = action.owner.principal_id();
let trigger = TriggerColumns::try_from(&action.trigger)?;
let kind = kind_to_str(&action.kind);
let mut tx = self.pool.begin().await?;
let row = sqlx::query_as!(
ActionRow,
r#"
INSERT INTO scheduled_action
(id, owner, name, schedule, kind, timezone, task, next_run_at, enabled,
trigger_type, event_filters, configuration_revision, event_activated_at, trigger_config,
run_visibility, template_id, description)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17)
RETURNING id, owner, name, schedule, kind, timezone, task, claimed, created_at,
updated_at, next_run_at, enabled, trigger_type, event_filters,
configuration_revision, event_activated_at, trigger_config, run_visibility, template_id,
description
"#,
id,
owner,
action.name,
trigger.schedule,
kind,
trigger.timezone,
action.task,
action.next_run_at,
action.enabled,
trigger.trigger_type,
trigger.event_filters,
action.configuration_revision.get(),
action.event_activated_at,
trigger.trigger_config,
<&str>::from(action.run_visibility),
action.template_id,
action.description,
)
.fetch_one(&mut *tx)
.await?;
let created = ScheduledAction::try_from(row)?;
match self
.registrar
.register_owned_entity(
&mut tx,
NewEntityRecord::new(
id,
RegisteredEntityType::ScheduledAction,
created.owner.clone(),
),
)
.await
.map_err(|error| {
anyhow::anyhow!("failed to register scheduled action owner: {error:#}")
})? {
InsertOutcome::Inserted | InsertOutcome::AlreadyRegistered => {}
}
tx.commit().await?;
Ok(created)
}
async fn get_owned_actions(
&self,
owner: &MacroUserIdStr<'static>,
) -> Result<Vec<ScheduledAction>> {
let owner = owner.to_string();
let rows = sqlx::query_as!(
ActionRow,
r#"
SELECT id, owner, name, schedule, kind, timezone, task, claimed, created_at,
updated_at, next_run_at, enabled, trigger_type, event_filters,
configuration_revision, event_activated_at, trigger_config, run_visibility, template_id,
description
FROM scheduled_action
WHERE owner = $1
"#,
owner,
)
.fetch_all(&self.pool)
.await?;
rows.into_iter().map(ScheduledAction::try_from).collect()
}
async fn get_actions_by_ids(&self, ids: &[Uuid]) -> Result<Vec<ScheduledAction>> {
let rows = sqlx::query_as!(
ActionRow,
r#"
SELECT id, owner, name, schedule, kind, timezone, task, claimed, created_at,
updated_at, next_run_at, enabled, trigger_type, event_filters, trigger_config,
configuration_revision, event_activated_at, run_visibility, template_id,
description
FROM scheduled_action
WHERE id = ANY($1)
"#,
ids,
)
.fetch_all(&self.pool)
.await?;
rows.into_iter().map(ScheduledAction::try_from).collect()
}
async fn get_action(&self, id: &Uuid) -> Result<Option<ScheduledAction>> {
sqlx::query_as!(
ActionRow,
r#"
SELECT id, owner, name, schedule, kind, timezone, task, claimed, created_at,
updated_at, next_run_at, enabled, trigger_type, event_filters,
configuration_revision, event_activated_at, trigger_config, run_visibility, template_id,
description
FROM scheduled_action
WHERE id = $1
"#,
*id,
)
.fetch_optional(&self.pool)
.await?
.map(ScheduledAction::try_from)
.transpose()
}
async fn get_next_unclaimed_actions(&self, limit: i64) -> Result<Vec<ScheduledAction>> {
let stale_threshold = Utc::now() - MAX_ACTION_TIME;
let rows = sqlx::query_as!(
ActionRow,
r#"
SELECT id, owner, name, schedule, kind, timezone, task, claimed, created_at,
updated_at, next_run_at, enabled, trigger_type, event_filters,
configuration_revision, event_activated_at, trigger_config, run_visibility, template_id,
description
FROM scheduled_action
WHERE enabled AND trigger_type IN ('cron', 'once', 'multiple') AND next_run_at IS NOT NULL
AND (claimed IS NULL OR claimed < $1)
ORDER BY next_run_at ASC, id ASC
LIMIT $2
"#,
stale_threshold,
limit,
)
.fetch_all(&self.pool)
.await?;
rows.into_iter().map(ScheduledAction::try_from).collect()
}
async fn update_action(&self, action: ScheduledAction) -> Result<ScheduledAction> {
let Some(id) = action.id else {
bail!("cannot update action without id");
};
let trigger = TriggerColumns::try_from(&action.trigger)?;
let kind = kind_to_str(&action.kind);
let row = sqlx::query_as!(
ActionRow,
r#"
UPDATE scheduled_action
SET name = $1,
schedule = $2,
kind = $3,
timezone = $4,
task = $5,
next_run_at = $6,
enabled = $7,
trigger_type = $8,
event_filters = $9,
configuration_revision = $10,
event_activated_at = $11,
trigger_config = $14,
run_visibility = $15,
description = $16,
updated_at = now()
WHERE id = $12
AND configuration_revision = $10::bigint - 1
AND (
claimed IS NULL OR claimed < $13
OR (
NOT $7 AND name = $1 AND kind = $3 AND task = $5
AND trigger_type = $8
AND schedule IS NOT DISTINCT FROM $2
AND timezone IS NOT DISTINCT FROM $4
AND event_filters IS NOT DISTINCT FROM $9
AND trigger_config IS NOT DISTINCT FROM $14
AND run_visibility = $15
AND description = $16
)
)
RETURNING id, owner, name, schedule, kind, timezone, task, claimed, created_at,
updated_at, next_run_at, enabled, trigger_type, event_filters,
configuration_revision, event_activated_at, trigger_config, run_visibility, template_id,
description
"#,
action.name,
trigger.schedule,
kind,
trigger.timezone,
action.task,
action.next_run_at,
action.enabled,
trigger.trigger_type,
trigger.event_filters,
action.configuration_revision.get(),
action.event_activated_at,
id,
Utc::now() - MAX_ACTION_TIME,
trigger.trigger_config,
<&str>::from(action.run_visibility),
action.description,
)
.fetch_optional(&self.pool)
.await?
.ok_or(ActionPolicyError::UpdateConflict)?;
ScheduledAction::try_from(row)
}
async fn delete_action(&self, id: &Uuid) -> Result<()> {
let mut tx = self.pool.begin().await?;
entity_access_db_utils::delete_entity_access_rows(&mut tx, id, EntityType::ScheduledAction)
.await
.context("failed to delete scheduled action grants")?;
let deleted = sqlx::query!(
r#"
DELETE FROM scheduled_action
WHERE id = $1
"#,
*id,
)
.execute(&mut *tx)
.await?;
if deleted.rows_affected() != 0 {
match delete_entity(&mut tx, *id).await? {
WriteOutcome::Applied | WriteOutcome::NotFound => {}
}
}
tx.commit().await?;
Ok(())
}
async fn claim_action(
&self,
id: &Uuid,
revision: ConfigurationRevision,
expected_next_run_at: Option<chrono::DateTime<chrono::Utc>>,
) -> Result<ClaimToken> {
let token = ClaimToken::generate();
let now = Utc::now();
let stale_threshold = now - MAX_ACTION_TIME;
let result = sqlx::query!(
r#"
UPDATE scheduled_action
SET claimed = $1, claim_token = $4, updated_at = now()
WHERE id = $2
AND configuration_revision = $5
AND next_run_at IS NOT DISTINCT FROM $6
AND (claimed IS NULL OR claimed < $3)
"#,
now,
*id,
stale_threshold,
token.as_uuid(),
revision.get(),
expected_next_run_at,
)
.execute(&self.pool)
.await?;
if result.rows_affected() == 0 {
return Err(anyhow::Error::new(AlreadyRunningError { action_id: *id }));
}
Ok(token)
}
async fn release_action(&self, id: &Uuid, token: ClaimToken) -> Result<()> {
sqlx::query!(
r#"
UPDATE scheduled_action
SET claimed = NULL, claim_token = NULL, updated_at = now()
WHERE id = $1 AND claim_token = $2
"#,
*id,
token.as_uuid(),
)
.execute(&self.pool)
.await?;
Ok(())
}
async fn create_execution_record(&self, record: ActionExecutionRecord) -> Result<()> {
sqlx::query!(
r#"
INSERT INTO action_execution_record (action_id, resource_id, start_time, end_time, is_success, result)
VALUES ($1, $2, $3, $4, $5, $6)
"#,
record.action_id,
record.resource_id,
record.start_time,
record.end_time,
record.is_success,
record.result,
)
.execute(&self.pool)
.await?;
Ok(())
}
async fn get_execution_records(&self, action_id: &Uuid) -> Result<Vec<ActionExecutionRecord>> {
let rows = sqlx::query!(
r#"
SELECT id, action_id, resource_id, start_time, end_time, is_success, result, created_at
FROM action_execution_record
WHERE action_id = $1
ORDER BY start_time DESC
"#,
*action_id,
)
.fetch_all(&self.pool)
.await?;
Ok(rows
.into_iter()
.map(|row| ActionExecutionRecord {
id: Some(row.id),
action_id: row.action_id,
resource_id: row.resource_id,
start_time: row.start_time,
end_time: row.end_time,
is_success: row.is_success,
result: row.result,
created_at: row.created_at,
})
.collect())
}
async fn run_history(&self, action_id: &Uuid) -> Result<RunHistory> {
// Runs a trigger's condition skipped didn't run, so they don't count.
let row = sqlx::query!(
r#"
SELECT
(SELECT start_time FROM action_execution_record
WHERE action_id = $1 AND NOT (result ? 'condition')
ORDER BY start_time DESC LIMIT 1) AS previous_at,
(SELECT is_success FROM action_execution_record
WHERE action_id = $1 AND NOT (result ? 'condition')
ORDER BY start_time DESC LIMIT 1) AS previous_succeeded,
(SELECT max(start_time) FROM action_execution_record
WHERE action_id = $1 AND is_success AND NOT (result ? 'condition'))
AS last_succeeded_at
"#,
action_id,
)
.fetch_one(&self.pool)
.await?;
Ok(RunHistory {
previous: row.previous_at.zip(row.previous_succeeded),
last_succeeded_at: row.last_succeeded_at,
})
}
async fn last_runs(&self, action_ids: &[Uuid]) -> Result<HashMap<Uuid, ActionExecutionRecord>> {
// One index probe per action on (action_id, start_time DESC).
let rows = sqlx::query!(
r#"
SELECT r.id AS "id!", r.action_id AS "action_id!", r.resource_id,
r.start_time AS "start_time!", r.end_time AS "end_time!",
r.is_success AS "is_success!", r.result AS "result!",
r.created_at AS "created_at!"
FROM UNNEST($1::uuid[]) AS ids(action_id)
CROSS JOIN LATERAL (
SELECT id, action_id, resource_id, start_time, end_time, is_success,
result, created_at
FROM action_execution_record
WHERE action_execution_record.action_id = ids.action_id
ORDER BY start_time DESC
LIMIT 1
) AS r
"#,
action_ids,
)
.fetch_all(&self.pool)
.await?;
Ok(rows
.into_iter()
.map(|row| {
let record = ActionExecutionRecord {
id: Some(row.id),
action_id: row.action_id,
resource_id: row.resource_id,
start_time: row.start_time,
end_time: row.end_time,
is_success: row.is_success,
result: row.result,
created_at: row.created_at,
};
(row.action_id, record)
})
.collect())
}
async fn update_next_run_at(&self, id: &Uuid) -> Result<()> {
// Lock through the update so a concurrent trigger replacement cannot
// receive a firing time derived from the old schedule.
let mut tx = self.pool.begin().await?;
let row = sqlx::query!(
r#"
SELECT schedule, timezone, trigger_type, trigger_config
FROM scheduled_action
WHERE id = $1
FOR UPDATE
"#,
*id,
)
.fetch_one(&mut *tx)
.await?;
if matches!(row.trigger_type.as_str(), "cron" | "once" | "multiple") {
let trigger = if row.trigger_type == "cron" {
ActionTrigger::Cron {
schedule: Schedule::from_cron(row.schedule.context("cron schedule missing")?)?,
timezone: parse_timezone(&row.timezone.context("cron timezone missing")?)?,
}
} else {
serde_json::from_value(row.trigger_config.context("routine triggers missing")?)?
};
let next_run_at = trigger.next_run_after(Utc::now());
sqlx::query!(
r#"
UPDATE scheduled_action
SET next_run_at = $1, updated_at = now()
WHERE id = $2 AND trigger_type IN ('cron', 'once', 'multiple')
"#,
next_run_at,
*id,
)
.execute(&mut *tx)
.await?;
}
tx.commit().await?;
Ok(())
}
async fn update_last_executed(&self, id: &Uuid, executed_at: DateTime<Utc>) -> Result<()> {
sqlx::query!(
r#"
UPDATE scheduled_action
SET updated_at = $1
WHERE id = $2
"#,
executed_at,
*id,
)
.execute(&self.pool)
.await?;
Ok(())
}
}