use arabica_adapters::{FileSessionStore, SessionListing};
use clap::Subcommand;
#[derive(Subcommand, Debug)]
pub enum SessionsAction {
List {
#[arg(long)]
all: bool,
},
}
pub fn run(action: SessionsAction) -> i32 {
let SessionsAction::List { all } = action;
list(all)
}
fn list(all: bool) -> i32 {
let arabica_home = match arabica_adapters::default_arabica_home() {
Ok(home) => home,
Err(error) => {
eprintln!("error: {error}");
return 2;
}
};
let runner_root = match std::env::current_dir() {
Ok(root) => root,
Err(error) => {
eprintln!("error: {error}");
return 2;
}
};
let workspace_id = crate::host::workspace_id_for(&runner_root);
let scope = if all { None } else { Some(&workspace_id) };
let lines = match listing_lines(&arabica_home, scope) {
Ok(lines) => lines,
Err(error) => {
eprintln!("error: {error}");
return 1;
}
};
if lines.is_empty() {
println!("no sessions found");
return 0;
}
for line in &lines {
println!("{line}");
}
0
}
pub(crate) struct SessionEntry {
pub(crate) id: String,
pub(crate) line: String,
}
pub(crate) fn listing_lines(
arabica_home: &std::path::Path,
scope: Option<&arabica_protocol::WorkspaceId>,
) -> Result<Vec<String>, Box<dyn std::error::Error>> {
Ok(listing_entries(arabica_home, scope)?
.into_iter()
.map(|entry| entry.line)
.collect())
}
pub(crate) fn listing_entries(
arabica_home: &std::path::Path,
scope: Option<&arabica_protocol::WorkspaceId>,
) -> Result<Vec<SessionEntry>, Box<dyn std::error::Error>> {
let listings = FileSessionStore::list_sessions(arabica_home, scope)?;
let now_ms = now_ms();
Ok(listings
.into_iter()
.map(|listing| {
let id = listing.header.id.to_string();
SessionEntry {
id,
line: format_listing(&listing, scope.is_none(), now_ms),
}
})
.collect())
}
fn now_ms() -> u64 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.expect("clock is after the epoch")
.as_millis() as u64
}
fn format_listing(listing: &SessionListing, show_workspace: bool, now_ms: u64) -> String {
let last_active = relative_time(listing.modified_at_ms, now_ms);
if show_workspace {
format!(
"{:<40} {:<10} {:<20} {}",
listing.header.id, last_active, listing.header.workspace_id, listing.header.cwd
)
} else {
format!(
"{:<40} {:<10} {}",
listing.header.id, last_active, listing.header.cwd
)
}
}
fn relative_time(modified_at_ms: Option<u64>, now_ms: u64) -> String {
let Some(modified_at_ms) = modified_at_ms else {
return "-".to_owned();
};
let elapsed_secs = now_ms.saturating_sub(modified_at_ms) / 1000;
if elapsed_secs < 60 {
"just now".to_owned()
} else if elapsed_secs < 60 * 60 {
format!("{}m ago", elapsed_secs / 60)
} else if elapsed_secs < 60 * 60 * 24 {
format!("{}h ago", elapsed_secs / (60 * 60))
} else {
format!("{}d ago", elapsed_secs / (60 * 60 * 24))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn relative_time_buckets_by_the_largest_whole_unit() {
let now = 1_000_000_000u64;
let secs = |n: u64| now - n * 1000;
assert_eq!(relative_time(None, now), "-");
assert_eq!(relative_time(Some(now), now), "just now");
assert_eq!(relative_time(Some(secs(59)), now), "just now");
assert_eq!(relative_time(Some(secs(60)), now), "1m ago");
assert_eq!(relative_time(Some(secs(90)), now), "1m ago");
assert_eq!(relative_time(Some(secs(3599)), now), "59m ago");
assert_eq!(relative_time(Some(secs(3600)), now), "1h ago");
assert_eq!(relative_time(Some(secs(86_399)), now), "23h ago");
assert_eq!(relative_time(Some(secs(86_400)), now), "1d ago");
}
#[test]
fn relative_time_never_panics_on_a_modified_time_after_now() {
let now = 1_000u64;
assert_eq!(relative_time(Some(now + 5_000), now), "just now");
}
fn header(id: &str, workspace_id: &str, cwd: &str) -> arabica_adapters::SessionHeader {
arabica_adapters::SessionHeader {
schema: "structure.session/v1".to_owned(),
id: arabica_protocol::SessionId::new(id),
workspace_id: arabica_protocol::WorkspaceId::new(workspace_id),
cwd: cwd.to_owned(),
created_at_ms: 0,
protocol_version: arabica_protocol::PROTOCOL_VERSION.to_owned(),
profile: None,
instructions_sha256: None,
}
}
#[test]
fn format_listing_omits_the_workspace_column_unless_asked_for_all() {
let listing = SessionListing {
header: header("session-1", "ws-1", "/repo"),
path: "/home/sessions/ws-1/session-1.jsonl".into(),
modified_at_ms: Some(0),
};
let scoped = format_listing(&listing, false, 0);
assert!(!scoped.contains("ws-1"));
assert!(scoped.contains("session-1"));
assert!(scoped.contains("/repo"));
let all = format_listing(&listing, true, 0);
assert!(all.contains("ws-1"));
}
}