Files
clawmates/crates/cm-db/src/repo/routines.rs
T
Omar SobhandClaude Fable 5 add4f79fed Rebrand: TeamClaw -> Clawmates (clawmates.work)
Full-depth rename per the approved plan; the 'claw' product vocabulary
(claws, /claws routes, clawId, Claw Chat) stays — it is now the brand.

- Display brand: Clawmates (manifest, titles, hero, login/rail logo
  'clawmates'); default host app.clawmates.work; registry
  ghcr.io/clawmates
- Crates tc-* -> cm-* (16 crates + all imports); binaries
  clawmates-server/broker/bundler; images clawmates/*; env prefix
  CLAWMATES_* (+ CM_TEST_DATABASE_URL / CM_LIVE_LLM); config
  clawmates.toml; helm chart deploy/helm/clawmates with clawmates-*
  resources; db names clawmates*; sockets /run/clawmates; cookie
  cm_session; kind cluster clawmates-test; seccomp node profile
  clawmates-agent-profile.json
- All 9 Playwright brand assertions updated in lockstep; historical
  spec document left untouched as the only remaining 'TeamClaw'
- Local env migrated: dev pg clawmates-dev-pg/clawmates_dev, shared
  test server clawmates-test-pg, kind cluster recreated with image +
  profile, compose images rebuilt under clawmates/*

Verified end to end: 161 Rust + 68 frontend tests, 29 Playwright
journeys, 4 live kind tests, helm/install/LOC/placeholder gates, and
the clean-room install rehearsal serving the clawmates login page from
a signed bundle of the rebuilt images.

Co-Authored-By: Claude Fable 5 <[email protected]>
2026-06-10 12:31:25 -05:00

107 lines
3.0 KiB
Rust

use cm_domain::AgentId;
use sqlx::PgPool;
use time::OffsetDateTime;
use uuid::Uuid;
use crate::DbError;
/// A scheduled task an agent runs on a cron cadence (§7.6).
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct Routine {
pub id: Uuid,
pub agent_id: Uuid,
pub name: String,
pub schedule_cron: String,
/// `{"message": "..."}` — the prompt sent into the routine's session.
pub action: serde_json::Value,
pub status: String,
#[serde(with = "time::serde::rfc3339::option")]
pub next_run_at: Option<OffsetDateTime>,
#[serde(with = "time::serde::rfc3339::option")]
pub last_run_at: Option<OffsetDateTime>,
}
pub async fn create(
pool: &PgPool,
agent_id: AgentId,
name: &str,
schedule_cron: &str,
action: serde_json::Value,
next_run_at: OffsetDateTime,
) -> Result<Routine, DbError> {
let id = Uuid::now_v7();
sqlx::query!(
"INSERT INTO routines (id, agent_id, name, schedule_cron, action, next_run_at)
VALUES ($1, $2, $3, $4, $5, $6)",
id,
agent_id.as_uuid(),
name,
schedule_cron,
action,
next_run_at,
)
.execute(pool)
.await?;
Ok(Routine {
id,
agent_id: agent_id.as_uuid(),
name: name.to_owned(),
schedule_cron: schedule_cron.to_owned(),
action,
status: "active".into(),
next_run_at: Some(next_run_at),
last_run_at: None,
})
}
pub async fn list_by_agent(pool: &PgPool, agent_id: AgentId) -> Result<Vec<Routine>, DbError> {
let rows = sqlx::query_as!(
Routine,
r#"SELECT id, agent_id, name, schedule_cron, action, status,
next_run_at, last_run_at
FROM routines WHERE agent_id = $1 ORDER BY created_at"#,
agent_id.as_uuid(),
)
.fetch_all(pool)
.await?;
Ok(rows)
}
/// Claims every due routine atomically (SKIP LOCKED: one firing per
/// routine even with multiple scheduler replicas) and advances its clock.
/// `next_runs` computes the following occurrence per claimed routine.
pub async fn claim_due(pool: &PgPool, now: OffsetDateTime) -> Result<Vec<Routine>, DbError> {
let rows = sqlx::query_as!(
Routine,
r#"UPDATE routines SET last_run_at = $1
WHERE id IN (
SELECT id FROM routines
WHERE status = 'active' AND next_run_at IS NOT NULL
AND next_run_at <= $1
FOR UPDATE SKIP LOCKED
)
RETURNING id, agent_id, name, schedule_cron, action, status,
next_run_at, last_run_at"#,
now,
)
.fetch_all(pool)
.await?;
Ok(rows)
}
/// Schedules the next firing after a claim.
pub async fn set_next_run(
pool: &PgPool,
id: Uuid,
next_run_at: Option<OffsetDateTime>,
) -> Result<(), DbError> {
sqlx::query!(
"UPDATE routines SET next_run_at = $2 WHERE id = $1",
id,
next_run_at,
)
.execute(pool)
.await?;
Ok(())
}