use std::fs::{self, OpenOptions};
use std::io::{BufRead, BufReader, Write};
use std::path::{Path, PathBuf};
use std::time::{SystemTime, UNIX_EPOCH};
use anyhow::Result;
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
use crate::ipc::{Agent, Tier};
pub fn now_ms() -> u64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_millis() as u64)
.unwrap_or(0)
}
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum Status {
Queued,
Running,
Idle,
Waiting,
Cancelled,
Failed,
Interrupted,
}
impl Status {
pub fn is_final(self) -> bool {
self == Status::Cancelled
}
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct Fingerprint {
pub path: PathBuf,
pub len: u64,
pub modified_ms: u64,
}
impl Fingerprint {
pub fn of(path: &Path) -> Option<Fingerprint> {
let m = fs::metadata(path).ok()?;
let modified_ms = m
.modified()
.ok()?
.duration_since(UNIX_EPOCH)
.ok()?
.as_millis() as u64;
Some(Fingerprint {
path: path.to_path_buf(),
len: m.len(),
modified_ms,
})
}
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Task {
pub id: String,
pub name: Option<String>,
pub parent: String,
pub agent: Agent,
pub tier: Tier,
pub cwd: PathBuf,
pub model: Option<String>,
pub effort: Option<String>,
pub timeout_secs: Option<u64>,
pub status: Status,
pub session: Option<String>,
pub session_file: Option<Fingerprint>,
pub turns: u32,
pub delivered: u32,
pub changed_files: Vec<String>,
#[serde(default)]
pub questions: Vec<Question>,
pub stalled: bool,
pub detached: bool,
pub created_ms: u64,
pub updated_ms: u64,
}
impl Task {
pub fn open_questions(&self) -> impl Iterator<Item = &Question> {
self.questions.iter().filter(|q| q.answer.is_none())
}
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Question {
pub id: String,
pub text: String,
pub answer: Option<String>,
pub asked_ms: u64,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Event {
pub seq: u64,
pub ts: u64,
pub task: String,
pub kind: String,
#[serde(flatten)]
pub data: Map<String, Value>,
}
pub fn deliverable(kind: &str) -> bool {
matches!(
kind,
"turn_ended"
| "question"
| "approval_request"
| "message"
| "stalled"
| "failed"
| "cancelled"
| "interrupted"
| "detached"
)
}
pub struct Store {
root: PathBuf,
}
impl Store {
pub fn new(home: &Path) -> Store {
Store {
root: home.join("tasks"),
}
}
fn dir(&self, id: &str) -> PathBuf {
self.root.join(id)
}
pub fn exists(&self, id: &str) -> bool {
self.dir(id).exists()
}
pub fn remove(&self, id: &str) -> Result<()> {
fs::remove_dir_all(self.dir(id))?;
Ok(())
}
pub fn save(&self, task: &Task) -> Result<()> {
let dir = self.dir(&task.id);
fs::create_dir_all(dir.join("turns"))?;
let tmp = dir.join("task.json.tmp");
fs::write(&tmp, serde_json::to_vec_pretty(task)?)?;
fs::rename(tmp, dir.join("task.json"))?;
Ok(())
}
pub fn append(&self, event: &Event) -> Result<()> {
let mut f = OpenOptions::new()
.create(true)
.append(true)
.open(self.dir(&event.task).join("events.jsonl"))?;
let mut line = serde_json::to_vec(event)?;
line.push(b'\n');
f.write_all(&line)?;
Ok(())
}
pub fn turn_path(&self, id: &str, turn: u32) -> PathBuf {
self.dir(id).join("turns").join(format!("{turn}.md"))
}
pub fn text_path(&self, id: &str, kind: &str, name: &str) -> PathBuf {
self.dir(id).join(kind).join(format!("{name}.txt"))
}
pub fn save_turn(&self, id: &str, turn: u32, text: &str) -> Result<PathBuf> {
let path = self.turn_path(id, turn);
fs::write(&path, text)?;
Ok(path)
}
pub fn events(&self, id: &str) -> Vec<Event> {
let Ok(f) = fs::File::open(self.dir(id).join("events.jsonl")) else {
return Vec::new();
};
BufReader::new(f)
.lines()
.map_while(|l| l.ok())
.filter_map(|l| serde_json::from_str(&l).ok())
.collect()
}
pub fn load_all(&self) -> Vec<Task> {
let Ok(entries) = fs::read_dir(&self.root) else {
return Vec::new();
};
entries
.filter_map(|e| e.ok())
.filter_map(|e| fs::read(e.path().join("task.json")).ok())
.filter_map(|b| serde_json::from_slice(&b).ok())
.collect()
}
}
#[derive(Serialize, Deserialize, Default)]
pub struct Ledger {
pub cursors: std::collections::HashMap<String, u64>,
#[serde(default)]
pub codex_hosts: std::collections::HashMap<String, CodexHost>,
#[serde(default, skip_serializing_if = "std::collections::HashMap::is_empty")]
pub pending_pushes: std::collections::HashMap<String, PendingPush>,
#[serde(default, skip_serializing_if = "std::collections::HashMap::is_empty")]
pub doorbells: std::collections::HashMap<String, String>,
#[serde(default)]
pub seq: u64,
}
#[derive(Serialize, Deserialize, Clone)]
pub struct PendingPush {
pub marker: String,
pub events: Vec<(String, u64)>,
pub turns: Vec<(String, u32)>,
#[serde(default)]
pub approvals: Vec<String>,
}
#[derive(Serialize, Deserialize, Clone)]
pub struct CodexHost {
pub exe: PathBuf,
pub codex_home: Option<PathBuf>,
pub thread: String,
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub doorbell: bool,
}
impl Ledger {
pub fn load(home: &Path) -> Ledger {
fs::read(home.join("ledger.json"))
.ok()
.and_then(|b| serde_json::from_slice(&b).ok())
.unwrap_or_default()
}
pub fn save(&self, home: &Path) -> Result<()> {
let tmp = home.join("ledger.json.tmp");
fs::write(&tmp, serde_json::to_vec(self)?)?;
fs::rename(tmp, home.join("ledger.json"))?;
Ok(())
}
}
pub fn new_id(prefix: char, taken: impl Fn(&str) -> bool) -> String {
const ALPHABET: &[u8] = b"abcdefghijkmnpqrstuvwxyz23456789";
for len in 4.. {
for _ in 0..8 {
let bytes = uuid::Uuid::new_v4().into_bytes();
let id: String = std::iter::once(prefix)
.chain(
bytes
.iter()
.take(len)
.map(|b| ALPHABET[(*b as usize) % ALPHABET.len()] as char),
)
.collect();
if !taken(&id) {
return id;
}
}
}
unreachable!()
}