Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/runbox-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub use record::{Record, RecordCommand, RecordGitState, RecordValidationError};
pub use result::{Artifact, Execution, Output, RunResult};
pub use run::{CodeState, Exec, LogRef, Patch, Run, RunStatus, RuntimeHandle, Timeline};
pub use runnable::{
format_ambiguous_matches, ResolveResult, Runnable, RunnableMatch, RunnableType,
format_ambiguous_matches, stable_short_id, ResolveResult, Runnable, RunnableMatch, RunnableType,
};
pub use runtime::{BackgroundAdapter, RuntimeAdapter, RuntimeRegistry, TmuxAdapter};
pub use skill::{
Expand Down
25 changes: 15 additions & 10 deletions crates/runbox-core/src/playlist.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use serde::{Deserialize, Serialize};

use crate::runnable::stable_short_id;

/// A collection of RunTemplate references
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Playlist {
Expand All @@ -18,17 +20,20 @@ pub struct PlaylistItem {

impl PlaylistItem {
/// Generate a short ID for this playlist item based on hash of playlist_id, index, and template_id.
/// The short ID is deterministic and unique within a playlist.
/// The short ID is deterministic, unique within a playlist, and stable across Rust versions (uses SHA256).
///
/// This uses the same algorithm as `Runnable::PlaylistItem.short_id()` to ensure consistency
/// between `runbox playlist show` and `runbox run <short_id>`.
pub fn short_id(&self, playlist_id: &str, index: usize) -> String {
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};

let mut hasher = DefaultHasher::new();
playlist_id.hash(&mut hasher);
index.hash(&mut hasher);
self.template_id.hash(&mut hasher);

format!("{:08x}", hasher.finish() as u32) // First 8 hex chars
// Use the same format as Runnable::PlaylistItem.short_id()
// Format: "playlist_item\0" + playlist_id + "\0" + index + "\0" + template_id
let mut data = b"playlist_item\0".to_vec();
data.extend_from_slice(playlist_id.as_bytes());
data.push(0);
data.extend_from_slice(index.to_string().as_bytes());
data.push(0);
data.extend_from_slice(self.template_id.as_bytes());
stable_short_id(&data)
}
}

Expand Down
52 changes: 33 additions & 19 deletions crates/runbox-core/src/runnable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ impl std::str::FromStr for RunnableType {
"template" => Ok(RunnableType::Template),
"replay" => Ok(RunnableType::Replay),
"playlist" => Ok(RunnableType::Playlist),
_ => Err(format!("Invalid runnable type: {}. Valid types: template, replay, playlist", s)),
_ => Err(format!(
"Invalid runnable type: {}. Valid types: template, replay, playlist",
s
)),
}
}
}
Expand All @@ -61,10 +64,13 @@ pub enum Runnable {

/// Generate a stable 8-character hex short ID from input bytes using SHA256.
/// This is stable across Rust versions unlike DefaultHasher.
fn stable_short_id(data: &[u8]) -> String {
pub fn stable_short_id(data: &[u8]) -> String {
let hash = Sha256::digest(data);
// Take first 4 bytes (8 hex chars)
format!("{:02x}{:02x}{:02x}{:02x}", hash[0], hash[1], hash[2], hash[3])
format!(
"{:02x}{:02x}{:02x}{:02x}",
hash[0], hash[1], hash[2], hash[3]
)
}

/// Check if a string looks like a valid UUID hex portion (for replay short ID extraction)
Expand All @@ -91,10 +97,8 @@ impl Runnable {
Runnable::Replay(run_id) => {
// run_id format: "run_{uuid}"
// Extract hex chars from UUID, removing "run_" prefix and dashes
let uuid_part = run_id
.trim_start_matches("run_")
.replace('-', "");

let uuid_part = run_id.trim_start_matches("run_").replace('-', "");

// If it looks like valid UUID hex, extract first 8 chars (lowercase)
if is_valid_uuid_hex(&uuid_part) {
uuid_part.chars().take(8).collect::<String>().to_lowercase()
Expand Down Expand Up @@ -168,7 +172,7 @@ impl Runnable {
/// Returns the source label for display in list view.
///
/// - Template: "-" (no source, it's a root definition)
/// - Replay: shortened run_id
/// - Replay: shortened run_id
/// - PlaylistItem: "name[index]" format
pub fn source_label(&self) -> String {
match self {
Expand All @@ -178,9 +182,7 @@ impl Runnable {
run_id.trim_start_matches("run_").chars().take(10).collect()
}
Runnable::PlaylistItem {
playlist_id,
index,
..
playlist_id, index, ..
} => {
// Format as "name[idx]" - strip the "pl_" prefix for brevity
let name = playlist_id.trim_start_matches("pl_");
Expand Down Expand Up @@ -298,7 +300,7 @@ mod tests {
// If this test fails after a code change, it means short IDs have changed
let runnable = Runnable::Template("tpl_echo".to_string());
let short_id = runnable.short_id();

// SHA256("template\0tpl_echo") first 4 bytes as hex
// This is a known value that should not change
assert_eq!(short_id.len(), 8);
Expand Down Expand Up @@ -331,11 +333,11 @@ mod tests {
// Non-UUID run ID should fallback to hash
let runnable = Runnable::Replay("run_custom_id_123".to_string());
let short_id = runnable.short_id();

// Should be 8 hex chars (from hash)
assert_eq!(short_id.len(), 8);
assert!(short_id.chars().all(|c| c.is_ascii_hexdigit()));

// Should be deterministic
let short_id2 = Runnable::Replay("run_custom_id_123".to_string()).short_id();
assert_eq!(short_id, short_id2);
Expand Down Expand Up @@ -384,7 +386,7 @@ mod tests {
label: None,
};
let short_id = runnable.short_id();

assert_eq!(short_id.len(), 8);
assert!(short_id.chars().all(|c| c.is_ascii_hexdigit()));
}
Expand Down Expand Up @@ -578,10 +580,22 @@ mod tests {

#[test]
fn test_runnable_type_from_str() {
assert_eq!("template".parse::<RunnableType>().unwrap(), RunnableType::Template);
assert_eq!("replay".parse::<RunnableType>().unwrap(), RunnableType::Replay);
assert_eq!("playlist".parse::<RunnableType>().unwrap(), RunnableType::Playlist);
assert_eq!("TEMPLATE".parse::<RunnableType>().unwrap(), RunnableType::Template);
assert_eq!(
"template".parse::<RunnableType>().unwrap(),
RunnableType::Template
);
assert_eq!(
"replay".parse::<RunnableType>().unwrap(),
RunnableType::Replay
);
assert_eq!(
"playlist".parse::<RunnableType>().unwrap(),
RunnableType::Playlist
);
assert_eq!(
"TEMPLATE".parse::<RunnableType>().unwrap(),
RunnableType::Template
);
assert!("invalid".parse::<RunnableType>().is_err());
}

Expand Down