use std::fs;
use std::path::{Path, PathBuf};
use clap::Parser;
use serde_json::json;
use super::super::{CmdError, CmdResult, Ctx};
use crate::envelope::Envelope;
use taskfast_agent::events::list_events_page_tolerant;
#[derive(Debug, Parser)]
pub struct PollArgs {
#[arg(long)]
pub cursor: Option<String>,
#[arg(long, default_value_t = 25)]
pub limit: i64,
}
pub async fn run(ctx: &Ctx, args: PollArgs) -> CmdResult {
let cursor_state = CursorState::resolve(args.cursor.as_deref(), ctx);
let effective_cursor = cursor_state.effective_cursor();
let client = ctx.client()?;
let page = list_events_page_tolerant(&client, effective_cursor, Some(args.limit))
.await
.map_err(CmdError::from)?;
if cursor_state.persistence_enabled() {
if let Some(path) = cursor_state.path() {
persist_cursor(path, page.meta.next_cursor.as_deref(), ctx);
}
}
for item in &page.unparseable {
tracing::warn!(
target: "taskfast::events",
raw = %item.raw,
error = %item.error,
"unparseable event surfaced via tolerant decode"
);
}
let unparseable: Vec<serde_json::Value> = page
.unparseable
.iter()
.map(|u| json!({ "raw": u.raw, "error": u.error }))
.collect();
Ok(Envelope::success(
ctx.environment,
ctx.dry_run,
json!({
"events": page.events,
"unparseable": unparseable,
"meta": page.meta,
}),
))
}
const CURSOR_FILENAME: &str = "events.cursor";
const DISABLE_SENTINEL: &str = "-";
const DISABLE_ENV: &str = "TASKFAST_NO_CURSOR_STATE";
struct CursorState {
request_cursor: Option<String>,
persist_to: Option<PathBuf>,
}
impl CursorState {
fn resolve(cursor_arg: Option<&str>, ctx: &Ctx) -> Self {
let env_disabled = std::env::var(DISABLE_ENV).is_ok_and(|v| v == "1");
let sentinel = matches!(cursor_arg, Some(s) if s == DISABLE_SENTINEL);
let persist_to = if env_disabled || sentinel {
None
} else {
cursor_path(ctx)
};
let request_cursor = match cursor_arg {
Some(s) if s == DISABLE_SENTINEL => None,
Some(s) => Some(s.to_string()),
None => persist_to.as_deref().and_then(read_cursor),
};
Self {
request_cursor,
persist_to,
}
}
fn effective_cursor(&self) -> Option<&str> {
self.request_cursor.as_deref()
}
fn persistence_enabled(&self) -> bool {
self.persist_to.is_some()
}
fn path(&self) -> Option<&Path> {
self.persist_to.as_deref()
}
}
fn cursor_path(ctx: &Ctx) -> Option<PathBuf> {
let parent = ctx.config_path.parent()?;
if parent.as_os_str().is_empty() {
return Some(PathBuf::from(CURSOR_FILENAME));
}
if parent == Path::new("/dev") {
return None;
}
Some(parent.join(CURSOR_FILENAME))
}
fn read_cursor(path: &Path) -> Option<String> {
let raw = fs::read_to_string(path).ok()?;
let trimmed = raw.trim().to_string();
if trimmed.is_empty() {
None
} else {
Some(trimmed)
}
}
fn persist_cursor(path: &Path, next_cursor: Option<&str>, ctx: &Ctx) {
let Some(payload) = next_cursor else {
return;
};
if let Some(dir) = path.parent() {
if let Err(e) = fs::create_dir_all(dir) {
warn_persist_failed(ctx, path, &e.to_string());
return;
}
}
if let Err(e) = fs::write(path, payload) {
warn_persist_failed(ctx, path, &e.to_string());
}
}
fn warn_persist_failed(ctx: &Ctx, path: &Path, reason: &str) {
if ctx.quiet {
return;
}
eprintln!(
"warning: failed to persist events cursor to {}: {reason}",
path.display()
);
}
#[cfg(test)]
mod tests {
use super::*;
use std::path::PathBuf;
fn ctx_with_config(path: PathBuf) -> Ctx {
Ctx {
config_path: path,
..Default::default()
}
}
#[test]
fn cursor_path_uses_config_parent() {
let ctx = ctx_with_config(PathBuf::from("/tmp/foo/.taskfast/config.json"));
assert_eq!(
cursor_path(&ctx),
Some(PathBuf::from("/tmp/foo/.taskfast/events.cursor"))
);
}
#[test]
fn cursor_path_skips_dev_null() {
let ctx = ctx_with_config(PathBuf::from("/dev/null"));
assert_eq!(cursor_path(&ctx), None);
}
#[test]
fn dash_sentinel_disables_both_request_and_persistence() {
let ctx = ctx_with_config(PathBuf::from("/tmp/x/.taskfast/config.json"));
let s = CursorState::resolve(Some("-"), &ctx);
assert!(!s.persistence_enabled());
assert_eq!(s.effective_cursor(), None);
}
#[test]
fn explicit_cursor_uses_passed_value() {
let ctx = ctx_with_config(PathBuf::from("/tmp/x/.taskfast/config.json"));
let s = CursorState::resolve(Some("abc"), &ctx);
assert_eq!(s.effective_cursor(), Some("abc"));
assert!(s.persistence_enabled());
}
#[test]
fn explicit_cursor_still_sent_when_persistence_unavailable() {
let ctx = ctx_with_config(PathBuf::from("/dev/null"));
let s = CursorState::resolve(Some("abc"), &ctx);
assert_eq!(s.effective_cursor(), Some("abc"));
assert!(!s.persistence_enabled());
}
#[test]
fn dev_null_config_disables_persistence() {
let ctx = ctx_with_config(PathBuf::from("/dev/null"));
let s = CursorState::resolve(None, &ctx);
assert_eq!(s.effective_cursor(), None);
assert!(!s.persistence_enabled());
}
#[test]
fn write_then_read_round_trips() {
let dir = tempfile::tempdir().unwrap();
let cfg = dir.path().join("config.json");
let ctx = ctx_with_config(cfg);
let path = cursor_path(&ctx).unwrap();
persist_cursor(&path, Some("xyz"), &ctx);
assert_eq!(read_cursor(&path), Some("xyz".into()));
}
#[test]
fn persist_none_preserves_last_known_cursor() {
let dir = tempfile::tempdir().unwrap();
let cfg = dir.path().join("config.json");
let ctx = ctx_with_config(cfg);
let path = cursor_path(&ctx).unwrap();
persist_cursor(&path, Some("abc"), &ctx);
persist_cursor(&path, None, &ctx);
assert_eq!(read_cursor(&path), Some("abc".into()));
}
}