Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

feat(todos): add one-time migration of legacy run ledgers into the crate store

Steven Enamakeld9803b05dc41
diff --git a/crates/openhuman-core/src/agent/todos/runs.rs b/crates/openhuman-core/src/agent/todos/runs.rs
index c7d4ef25de..158e73f692 100644
--- a/crates/openhuman-core/src/agent/todos/runs.rs
+++ b/crates/openhuman-core/src/agent/todos/runs.rs
@@ -1,16 +1,20 @@
-//! OpenHuman host adapter over [`tinyagents_graph::todos::runs`].
+//! Compatibility facade over [`tinyagents_graph::todos::runs`].
//!
//! TinyAgents owns the durable task-run record, the heartbeat, the staleness
//! policy, and the reclaim sweep (card back to `todo`, or parked at `blocked`
//! once a card has burned through its reclaim budget). What stays here is
//! OpenHuman's own shape around it: [`BoardLocation`] addressing (including the
-//! process-global scratch board), RFC 3339 timestamps on the wire, and the
-//! `TaskRunReclaimed` domain event.
+//! process-global scratch board), RFC 3339 timestamps on the wire, the
+//! `TaskRunReclaimed` domain event, and the one-time import of the retired
+//! `{workspace}/agent_task_boards/<hex>.runs.json` ledger.
//!
//! Run records live in the crate KV store beside the board itself
//! (`graph.todos.runs`), so a board and its run log can no longer drift apart
//! across a restart.
+use std::path::Path;
+
+use serde::{Deserialize, Serialize};
use tinyagents_graph::todos::runs as crate_runs;
pub use tinyagents_graph::todos::runs::{
@@ -18,19 +22,22 @@ pub use tinyagents_graph::todos::runs::{
DEFAULT_HEARTBEAT_STALE_SECS, DEFAULT_MAX_RECLAIM_COUNT,
};
-use crate::agent::todos::types::normalize_timestamp_for_wire;
+use crate::agent::task_board::normalize_timestamp_for_wire;
use super::ops::{target, BoardLocation};
/// Cadence of the background heartbeat spawned alongside an autonomous run.
const HEARTBEAT_TICK: std::time::Duration = crate_runs::DEFAULT_HEARTBEAT_TICK;
+/// Legacy on-disk ledger the crate store replaced.
+const TASK_BOARD_DIR: &str = "agent_task_boards";
+
fn map_err<T>(result: tinyagents_harness::error::Result<T>) -> Result<T, String> {
result.map_err(|error| error.to_string())
}
-/// Crate stamps are unix-epoch milliseconds; OpenHuman logs and transcripts use
-/// RFC 3339, so translate on the way out.
+/// Crate stamps are unix-epoch milliseconds; the `openhuman.todos_run_*` RPC
+/// surface has always spoken RFC 3339, so translate on the way out.
fn for_wire(mut run: TaskRun) -> TaskRun {
run.started_at = normalize_timestamp_for_wire(&run.started_at);
run.last_heartbeat_at = normalize_timestamp_for_wire(&run.last_heartbeat_at);
@@ -101,7 +108,7 @@ pub async fn find_stale_runs(
}
/// Reclaim stale runs and publish a `TaskRunReclaimed` event per reclaimed
-/// card for other runtime consumers.
+/// card, so the Tasks board UI sees a wedged card come back without a refresh.
pub async fn reclaim_stale(
location: &BoardLocation,
limits: &RunLimits,
@@ -134,6 +141,115 @@ pub fn spawn_heartbeat_task(
crate_runs::spawn_heartbeat_task(store, thread_id.to_string(), run_id, cancel, HEARTBEAT_TICK);
}
+// ── Legacy ledger migration ────────────────────────────────────────────
+
+/// Outcome of the one-time `<hex>.runs.json` import.
+#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
+pub struct TaskRunMigrationReport {
+ pub total: usize,
+ pub copied: usize,
+ pub skipped: usize,
+}
+
+/// Copy any run ledgers left in the retired file tree into the crate store,
+/// without replacing runs the crate already holds.
+///
+/// A thread whose crate log is non-empty is skipped wholesale: the crate log is
+/// authoritative, and merging two histories would double-count the reclaims the
+/// sweep's `max_reclaim_count` budget is derived from.
+pub async fn migrate_legacy_task_runs(
+ workspace_dir: &Path,
+) -> Result<TaskRunMigrationReport, String> {
+ let dir = workspace_dir.join(TASK_BOARD_DIR);
+ let mut entries = match tokio::fs::read_dir(&dir).await {
+ Ok(entries) => entries,
+ Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
+ return Ok(TaskRunMigrationReport::default());
+ }
+ Err(error) => return Err(format!("read legacy runs dir {}: {error}", dir.display())),
+ };
+
+ let mut report = TaskRunMigrationReport::default();
+ while let Some(entry) = entries
+ .next_entry()
+ .await
+ .map_err(|error| format!("iterate legacy runs dir: {error}"))?
+ {
+ let path = entry.path();
+ let Some(thread_id) = legacy_thread_id(&path) else {
+ continue;
+ };
+ report.total += 1;
+
+ let runs: Vec<TaskRun> = match tokio::fs::read_to_string(&path).await {
+ Ok(body) => match serde_json::from_str(&body) {
+ Ok(runs) => runs,
+ Err(error) => {
+ tracing::warn!(path = %path.display(), %error, "skip invalid legacy run ledger");
+ report.skipped += 1;
+ continue;
+ }
+ },
+ Err(error) => {
+ tracing::warn!(path = %path.display(), %error, "skip unreadable legacy run ledger");
+ report.skipped += 1;
+ continue;
+ }
+ };
+
+ let location = BoardLocation::Thread {
+ workspace_dir: workspace_dir.to_path_buf(),
+ thread_id: thread_id.clone(),
+ };
+ let (store, thread_id) = target(&location);
+ match map_err(crate_runs::import_if_absent(&store, thread_id, runs).await) {
+ Ok(true) => report.copied += 1,
+ Ok(false) => report.skipped += 1,
+ Err(error) => {
+ tracing::warn!(path = %path.display(), %error, "skip legacy run ledger: store write failed");
+ report.skipped += 1;
+ }
+ }
+ }
+ Ok(report)
+}
+
+/// Decode the thread id encoded in a `<hex>.runs.json` file name.
+///
+/// Only strict, ASCII, even-length lowercase hex is accepted. The inner two
+/// bytes of every pair must both be hexadecimal digits (`0-9a-f`), so a signed
+/// or malformed stem such as `+f` is rejected rather than accepted by
+/// `u8::from_str_radix`. Decoding walks `hex.as_bytes()` in whole pairs, never
+/// slicing a multi-byte UTF-8 character, so a non-ASCII stem like `aéb` returns
+/// `None` instead of panicking mid-startup.
+fn legacy_thread_id(path: &Path) -> Option<String> {
+ let name = path.file_name()?.to_str()?;
+ let hex = name.strip_suffix(".runs.json")?;
+ if hex.is_empty() || hex.len() % 2 != 0 || !hex.is_ascii() {
+ return None;
+ }
+ let bytes: Vec<u8> = hex
+ .as_bytes()
+ .chunks_exact(2)
+ .map(|pair| {
+ let hi = lowercase_hex_nibble(pair[0])?;
+ let lo = lowercase_hex_nibble(pair[1])?;
+ Some(hi * 16 + lo)
+ })
+ .collect::<Option<Vec<u8>>>()?;
+ String::from_utf8(bytes).ok()
+}
+
+/// Decode one ASCII byte as a lowercase hexadecimal nibble (`0-9a-f`), or
+/// `None` for any other byte (including uppercase `A-F`).
+fn lowercase_hex_nibble(byte: u8) -> Option<u8> {
+ match byte {
+ b'0'..=b'9' => Some(byte - b'0'),
+ b'a'..=b'f' => Some(byte - b'a' + 10),
+ _ => None,
+ }
+}
+
// ── Tests ──────────────────────────────────────────────────────────────
#[cfg(test)]