use std::collections::HashMap;
use std::future::Future;
use std::sync::Arc;
use std::time::Duration;
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use reqwest::StatusCode;
use rusty_agent_runtime::durable::ErrorClass;
use rusty_agent_runtime::error::{Result, RustyError};
use rusty_agent_runtime::record::EffectReceipt;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use tokio_util::sync::CancellationToken;
use tracing::Instrument;
use uuid::Uuid;
pub const DEFAULT_LEASE: Duration = Duration::from_secs(30);
pub const DEFAULT_REQUEST_TIMEOUT: Duration = Duration::from_secs(10);
pub const DEFAULT_CLAIM_BACKOFF_BASE: Duration = Duration::from_millis(100);
pub const DEFAULT_CLAIM_BACKOFF_MAX: Duration = Duration::from_secs(5);
pub const DEFAULT_DRAIN_GRACE: Duration = Duration::from_secs(25);
const MIN_LEASE: Duration = Duration::from_millis(100);
const MAX_LEASE: Duration = Duration::from_secs(3_600);
#[derive(Debug, Clone)]
pub struct ActivityContext {
task_id: String,
kind: String,
attempt: u32,
idempotency_key: Option<String>,
payload: Value,
}
impl ActivityContext {
pub fn task_id(&self) -> &str {
&self.task_id
}
pub fn kind(&self) -> &str {
&self.kind
}
pub fn attempt(&self) -> u32 {
self.attempt
}
pub fn idempotency_key(&self) -> Option<&str> {
self.idempotency_key.as_deref()
}
pub fn payload(&self) -> &Value {
&self.payload
}
}
#[async_trait]
pub trait Activity: Send + Sync {
async fn run(&self, ctx: ActivityContext) -> Result<Value>;
async fn run_with_receipt(&self, ctx: ActivityContext) -> Result<ActivityCompletion> {
Ok(ActivityCompletion {
result: self.run(ctx).await?,
receipt: None,
})
}
}
#[derive(Debug, Clone)]
pub struct ActivityCompletion {
pub result: Value,
pub receipt: Option<EffectReceipt>,
}
#[async_trait]
impl<F, Fut> Activity for F
where
F: Fn(ActivityContext) -> Fut + Send + Sync,
Fut: Future<Output = Result<Value>> + Send,
{
async fn run(&self, ctx: ActivityContext) -> Result<Value> {
(self)(ctx).await
}
}
#[async_trait]
impl Activity for Arc<dyn Activity> {
async fn run(&self, ctx: ActivityContext) -> Result<Value> {
self.as_ref().run(ctx).await
}
async fn run_with_receipt(&self, ctx: ActivityContext) -> Result<ActivityCompletion> {
self.as_ref().run_with_receipt(ctx).await
}
}
#[derive(Debug, Clone, Deserialize)]
pub struct ClaimedTask {
pub task_id: String,
pub kind: String,
#[serde(default)]
pub payload: Value,
#[serde(default)]
pub attempt: u32,
#[serde(default)]
pub max_attempts: u32,
#[serde(default)]
pub idempotency_key: Option<String>,
#[serde(default)]
pub run_id: Option<String>,
#[serde(default)]
pub thread_id: Option<String>,
#[serde(default)]
pub cancel_requested: bool,
#[serde(default)]
pub deadline: Option<DateTime<Utc>>,
}
#[derive(Debug, Serialize)]
struct ClaimRequest<'a> {
worker_id: &'a str,
#[serde(skip_serializing_if = "Option::is_none")]
pools: Option<&'a [String]>,
#[serde(skip_serializing_if = "Option::is_none")]
worker_version: Option<&'a str>,
lease_ms: u64,
}
#[derive(Debug, Deserialize)]
struct ClaimResponse {
task: ClaimedTask,
}
#[derive(Debug, Serialize)]
struct HeartbeatRequest<'a> {
worker_id: &'a str,
lease_ms: u64,
}
#[derive(Debug, Deserialize)]
struct HeartbeatResponse {
lease_expires_at: Option<String>,
#[serde(default)]
cancel_requested: bool,
}
#[derive(Debug, Serialize)]
struct CompleteRequest<'a> {
worker_id: &'a str,
result: &'a Value,
#[serde(skip_serializing_if = "Option::is_none")]
receipt: Option<&'a EffectReceipt>,
}
#[derive(Debug, Serialize)]
struct FailRequest<'a> {
worker_id: &'a str,
error_class: ErrorClass,
message: &'a str,
retryable: bool,
}
#[derive(Debug, Deserialize)]
struct FailResponse {
requeued: bool,
next_attempt_at: Option<String>,
dead: bool,
}
fn classify_error(error: &RustyError) -> (ErrorClass, bool) {
match error {
RustyError::Llm(_) => (ErrorClass::DependencyFailure, true),
RustyError::Tool(_) => (ErrorClass::DependencyFailure, true),
RustyError::Node(_) => (ErrorClass::Unknown, false),
RustyError::Graph(_) => (ErrorClass::InvalidInput, false),
RustyError::InvalidUpdate(_) => (ErrorClass::InvalidInput, false),
RustyError::Checkpoint(_) => (ErrorClass::Unknown, false),
RustyError::Replay(_) => (ErrorClass::Unknown, false),
RustyError::Serialization(_) => (ErrorClass::Unknown, false),
RustyError::Interrupt { .. } => (ErrorClass::Cancelled, false),
RustyError::Cancelled(_) => (ErrorClass::Cancelled, false),
}
}
enum ClaimOutcome {
Task(Box<ClaimedTask>),
Empty,
Unavailable,
}
enum AttemptOutcome {
Finished(std::result::Result<Result<ActivityCompletion>, tokio::task::JoinError>),
LeaseLost,
Cancelled(&'static str),
DrainGraceExpired,
}
async fn sleep_until_deadline(until: Option<Duration>) {
match until {
Some(delay) => tokio::time::sleep(delay).await,
None => std::future::pending().await,
}
}
pub struct ActivityWorker {
handlers: HashMap<String, Arc<dyn Activity>>,
base_url: String,
worker_id: String,
client: reqwest::Client,
lease: Duration,
pools: Vec<String>,
worker_version: Option<String>,
claim_backoff_base: Duration,
claim_backoff_max: Duration,
drain_grace: Duration,
}
impl ActivityWorker {
pub fn new(base_url: impl Into<String>) -> Self {
Self {
handlers: HashMap::new(),
base_url: base_url.into().trim_end_matches('/').to_owned(),
worker_id: format!("worker-{}", Uuid::new_v4()),
client: reqwest::Client::builder()
.timeout(DEFAULT_REQUEST_TIMEOUT)
.build()
.expect("reqwest client builder with rustls must succeed"),
lease: DEFAULT_LEASE,
pools: Vec::new(),
worker_version: None,
claim_backoff_base: DEFAULT_CLAIM_BACKOFF_BASE,
claim_backoff_max: DEFAULT_CLAIM_BACKOFF_MAX,
drain_grace: DEFAULT_DRAIN_GRACE,
}
}
pub fn register<A>(mut self, kind: impl Into<String>, activity: A) -> Self
where
A: Activity + 'static,
{
self.handlers.insert(kind.into(), Arc::new(activity));
self
}
pub fn with_worker_id(mut self, worker_id: impl Into<String>) -> Self {
self.worker_id = worker_id.into();
self
}
pub fn with_lease(mut self, lease: Duration) -> Self {
self.lease = lease.clamp(MIN_LEASE, MAX_LEASE);
self
}
pub fn with_pools<I, S>(mut self, pools: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.pools = pools.into_iter().map(Into::into).collect();
self
}
pub fn with_worker_version(mut self, version: impl Into<String>) -> Self {
self.worker_version = Some(version.into());
self
}
pub fn with_claim_backoff(mut self, base: Duration, max: Duration) -> Self {
self.claim_backoff_base = base;
self.claim_backoff_max = max;
self
}
pub fn with_drain_grace(mut self, grace: Duration) -> Self {
self.drain_grace = grace;
self
}
pub fn worker_id(&self) -> &str {
&self.worker_id
}
pub async fn run(self, shutdown: CancellationToken) {
tracing::info!(
worker_id = %self.worker_id,
base_url = %self.base_url,
lease_ms = self.lease_ms(),
pools = ?self.pools,
worker_version = %self.worker_version.as_deref().unwrap_or(""),
kinds = ?self.registered_kinds(),
drain_grace_ms = self.drain_grace.as_millis() as u64,
"activity worker started"
);
let mut backoff = self.claim_backoff_base;
loop {
let claim = tokio::select! {
_ = shutdown.cancelled() => break,
outcome = self.claim() => outcome,
};
match claim {
ClaimOutcome::Task(task) => {
backoff = self.claim_backoff_base;
self.run_activity(*task, &shutdown).await;
}
ClaimOutcome::Empty | ClaimOutcome::Unavailable => {
let delay = backoff;
backoff = backoff
.checked_mul(2)
.unwrap_or(self.claim_backoff_max)
.min(self.claim_backoff_max);
tokio::select! {
_ = tokio::time::sleep(delay) => {}
_ = shutdown.cancelled() => {}
}
}
}
}
tracing::info!(worker_id = %self.worker_id, "activity worker drained and stopped");
}
fn registered_kinds(&self) -> Vec<&str> {
let mut kinds: Vec<&str> = self.handlers.keys().map(String::as_str).collect();
kinds.sort_unstable();
kinds
}
fn lease_ms(&self) -> u64 {
u64::try_from(self.lease.as_millis()).unwrap_or(u64::MAX)
}
async fn claim(&self) -> ClaimOutcome {
let body = ClaimRequest {
worker_id: &self.worker_id,
pools: if self.pools.is_empty() {
None
} else {
Some(self.pools.as_slice())
},
worker_version: self.worker_version.as_deref(),
lease_ms: self.lease_ms(),
};
let response = match self
.client
.post(format!("{}/tasks/claim", self.base_url))
.json(&body)
.send()
.await
{
Ok(response) => response,
Err(e) => {
tracing::warn!(error = %e, "claim request failed");
return ClaimOutcome::Unavailable;
}
};
match response.status() {
StatusCode::OK => match response.json::<ClaimResponse>().await {
Ok(claimed) => ClaimOutcome::Task(Box::new(claimed.task)),
Err(e) => {
tracing::warn!(error = %e, "claim returned an undecodable task body");
ClaimOutcome::Unavailable
}
},
StatusCode::NO_CONTENT => ClaimOutcome::Empty,
status => {
tracing::warn!(%status, "claim rejected by server");
ClaimOutcome::Unavailable
}
}
}
async fn run_activity(&self, task: ClaimedTask, shutdown: &CancellationToken) {
let span = tracing::info_span!(
"rusty.activity",
worker_id = %self.worker_id,
task_id = %task.task_id,
kind = %task.kind,
attempt = task.attempt,
max_attempts = task.max_attempts,
run_id = %task.run_id.as_deref().unwrap_or(""),
thread_id = %task.thread_id.as_deref().unwrap_or(""),
);
async move {
let Some(handler) = self.handlers.get(&task.kind).cloned() else {
self.fail(
&task.task_id,
ErrorClass::InvalidInput,
format!(
"no activity registered for kind `{}` on this worker (registered: {:?})",
task.kind,
self.registered_kinds()
),
false,
)
.await;
return;
};
if task.cancel_requested {
tracing::warn!("claimed task was already cancel-requested; reporting cancelled");
self.fail(
&task.task_id,
ErrorClass::Cancelled,
format!("activity `{}` cancelled before dispatch", task.kind),
false,
)
.await;
return;
}
let until_deadline = match task.deadline {
Some(deadline) => match (deadline - Utc::now()).to_std() {
Ok(remaining) => Some(remaining),
Err(_) => {
tracing::warn!(%deadline, "claimed task's deadline already passed; reporting cancelled");
self.fail(
&task.task_id,
ErrorClass::Cancelled,
format!(
"activity `{}` cancelled: whole-task deadline already passed",
task.kind
),
false,
)
.await;
return;
}
},
None => None,
};
let ctx = ActivityContext {
task_id: task.task_id.clone(),
kind: task.kind.clone(),
attempt: task.attempt,
idempotency_key: task.idempotency_key.clone(),
payload: task.payload.clone(),
};
let task_id = task.task_id.clone();
let lease_lost = CancellationToken::new();
let cancel_requested = CancellationToken::new();
let heartbeat =
self.spawn_heartbeat(&task_id, lease_lost.clone(), cancel_requested.clone());
let mut handle = tokio::spawn(async move { handler.run_with_receipt(ctx).await });
let drain_grace = self.drain_grace;
let drained = shutdown.clone();
let outcome = tokio::select! {
joined = &mut handle => AttemptOutcome::Finished(joined),
_ = lease_lost.cancelled() => AttemptOutcome::LeaseLost,
_ = cancel_requested.cancelled() => AttemptOutcome::Cancelled("cancelled by the control plane"),
_ = sleep_until_deadline(until_deadline) => AttemptOutcome::Cancelled("whole-task deadline expired mid-attempt"),
_ = async {
drained.cancelled().await;
tokio::time::sleep(drain_grace).await;
} => AttemptOutcome::DrainGraceExpired,
};
heartbeat.abort();
let joined = match outcome {
AttemptOutcome::Finished(joined) => joined,
AttemptOutcome::LeaseLost => {
handle.abort();
let _ = handle.await;
tracing::warn!("lease lost; activity aborted and left for the server to reassign");
return;
}
AttemptOutcome::DrainGraceExpired => {
handle.abort();
let _ = handle.await;
tracing::warn!(
drain_grace_ms = drain_grace.as_millis() as u64,
"drain grace expired; activity aborted and left for reassignment at lease expiry"
);
return;
}
AttemptOutcome::Cancelled(reason) => {
handle.abort();
let _ = handle.await;
tracing::warn!(reason, "activity aborted");
self.fail(
&task_id,
ErrorClass::Cancelled,
format!("activity `{}` {reason}", task.kind),
false,
)
.await;
return;
}
};
match joined {
Ok(Ok(completion)) => {
tracing::info!(receipt = completion.receipt.is_some(), "activity succeeded");
self.complete(&task_id, &completion.result, completion.receipt.as_ref())
.await;
}
Ok(Err(e)) => {
let (error_class, retryable) = classify_error(&e);
tracing::warn!(error = %e, ?error_class, retryable, "activity failed");
self.fail(
&task_id,
error_class,
format!("activity `{}` failed: {e}", task.kind),
retryable,
)
.await;
}
Err(join_err) => {
let detail = if join_err.is_panic() {
let payload = join_err.into_panic();
let message = payload
.downcast_ref::<&str>()
.map(|s| (*s).to_owned())
.or_else(|| payload.downcast_ref::<String>().cloned())
.unwrap_or_else(|| "non-string panic payload".to_owned());
format!("handler panicked: {message}")
} else {
"handler task cancelled".to_owned()
};
tracing::warn!(%detail, "activity panicked");
self.fail(
&task_id,
ErrorClass::Unknown,
format!("activity `{}` {detail}", task.kind),
false,
)
.await;
}
}
}
.instrument(span)
.await
}
fn spawn_heartbeat(
&self,
task_id: &str,
lease_lost: CancellationToken,
cancel_requested: CancellationToken,
) -> tokio::task::JoinHandle<()> {
let client = self.client.clone();
let url = format!("{}/tasks/{task_id}/heartbeat", self.base_url);
let worker_id = self.worker_id.clone();
let lease_ms = self.lease_ms();
let interval = (self.lease / 3).max(Duration::from_millis(1));
tokio::spawn(async move {
let mut ticker = tokio::time::interval(interval);
ticker.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
loop {
ticker.tick().await;
let body = HeartbeatRequest {
worker_id: &worker_id,
lease_ms,
};
match client.post(&url).json(&body).send().await {
Ok(response) if response.status() == StatusCode::OK => {
match response.json::<HeartbeatResponse>().await {
Ok(renewed) => {
if renewed.cancel_requested {
tracing::warn!(
"heartbeat carried cancel_requested; aborting the attempt"
);
cancel_requested.cancel();
return;
}
let expires_at = renewed
.lease_expires_at
.unwrap_or_else(|| "<missing>".to_owned());
tracing::debug!(lease_expires_at = %expires_at, "lease renewed");
}
Err(_) => {
tracing::warn!("heartbeat reply undecodable; will retry");
}
}
}
Ok(response) if response.status() == StatusCode::CONFLICT => {
tracing::warn!("heartbeat rejected (409): lease lost");
lease_lost.cancel();
return;
}
Ok(response) => {
tracing::warn!(status = %response.status(), "heartbeat failed; will retry");
}
Err(e) => {
tracing::warn!(error = %e, "heartbeat request failed; will retry");
}
}
}
})
}
async fn complete(&self, task_id: &str, result: &Value, receipt: Option<&EffectReceipt>) {
let body = CompleteRequest {
worker_id: &self.worker_id,
result,
receipt,
};
let response = self
.client
.post(format!("{}/tasks/{task_id}/complete", self.base_url))
.json(&body)
.send()
.await;
self.log_settle("complete", response).await;
}
async fn fail(&self, task_id: &str, error_class: ErrorClass, message: String, retryable: bool) {
let body = FailRequest {
worker_id: &self.worker_id,
error_class,
message: &message,
retryable,
};
let response = self
.client
.post(format!("{}/tasks/{task_id}/fail", self.base_url))
.json(&body)
.send()
.await;
match response {
Ok(response) if response.status() == StatusCode::OK => {
match response.json::<FailResponse>().await {
Ok(outcome) => tracing::info!(
requeued = outcome.requeued,
next_attempt_at = ?outcome.next_attempt_at,
dead = outcome.dead,
"task settled as failed"
),
Err(e) => {
tracing::warn!(error = %e, "task settled as failed; reply undecodable")
}
}
}
other => self.log_settle("fail", other).await,
}
}
async fn log_settle(&self, call: &str, response: reqwest::Result<reqwest::Response>) {
match response {
Ok(response) if response.status() == StatusCode::OK => {
tracing::debug!("{call} accepted");
}
Ok(response) if response.status() == StatusCode::CONFLICT => {
tracing::warn!(
"{call} rejected (409): task already lost or settled; \
outcome dropped in favor of the server's reassignment"
);
}
Ok(response) => {
tracing::warn!(status = %response.status(), "{call} call failed");
}
Err(e) => {
tracing::error!(
error = %e,
"{call} call failed; the task returns to the queue at lease expiry"
);
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn closure_and_arc_implement_activity() {
let closure = |ctx: ActivityContext| async move { Ok(ctx.payload().clone()) };
fn assert_activity<T: Activity>(_: &T) {}
assert_activity(&closure);
let arc: Arc<dyn Activity> = Arc::new(closure);
assert_activity(&arc);
}
#[tokio::test]
async fn run_with_receipt_defaults_to_no_receipt() {
let closure = |ctx: ActivityContext| async move { Ok(json!({"echo": ctx.payload()})) };
let ctx = ActivityContext {
task_id: "t-1".into(),
kind: "k".into(),
attempt: 1,
idempotency_key: None,
payload: json!({"n": 1}),
};
let completion = closure.run_with_receipt(ctx).await.unwrap();
assert_eq!(completion.result, json!({"echo": {"n": 1}}));
assert!(completion.receipt.is_none());
}
#[tokio::test]
async fn run_with_receipt_carries_the_reported_receipt() {
struct Charger;
#[async_trait]
impl Activity for Charger {
async fn run(&self, _ctx: ActivityContext) -> Result<Value> {
unreachable!("the worker drives run_with_receipt")
}
async fn run_with_receipt(&self, ctx: ActivityContext) -> Result<ActivityCompletion> {
Ok(ActivityCompletion {
result: json!({"charged": true}),
receipt: Some(EffectReceipt {
provider: "stripe".into(),
provider_id: "ch_3PKd".into(),
idempotency_key: ctx.idempotency_key().unwrap_or_default().to_owned(),
task_id: Some(ctx.task_id().to_owned()),
effect_id: None,
}),
})
}
}
let ctx = ActivityContext {
task_id: "t-7".into(),
kind: "charge_card".into(),
attempt: 1,
idempotency_key: Some("run-9:charge:7".into()),
payload: json!({}),
};
let completion = Charger.run_with_receipt(ctx).await.unwrap();
let receipt = completion.receipt.expect("receipt reported");
assert_eq!(receipt.provider_id, "ch_3PKd");
assert_eq!(receipt.idempotency_key, "run-9:charge:7");
assert_eq!(receipt.task_id.as_deref(), Some("t-7"));
}
#[tokio::test]
async fn arc_dyn_activity_delegates_run_with_receipt() {
struct Charger;
#[async_trait]
impl Activity for Charger {
async fn run(&self, _ctx: ActivityContext) -> Result<Value> {
unreachable!("the worker drives run_with_receipt")
}
async fn run_with_receipt(&self, ctx: ActivityContext) -> Result<ActivityCompletion> {
Ok(ActivityCompletion {
result: json!({"charged": true}),
receipt: Some(EffectReceipt {
provider: "stripe".into(),
provider_id: "ch_arc".into(),
idempotency_key: ctx.idempotency_key().unwrap_or_default().to_owned(),
task_id: Some(ctx.task_id().to_owned()),
effect_id: None,
}),
})
}
}
let activity: Arc<dyn Activity> = Arc::new(Charger);
let ctx = ActivityContext {
task_id: "t-9".into(),
kind: "charge_card".into(),
attempt: 2,
idempotency_key: Some("run-1:charge:9".into()),
payload: json!({}),
};
let completion = activity.run_with_receipt(ctx).await.unwrap();
assert_eq!(completion.result, json!({"charged": true}));
let receipt = completion
.receipt
.expect("the Arc must delegate the receipt");
assert_eq!(receipt.provider_id, "ch_arc");
assert_eq!(receipt.idempotency_key, "run-1:charge:9");
}
#[test]
fn classify_error_maps_to_the_shared_taxonomy() {
assert_eq!(
classify_error(&RustyError::Llm("rate limited".into())),
(ErrorClass::DependencyFailure, true)
);
assert_eq!(
classify_error(&RustyError::Tool("backend exploded".into())),
(ErrorClass::DependencyFailure, true)
);
assert_eq!(
classify_error(&RustyError::Graph("cycle".into())),
(ErrorClass::InvalidInput, false)
);
assert_eq!(
classify_error(&RustyError::InvalidUpdate("double write".into())),
(ErrorClass::InvalidInput, false)
);
assert_eq!(
classify_error(&RustyError::Node("boom".into())),
(ErrorClass::Unknown, false)
);
assert_eq!(
classify_error(&RustyError::Checkpoint("io".into())),
(ErrorClass::Unknown, false)
);
assert_eq!(
classify_error(&RustyError::Replay("diverged".into())),
(ErrorClass::Unknown, false)
);
let serde_err = serde_json::from_str::<Value>("{").unwrap_err();
assert_eq!(
classify_error(&RustyError::Serialization(serde_err)),
(ErrorClass::Unknown, false)
);
assert_eq!(
classify_error(&RustyError::Interrupt { value: json!(null) }),
(ErrorClass::Cancelled, false)
);
}
#[test]
fn claimed_task_decodes_the_server_wire_shape() {
let body = json!({
"task": {
"task_id": "t-1",
"kind": "send_email",
"payload": {"to": "a@b.c"},
"pool": "default",
"status": "leased",
"attempt": 2,
"max_attempts": 3,
"error_class": null,
"last_error": "previous attempt blew up",
"idempotency_key": "run-9:charge:7",
"result": null,
"run_id": "run-9",
"thread_id": "thread-1",
"cancel_requested": false,
"deadline": "2026-08-07T13:00:00Z",
"lease": {"owner": "w-1", "expires_at": "2026-08-07T12:00:30Z"},
"next_attempt_at": null,
"created_at": "2026-08-07T12:00:00Z",
"updated_at": "2026-08-07T12:00:01Z"
}
});
let claimed: ClaimResponse = serde_json::from_value(body).unwrap();
let task = claimed.task;
assert_eq!(task.task_id, "t-1");
assert_eq!(task.kind, "send_email");
assert_eq!(task.payload, json!({"to": "a@b.c"}));
assert_eq!(task.attempt, 2);
assert_eq!(task.max_attempts, 3);
assert_eq!(task.idempotency_key.as_deref(), Some("run-9:charge:7"));
assert_eq!(task.run_id.as_deref(), Some("run-9"));
assert_eq!(task.thread_id.as_deref(), Some("thread-1"));
assert!(!task.cancel_requested);
assert_eq!(
task.deadline,
DateTime::parse_from_rfc3339("2026-08-07T13:00:00Z")
.ok()
.map(|dt| dt.with_timezone(&Utc))
);
}
#[test]
fn claimed_task_defaults_optional_fields() {
let task: ClaimedTask =
serde_json::from_value(json!({"task_id": "t-1", "kind": "k"})).unwrap();
assert_eq!(task.payload, Value::Null);
assert_eq!(task.attempt, 0);
assert!(task.idempotency_key.is_none());
assert!(task.run_id.is_none() && task.thread_id.is_none());
assert!(!task.cancel_requested);
assert!(task.deadline.is_none());
}
#[test]
fn claim_request_omits_pools_when_unconfigured() {
let body = ClaimRequest {
worker_id: "w-1",
pools: None,
worker_version: None,
lease_ms: 100,
};
let value = serde_json::to_value(&body).unwrap();
assert_eq!(value, json!({"worker_id": "w-1", "lease_ms": 100}));
let pools = vec!["gpu".to_string()];
let body = ClaimRequest {
worker_id: "w-1",
pools: Some(pools.as_slice()),
worker_version: None,
lease_ms: 100,
};
let value = serde_json::to_value(&body).unwrap();
assert_eq!(
value,
json!({"worker_id": "w-1", "pools": ["gpu"], "lease_ms": 100})
);
}
#[test]
fn claim_request_carries_the_advertised_worker_version() {
let body = ClaimRequest {
worker_id: "w-1",
pools: None,
worker_version: Some("activity-worker/1.4.0"),
lease_ms: 100,
};
let value = serde_json::to_value(&body).unwrap();
assert_eq!(
value,
json!({"worker_id": "w-1", "worker_version": "activity-worker/1.4.0", "lease_ms": 100})
);
}
#[test]
fn settle_request_shapes_match_the_contract() {
let heartbeat = HeartbeatRequest {
worker_id: "w-1",
lease_ms: 100,
};
assert_eq!(
serde_json::to_value(&heartbeat).unwrap(),
json!({"worker_id": "w-1", "lease_ms": 100})
);
let result = json!({"sent": true});
let complete = CompleteRequest {
worker_id: "w-1",
result: &result,
receipt: None,
};
assert_eq!(
serde_json::to_value(&complete).unwrap(),
json!({"worker_id": "w-1", "result": {"sent": true}})
);
let receipt = EffectReceipt {
provider: "stripe".into(),
provider_id: "ch_3PKd".into(),
idempotency_key: "run-9:charge:7".into(),
task_id: Some("t-1".into()),
effect_id: None,
};
let complete = CompleteRequest {
worker_id: "w-1",
result: &result,
receipt: Some(&receipt),
};
assert_eq!(
serde_json::to_value(&complete).unwrap(),
json!({
"worker_id": "w-1",
"result": {"sent": true},
"receipt": {
"provider": "stripe",
"provider_id": "ch_3PKd",
"idempotency_key": "run-9:charge:7",
"task_id": "t-1"
}
})
);
let fail = FailRequest {
worker_id: "w-1",
error_class: ErrorClass::DependencyFailure,
message: "backend exploded",
retryable: true,
};
assert_eq!(
serde_json::to_value(&fail).unwrap(),
json!({
"worker_id": "w-1",
"error_class": "dependency_failure",
"message": "backend exploded",
"retryable": true
})
);
}
#[test]
fn heartbeat_and_fail_responses_decode() {
let heartbeat: HeartbeatResponse =
serde_json::from_value(json!({"lease_expires_at": "2026-08-07T12:00:30Z"})).unwrap();
assert_eq!(
heartbeat.lease_expires_at.as_deref(),
Some("2026-08-07T12:00:30Z")
);
assert!(!heartbeat.cancel_requested);
let heartbeat: HeartbeatResponse = serde_json::from_value(
json!({"lease_expires_at": "2026-08-07T12:00:30Z", "cancel_requested": true}),
)
.unwrap();
assert!(heartbeat.cancel_requested);
let fail: FailResponse = serde_json::from_value(json!({
"requeued": true,
"next_attempt_at": "2026-08-07T12:01:00Z",
"dead": false
}))
.unwrap();
assert!(fail.requeued && !fail.dead);
assert_eq!(
fail.next_attempt_at.as_deref(),
Some("2026-08-07T12:01:00Z")
);
}
#[test]
fn lease_is_clamped_to_the_server_bounds() {
let worker = ActivityWorker::new("http://127.0.0.1:1").with_lease(Duration::from_millis(5));
assert_eq!(worker.lease, MIN_LEASE);
let worker =
ActivityWorker::new("http://127.0.0.1:1").with_lease(Duration::from_secs(86_400));
assert_eq!(worker.lease, MAX_LEASE);
}
}