Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

Cache absence of CI status to avoid repeated API calls

Maximilian Roos8db392854ff0
diff --git a/src/commands/config.rs b/src/commands/config.rs
index 53439b9d3..a490ae23d 100644
--- a/src/commands/config.rs
+++ b/src/commands/config.rs
@@ -687,9 +687,12 @@ pub fn handle_cache_show() -> anyhow::Result<()> {
let mut ci_lines = Vec::new();
for (branch, cached) in entries {
- let status = serde_json::to_string(&cached.status.ci_status)
- .map(|s| s.trim_matches('"').to_string())
- .unwrap_or_else(|_| "unknown".to_string());
+ let status = match &cached.status {
+ Some(pr_status) => serde_json::to_string(&pr_status.ci_status)
+ .map(|s| s.trim_matches('"').to_string())
+ .unwrap_or_else(|_| "unknown".to_string()),
+ None => "none".to_string(),
+ };
let age = now_secs.saturating_sub(cached.checked_at);
let head: String = cached.head.chars().take(8).collect();
diff --git a/src/commands/list/ci_status.rs b/src/commands/list/ci_status.rs
index 05c75e754..ee9af9336 100644
--- a/src/commands/list/ci_status.rs
+++ b/src/commands/list/ci_status.rs
@@ -229,8 +229,8 @@ pub struct PrStatus {
/// Cached CI status stored in git config
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct CachedCiStatus {
- /// The cached CI status
- pub status: PrStatus,
+ /// The cached CI status (None means no CI found for this branch)
+ pub status: Option<PrStatus>,
/// Unix timestamp when the status was fetched
pub checked_at: u64,
/// The HEAD commit SHA when the status was fetched
@@ -318,16 +318,6 @@ impl CachedCiStatus {
}
}
- /// Clear cached CI status from git config
- fn clear(branch: &str, repo_root: &str) {
- let config_key = format!("worktrunk.ci.{}", Self::escape_branch(branch));
- // Ignore errors - key may not exist, which is fine
- let _ = Command::new("git")
- .args(["config", "--unset", &config_key])
- .current_dir(repo_root)
- .output();
- }
-
/// List all cached CI statuses as (branch_name, cached_status) pairs
pub(crate) fn list_all(repo: &Repository) -> Vec<(String, Self)> {
let output = repo
@@ -432,9 +422,10 @@ impl PrStatus {
/// Returns None if no CI found or CLI tools unavailable
///
/// # Caching
- /// Results are cached in git config (`worktrunk.ci.{branch}`) for 30-60 seconds to avoid
- /// hitting GitHub API rate limits. TTL uses deterministic jitter based on repo path to
- /// spread cache expirations across concurrent statuslines. Invalidated when HEAD changes.
+ /// Results (including None) are cached in git config (`worktrunk.ci.{branch}`) for 30-60
+ /// seconds to avoid hitting GitHub API rate limits. TTL uses deterministic jitter based on
+ /// repo path to spread cache expirations across concurrent statuslines. Invalidated when
+ /// HEAD changes.
///
/// # Fork Support
/// Runs gh commands from the repository directory to enable auto-detection of
@@ -455,12 +446,13 @@ impl PrStatus {
if let Some(cached) = CachedCiStatus::read(branch, repo_root) {
if cached.is_valid(local_head, now_secs, repo_root) {
log::debug!(
- "Using cached CI status for {} (age={}s, ttl={}s)",
+ "Using cached CI status for {} (age={}s, ttl={}s, status={:?})",
branch,
now_secs - cached.checked_at,
- CachedCiStatus::ttl_for_repo(repo_root)
+ CachedCiStatus::ttl_for_repo(repo_root),
+ cached.status.as_ref().map(|s| &s.ci_status)
);
- return Some(cached.status);
+ return cached.status;
}
log::debug!(
"Cache expired for {} (age={}s, ttl={}s, head_match={})",
@@ -474,18 +466,13 @@ impl PrStatus {
// Cache miss or expired - fetch fresh status
let status = Self::detect_uncached(branch, local_head, repo_root);
- // Update cache
- if let Some(ref status) = status {
- let cached = CachedCiStatus {
- status: status.clone(),
- checked_at: now_secs,
- head: local_head.to_string(),
- };
- cached.write(branch, repo_root);
- } else {
- // Clear stale cache if no CI found
- CachedCiStatus::clear(branch, repo_root);
- }
+ // Cache the result (including None - means no CI found for this branch)
+ let cached = CachedCiStatus {
+ status: status.clone(),
+ checked_at: now_secs,
+ head: local_head.to_string(),
+ };
+ cached.write(branch, repo_root);
status
}