Skip to content

Commit 14c4c1d

Browse files
authored
stable hand agent IDs
* fix: use fixed agent ID for hand agents based on hand_id This ensures triggers and cron jobs continue to work after daemon restart, as hand agents now have stable IDs instead of generating a new UUID each time. Changes: - Add AgentId::from_string() method for deterministic ID generation - Modify spawn_agent_with_parent() to accept optional fixed_id - Use hand_id-based fixed ID in activate_hand() See: #519 * remove: remove serena local config from commit * chore: ignore .serena directory
1 parent fa7dd27 commit 14c4c1d

File tree

5 files changed

+25
-6
lines changed

5 files changed

+25
-6
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,4 @@ Thumbs.db
4444
*.swp
4545
*.swo
4646
*~
47+
.serena/

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ chrono = { version = "0.4", features = ["serde"] }
5252
chrono-tz = "0.10"
5353

5454
# IDs
55-
uuid = { version = "1", features = ["v4", "serde"] }
55+
uuid = { version = "1", features = ["v4", "v5", "serde"] }
5656

5757
# Database
5858
rusqlite = { version = "0.31", features = ["bundled", "serde_json"] }

crates/openfang-kernel/src/kernel.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,16 +1227,18 @@ impl OpenFangKernel {
12271227

12281228
/// Spawn a new agent from a manifest, optionally linking to a parent agent.
12291229
pub fn spawn_agent(&self, manifest: AgentManifest) -> KernelResult<AgentId> {
1230-
self.spawn_agent_with_parent(manifest, None)
1230+
self.spawn_agent_with_parent(manifest, None, None)
12311231
}
12321232

12331233
/// Spawn a new agent with an optional parent for lineage tracking.
1234+
/// If fixed_id is provided, use it instead of generating a new UUID.
12341235
pub fn spawn_agent_with_parent(
12351236
&self,
12361237
manifest: AgentManifest,
12371238
parent: Option<AgentId>,
1239+
fixed_id: Option<AgentId>,
12381240
) -> KernelResult<AgentId> {
1239-
let agent_id = AgentId::new();
1241+
let agent_id = fixed_id.unwrap_or_else(AgentId::new);
12401242
let session_id = SessionId::new();
12411243
let name = manifest.name.clone();
12421244

@@ -3279,8 +3281,10 @@ impl OpenFangKernel {
32793281
let _ = self.kill_agent(old.id);
32803282
}
32813283

3282-
// Spawn the agent
3283-
let agent_id = self.spawn_agent(manifest)?;
3284+
// Spawn the agent with a fixed ID based on hand_id for stable identity across restarts.
3285+
// This ensures triggers and cron jobs continue to work after daemon restart.
3286+
let fixed_agent_id = AgentId::from_string(hand_id);
3287+
let agent_id = self.spawn_agent_with_parent(manifest, None, Some(fixed_agent_id))?;
32843288

32853289
// Restore triggers from the old agent under the new agent ID (#519).
32863290
if !saved_triggers.is_empty() {
@@ -5500,7 +5504,7 @@ impl KernelHandle for OpenFangKernel {
55005504
let name = manifest.name.clone();
55015505
let parent = parent_id.and_then(|pid| pid.parse::<AgentId>().ok());
55025506
let id = self
5503-
.spawn_agent_with_parent(manifest, parent)
5507+
.spawn_agent_with_parent(manifest, parent, None)
55045508
.map_err(|e| format!("Spawn failed: {e}"))?;
55055509
Ok((id.to_string(), name))
55065510
}

crates/openfang-types/src/agent.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,13 @@ impl AgentId {
117117
pub fn new() -> Self {
118118
Self(Uuid::new_v4())
119119
}
120+
121+
/// Create a deterministic AgentId from a string using SHA-1 namespace.
122+
/// Useful for hand agents that need stable IDs across restarts.
123+
pub fn from_string(s: &str) -> Self {
124+
const NAMESPACE: Uuid = Uuid::NAMESPACE_DNS;
125+
Self(Uuid::new_v5(&NAMESPACE, s.as_bytes()))
126+
}
120127
}
121128

122129
impl Default for AgentId {

0 commit comments

Comments
 (0)