use std::io::Write;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::Duration;
use adk_core::{AdkError, Result};
use async_trait::async_trait;
use chrono::{DateTime, Utc};
#[async_trait]
pub trait TickWatermark: Send + Sync {
async fn read(&self) -> Result<Option<DateTime<Utc>>>;
async fn write(&self, cursor: DateTime<Utc>) -> Result<()>;
}
#[derive(Debug, Clone)]
pub struct FileTickWatermark {
path: PathBuf,
writing: Arc<tokio::sync::Mutex<()>>,
}
impl FileTickWatermark {
pub const PUBLISH_RETRY: Duration = Duration::from_secs(1);
pub fn new(path: impl Into<PathBuf>) -> Self {
Self { path: path.into(), writing: Arc::new(tokio::sync::Mutex::new(())) }
}
pub fn path(&self) -> &Path {
&self.path
}
}
#[async_trait]
impl TickWatermark for FileTickWatermark {
async fn read(&self) -> Result<Option<DateTime<Utc>>> {
let path = self.path.clone();
let raw = tokio::task::spawn_blocking(move || match std::fs::read_to_string(&path) {
Ok(contents) => Ok(Some(contents)),
Err(err) if err.kind() == std::io::ErrorKind::NotFound => Ok(None),
Err(err) => Err(AdkError::agent(format!(
"failed to read tick watermark at {}: {err}",
path.display()
))),
})
.await
.map_err(|err| AdkError::agent(format!("tick watermark read task failed: {err}")))??;
let Some(raw) = raw else {
return Ok(None);
};
let trimmed = raw.trim();
if trimmed.is_empty() {
return Ok(None);
}
let parsed = DateTime::parse_from_rfc3339(trimmed).map_err(|err| {
AdkError::agent(format!(
"tick watermark at {} is not an RFC 3339 timestamp: {err}",
self.path.display()
))
})?;
Ok(Some(parsed.with_timezone(&Utc)))
}
async fn write(&self, tick: DateTime<Utc>) -> Result<()> {
let path = self.path.clone();
let encoded = tick.to_rfc3339();
let _turn = self.writing.lock().await;
tokio::task::spawn_blocking(move || {
if let Some(parent) = path.parent()
&& !parent.as_os_str().is_empty()
{
std::fs::create_dir_all(parent).map_err(|err| {
AdkError::agent(format!(
"failed to create tick watermark directory {}: {err}",
parent.display()
))
})?;
}
let parent = path
.parent()
.filter(|parent| !parent.as_os_str().is_empty())
.unwrap_or_else(|| Path::new("."));
let mut temporary = tempfile::NamedTempFile::new_in(parent).map_err(|err| {
AdkError::agent(format!(
"failed to create a temporary tick watermark beside {}: {err}",
path.display()
))
})?;
temporary.write_all(encoded.as_bytes()).map_err(|err| {
AdkError::agent(format!(
"failed to write tick watermark for {}: {err}",
path.display()
))
})?;
temporary.as_file().sync_all().map_err(|err| {
AdkError::agent(format!(
"failed to synchronize tick watermark for {}: {err}",
path.display()
))
})?;
publish(temporary, &path)?;
#[cfg(unix)]
std::fs::File::open(parent).and_then(|directory| directory.sync_all()).map_err(
|err| {
AdkError::agent(format!(
"failed to synchronize tick watermark directory {}: {err}",
parent.display()
))
},
)?;
Ok(())
})
.await
.map_err(|err| AdkError::agent(format!("tick watermark write task failed: {err}")))?
}
}
fn publish(mut temporary: tempfile::NamedTempFile, path: &Path) -> Result<()> {
let started = std::time::Instant::now();
let mut pause = Duration::from_millis(10);
loop {
let error = match temporary.persist(path) {
Ok(_) => return Ok(()),
Err(error) => error,
};
let held = cfg!(windows)
&& (error.error.kind() == std::io::ErrorKind::PermissionDenied
|| error.error.raw_os_error() == Some(32));
if held && started.elapsed() + pause <= FileTickWatermark::PUBLISH_RETRY {
std::thread::sleep(pause);
pause = (pause * 2).min(Duration::from_millis(200));
temporary = error.file;
continue;
}
let remedy = if held {
format!(
"; another process kept it for {:?}: give each schedule its own watermark file",
FileTickWatermark::PUBLISH_RETRY
)
} else {
String::new()
};
return Err(AdkError::agent(format!(
"failed to publish tick watermark to {}: {}{remedy}",
path.display(),
error.error
)));
}
}
#[cfg(test)]
mod tests {
use super::*;
use chrono::TimeZone;
use std::sync::Arc;
#[tokio::test]
async fn read_returns_none_when_the_file_is_absent() {
let dir = tempfile::tempdir().expect("tempdir");
let watermark = FileTickWatermark::new(dir.path().join("missing.tick"));
assert_eq!(watermark.read().await.expect("read"), None);
}
#[tokio::test]
async fn write_then_read_round_trips_the_tick() {
let dir = tempfile::tempdir().expect("tempdir");
let watermark = FileTickWatermark::new(dir.path().join("sweep.tick"));
let tick = Utc.with_ymd_and_hms(2026, 8, 22, 13, 45, 0).unwrap();
watermark.write(tick).await.expect("write");
assert_eq!(watermark.read().await.expect("read"), Some(tick));
}
#[tokio::test]
async fn write_creates_missing_parent_directories() {
let dir = tempfile::tempdir().expect("tempdir");
let watermark = FileTickWatermark::new(dir.path().join("nested/deeper/sweep.tick"));
let tick = Utc.with_ymd_and_hms(2026, 1, 2, 3, 4, 5).unwrap();
watermark.write(tick).await.expect("write");
assert_eq!(watermark.read().await.expect("read"), Some(tick));
}
#[tokio::test]
async fn write_overwrites_a_previous_tick() {
let dir = tempfile::tempdir().expect("tempdir");
let watermark = FileTickWatermark::new(dir.path().join("sweep.tick"));
let first = Utc.with_ymd_and_hms(2026, 8, 22, 10, 0, 0).unwrap();
let second = Utc.with_ymd_and_hms(2026, 8, 22, 11, 0, 0).unwrap();
watermark.write(first).await.expect("first write");
watermark.write(second).await.expect("second write");
assert_eq!(watermark.read().await.expect("read"), Some(second));
}
#[tokio::test]
async fn concurrent_writes_use_independent_temporary_files() {
let dir = tempfile::tempdir().expect("tempdir");
let watermark = Arc::new(FileTickWatermark::new(dir.path().join("sweep.tick")));
let first = Utc.with_ymd_and_hms(2026, 8, 22, 10, 0, 0).unwrap();
let second = Utc.with_ymd_and_hms(2026, 8, 22, 11, 0, 0).unwrap();
let (first_result, second_result) =
tokio::join!(watermark.write(first), watermark.write(second));
first_result.expect("first write");
second_result.expect("second write");
let stored = watermark.read().await.expect("read").expect("stored");
assert!(stored == first || stored == second);
}
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn many_concurrent_writes_through_clones_all_land() {
let dir = tempfile::tempdir().expect("tempdir");
let watermark = FileTickWatermark::new(dir.path().join("sweep.tick"));
let ticks: Vec<_> =
(0..16).map(|hour| Utc.with_ymd_and_hms(2026, 8, 22, hour, 0, 0).unwrap()).collect();
let writes = ticks.iter().map(|tick| {
let watermark = watermark.clone();
let tick = *tick;
tokio::spawn(async move { watermark.write(tick).await })
});
for write in futures::future::join_all(writes).await {
write.expect("write task").expect("write");
}
let stored = watermark.read().await.expect("read").expect("stored");
assert!(ticks.contains(&stored), "{stored}");
}
#[cfg(windows)]
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn a_write_waits_out_another_process_holding_the_file() {
use std::os::windows::fs::OpenOptionsExt as _;
let dir = tempfile::tempdir().expect("tempdir");
let path = dir.path().join("sweep.tick");
std::fs::write(&path, "2026-08-22T10:00:00+00:00").expect("seed");
let tick = Utc.with_ymd_and_hms(2026, 8, 22, 11, 0, 0).unwrap();
let held = std::fs::OpenOptions::new().read(true).share_mode(0).open(&path).expect("hold");
let release = std::thread::spawn(move || {
std::thread::sleep(Duration::from_millis(200));
drop(held);
});
FileTickWatermark::new(&path).write(tick).await.expect("write after the release");
release.join().unwrap();
assert_eq!(FileTickWatermark::new(&path).read().await.expect("read"), Some(tick));
let _held = std::fs::OpenOptions::new().read(true).share_mode(0).open(&path).expect("hold");
let started = std::time::Instant::now();
let error = FileTickWatermark::new(&path).write(tick).await.unwrap_err();
assert!(started.elapsed() < FileTickWatermark::PUBLISH_RETRY * 3, "the retry is bounded");
assert!(error.to_string().contains("its own watermark file"), "{error}");
}
#[tokio::test]
async fn read_treats_an_empty_file_as_no_watermark() {
let dir = tempfile::tempdir().expect("tempdir");
let path = dir.path().join("empty.tick");
std::fs::write(&path, " \n").expect("seed");
let watermark = FileTickWatermark::new(path);
assert_eq!(watermark.read().await.expect("read"), None);
}
#[tokio::test]
async fn read_rejects_a_corrupt_watermark() {
let dir = tempfile::tempdir().expect("tempdir");
let path = dir.path().join("corrupt.tick");
std::fs::write(&path, "not-a-timestamp").expect("seed");
let watermark = FileTickWatermark::new(path);
assert!(watermark.read().await.is_err());
}
}