File size: 21,750 Bytes
5b471d6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 | // SPF Smart Gateway - Agent State LMDB
// Copyright 2026 Joseph Stone - All Rights Reserved
//
// LMDB-backed persistent state for Agent's virtual home. Stores preferences,
// memory, working context, and session continuity data across sessions.
//
// Database: AGENT_STATE
// Storage: ~/SPFsmartGATE/LIVE/LMDB5/LMDB5.DB/
use anyhow::{anyhow, Result};
use heed::types::*;
use heed::{Database, Env, EnvOpenOptions};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::Path;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::{SystemTime, UNIX_EPOCH};
/// Atomic counter for unique memory IDs within same timestamp
static MEMORY_COUNTER: AtomicU64 = AtomicU64::new(0);
const MAX_DB_SIZE: usize = 1024 * 1024 * 1024; // 1GB — virtual address space only, no physical cost
/// Memory entry type
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub enum MemoryType {
/// User preference
Preference,
/// Fact about the user/project
Fact,
/// Instruction from user
Instruction,
/// Context from previous sessions
Context,
/// Working state (temporary, session-bound)
Working,
/// Pinned (never auto-expire)
Pinned,
}
/// Memory entry stored in Agent's memory
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MemoryEntry {
/// Unique ID
pub id: String,
/// Memory content
pub content: String,
/// Memory type
pub memory_type: MemoryType,
/// Tags for categorization
pub tags: Vec<String>,
/// Source (session ID or "user" if explicit)
pub source: String,
/// Created timestamp
pub created_at: u64,
/// Last accessed timestamp
pub last_accessed: u64,
/// Access count
pub access_count: u64,
/// Relevance score (0.0 - 1.0)
pub relevance: f64,
/// Expiry timestamp (0 = never)
pub expires_at: u64,
}
/// Session context for continuity
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SessionContext {
/// Session ID
pub session_id: String,
/// Parent session ID (if resumed)
pub parent_session: Option<String>,
/// Session start time
pub started_at: u64,
/// Session end time (0 if ongoing)
pub ended_at: u64,
/// Working directory at start
pub working_dir: String,
/// Active project at start
pub active_project: Option<String>,
/// Summary of what was accomplished
pub summary: String,
/// Files modified
pub files_modified: Vec<String>,
/// Total complexity
pub total_complexity: u64,
/// Total actions
pub total_actions: u64,
}
/// Agent preferences
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct AgentPreferences {
/// Preferred code style (e.g., "rust", "python")
pub code_style: Option<String>,
/// Preferred response length ("brief", "detailed", "adaptive")
pub response_length: String,
/// Whether to show thinking process
pub show_thinking: bool,
/// Preferred editor for large edits
pub preferred_editor: Option<String>,
/// Auto-save session context
pub auto_save_context: bool,
/// Maximum context entries to remember
pub max_context_entries: usize,
/// Custom key-value preferences
pub custom: HashMap<String, String>,
}
/// LMDB-backed Agent state manager
pub struct AgentStateDb {
env: Env,
/// Memory storage: id -> MemoryEntry
memory: Database<Str, SerdeBincode<MemoryEntry>>,
/// Session history: session_id -> SessionContext
sessions: Database<Str, SerdeBincode<SessionContext>>,
/// Key-value state: key -> JSON value
state: Database<Str, Str>,
/// Tag index: "tag:tagname" -> list of memory IDs (JSON array)
tags: Database<Str, Str>,
}
impl AgentStateDb {
/// Open or create Agent state LMDB at given path
pub fn open(path: &Path) -> Result<Self> {
std::fs::create_dir_all(path)?;
let env = unsafe {
EnvOpenOptions::new()
.map_size(MAX_DB_SIZE)
.max_dbs(8)
.open(path)?
};
let mut wtxn = env.write_txn()?;
let memory = env.create_database(&mut wtxn, Some("memory"))?;
let sessions = env.create_database(&mut wtxn, Some("sessions"))?;
let state = env.create_database(&mut wtxn, Some("state"))?;
let tags = env.create_database(&mut wtxn, Some("tags"))?;
wtxn.commit()?;
log::info!("Agent State LMDB opened at {:?}", path);
Ok(Self { env, memory, sessions, state, tags })
}
// ========================================================================
// MEMORY OPERATIONS
// ========================================================================
/// Store a memory entry
pub fn remember(&self, entry: MemoryEntry) -> Result<String> {
let id = entry.id.clone();
// Update tag index
for tag in &entry.tags {
self.add_to_tag_index(tag, &id)?;
}
let mut wtxn = self.env.write_txn()?;
self.memory.put(&mut wtxn, &id, &entry)?;
wtxn.commit()?;
Ok(id)
}
/// Create and store a new memory
pub fn create_memory(
&self,
content: &str,
memory_type: MemoryType,
tags: Vec<String>,
source: &str,
) -> Result<String> {
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_secs();
let counter = MEMORY_COUNTER.fetch_add(1, Ordering::SeqCst);
let id = format!("mem_{}_{}", now, counter);
let entry = MemoryEntry {
id: id.clone(),
content: content.to_string(),
memory_type,
tags,
source: source.to_string(),
created_at: now,
last_accessed: now,
access_count: 0,
relevance: 1.0,
expires_at: match memory_type {
MemoryType::Working => now + 3600, // 1 hour
MemoryType::Fact => now + 43200, // 12 hours
MemoryType::Pinned => now + 172800, // 48 hours
_ => 0,
},
};
self.remember(entry)
}
/// Recall a memory by ID
pub fn recall(&self, id: &str) -> Result<Option<MemoryEntry>> {
let rtxn = self.env.read_txn()?;
let entry = self.memory.get(&rtxn, id)?;
drop(rtxn);
// Update access stats
if let Some(mut e) = entry.clone() {
e.last_accessed = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_secs();
e.access_count += 1;
let mut wtxn = self.env.write_txn()?;
self.memory.put(&mut wtxn, id, &e)?;
wtxn.commit()?;
}
Ok(entry)
}
/// Search memories by content (simple substring match)
pub fn search_memories(&self, query: &str, limit: usize) -> Result<Vec<MemoryEntry>> {
let rtxn = self.env.read_txn()?;
let iter = self.memory.iter(&rtxn)?;
let query_lower = query.to_lowercase();
let mut matches = Vec::new();
for result in iter {
let (_, entry) = result?;
if entry.content.to_lowercase().contains(&query_lower) {
matches.push(entry);
if matches.len() >= limit {
break;
}
}
}
// Sort by relevance * recency
matches.sort_by(|a, b| {
let score_a = a.relevance * (a.last_accessed as f64);
let score_b = b.relevance * (b.last_accessed as f64);
score_b.partial_cmp(&score_a).unwrap_or(std::cmp::Ordering::Equal)
});
Ok(matches)
}
/// Get memories by tag
pub fn get_by_tag(&self, tag: &str) -> Result<Vec<MemoryEntry>> {
let key = format!("tag:{}", tag);
let rtxn = self.env.read_txn()?;
let ids: Vec<String> = match self.tags.get(&rtxn, &key)? {
Some(json) => serde_json::from_str(json)?,
None => return Ok(Vec::new()),
};
let mut entries = Vec::new();
for id in ids {
if let Some(entry) = self.memory.get(&rtxn, &id)? {
entries.push(entry);
}
}
Ok(entries)
}
/// Get memories by type
pub fn get_by_type(&self, memory_type: MemoryType) -> Result<Vec<MemoryEntry>> {
let rtxn = self.env.read_txn()?;
let iter = self.memory.iter(&rtxn)?;
let mut entries = Vec::new();
for result in iter {
let (_, entry) = result?;
if entry.memory_type == memory_type {
entries.push(entry);
}
}
Ok(entries)
}
/// Forget a memory
pub fn forget(&self, id: &str) -> Result<bool> {
// Remove from tag index
if let Some(entry) = self.recall(id)? {
for tag in &entry.tags {
self.remove_from_tag_index(tag, id)?;
}
}
let mut wtxn = self.env.write_txn()?;
let deleted = self.memory.delete(&mut wtxn, id)?;
wtxn.commit()?;
Ok(deleted)
}
/// Expire old memories
pub fn expire_memories(&self) -> Result<u64> {
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_secs();
let rtxn = self.env.read_txn()?;
let iter = self.memory.iter(&rtxn)?;
let mut to_delete = Vec::new();
for result in iter {
let (id, entry) = result?;
if entry.expires_at > 0 && entry.expires_at < now {
to_delete.push(id.to_string());
}
}
drop(rtxn);
let count = to_delete.len() as u64;
for id in to_delete {
self.forget(&id)?;
}
Ok(count)
}
// ========================================================================
// TAG INDEX
// ========================================================================
fn add_to_tag_index(&self, tag: &str, id: &str) -> Result<()> {
let key = format!("tag:{}", tag);
let rtxn = self.env.read_txn()?;
let mut ids: Vec<String> = match self.tags.get(&rtxn, &key)? {
Some(json) => serde_json::from_str(json)?,
None => Vec::new(),
};
drop(rtxn);
if !ids.contains(&id.to_string()) {
ids.push(id.to_string());
let json = serde_json::to_string(&ids)?;
let mut wtxn = self.env.write_txn()?;
self.tags.put(&mut wtxn, &key, &json)?;
wtxn.commit()?;
}
Ok(())
}
fn remove_from_tag_index(&self, tag: &str, id: &str) -> Result<()> {
let key = format!("tag:{}", tag);
let rtxn = self.env.read_txn()?;
let mut ids: Vec<String> = match self.tags.get(&rtxn, &key)? {
Some(json) => serde_json::from_str(json)?,
None => return Ok(()),
};
drop(rtxn);
ids.retain(|i| i != id);
let json = serde_json::to_string(&ids)?;
let mut wtxn = self.env.write_txn()?;
self.tags.put(&mut wtxn, &key, &json)?;
wtxn.commit()?;
Ok(())
}
/// List all tags
pub fn list_tags(&self) -> Result<Vec<String>> {
let rtxn = self.env.read_txn()?;
let iter = self.tags.iter(&rtxn)?;
let mut tags = Vec::new();
for result in iter {
let (key, _) = result?;
if key.starts_with("tag:") {
tags.push(key[4..].to_string());
}
}
Ok(tags)
}
// ========================================================================
// SESSION MANAGEMENT
// ========================================================================
/// Start a new session
pub fn start_session(&self, session_id: &str, working_dir: &str) -> Result<SessionContext> {
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_secs();
// Check for parent session (most recent)
let parent = self.get_latest_session()?.map(|s| s.session_id);
let ctx = SessionContext {
session_id: session_id.to_string(),
parent_session: parent,
started_at: now,
ended_at: 0,
working_dir: working_dir.to_string(),
active_project: None,
summary: String::new(),
files_modified: Vec::new(),
total_complexity: 0,
total_actions: 0,
};
let mut wtxn = self.env.write_txn()?;
self.sessions.put(&mut wtxn, session_id, &ctx)?;
wtxn.commit()?;
Ok(ctx)
}
/// End a session
pub fn end_session(&self, session_id: &str, summary: &str) -> Result<()> {
let rtxn = self.env.read_txn()?;
let mut ctx = self.sessions.get(&rtxn, session_id)?
.ok_or_else(|| anyhow!("Session not found: {}", session_id))?;
drop(rtxn);
ctx.ended_at = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_secs();
ctx.summary = summary.to_string();
let mut wtxn = self.env.write_txn()?;
self.sessions.put(&mut wtxn, session_id, &ctx)?;
wtxn.commit()?;
Ok(())
}
/// Update session context
pub fn update_session(&self, ctx: &SessionContext) -> Result<()> {
let mut wtxn = self.env.write_txn()?;
self.sessions.put(&mut wtxn, &ctx.session_id, ctx)?;
wtxn.commit()?;
Ok(())
}
/// Get session by ID
pub fn get_session(&self, session_id: &str) -> Result<Option<SessionContext>> {
let rtxn = self.env.read_txn()?;
Ok(self.sessions.get(&rtxn, session_id)?)
}
/// Get most recent session
pub fn get_latest_session(&self) -> Result<Option<SessionContext>> {
let rtxn = self.env.read_txn()?;
let iter = self.sessions.iter(&rtxn)?;
let mut latest: Option<SessionContext> = None;
for result in iter {
let (_, ctx) = result?;
if latest.as_ref().map_or(true, |l| ctx.started_at > l.started_at) {
latest = Some(ctx);
}
}
Ok(latest)
}
/// Get session chain (this session and all parents)
pub fn get_session_chain(&self, session_id: &str) -> Result<Vec<SessionContext>> {
let mut chain = Vec::new();
let mut current = session_id.to_string();
while let Some(ctx) = self.get_session(¤t)? {
let parent = ctx.parent_session.clone();
chain.push(ctx);
match parent {
Some(p) => current = p,
None => break,
}
}
Ok(chain)
}
/// Record file modification in session
pub fn record_file_modified(&self, session_id: &str, file_path: &str) -> Result<()> {
let rtxn = self.env.read_txn()?;
let mut ctx = self.sessions.get(&rtxn, session_id)?
.ok_or_else(|| anyhow!("Session not found"))?;
drop(rtxn);
if !ctx.files_modified.contains(&file_path.to_string()) {
ctx.files_modified.push(file_path.to_string());
self.update_session(&ctx)?;
}
Ok(())
}
/// Increment session counters
pub fn increment_session_stats(&self, session_id: &str, complexity: u64) -> Result<()> {
let rtxn = self.env.read_txn()?;
let mut ctx = self.sessions.get(&rtxn, session_id)?
.ok_or_else(|| anyhow!("Session not found"))?;
drop(rtxn);
ctx.total_complexity += complexity;
ctx.total_actions += 1;
self.update_session(&ctx)
}
// ========================================================================
// STATE (Key-Value)
// ========================================================================
/// Get a state value
pub fn get_state(&self, key: &str) -> Result<Option<String>> {
let rtxn = self.env.read_txn()?;
Ok(self.state.get(&rtxn, key)?.map(|s| s.to_string()))
}
/// Set a state value
pub fn set_state(&self, key: &str, value: &str) -> Result<()> {
let mut wtxn = self.env.write_txn()?;
self.state.put(&mut wtxn, key, value)?;
wtxn.commit()?;
Ok(())
}
/// Get typed state value
pub fn get_state_typed<T: for<'de> Deserialize<'de>>(&self, key: &str) -> Result<Option<T>> {
match self.get_state(key)? {
Some(json) => Ok(Some(serde_json::from_str(&json)?)),
None => Ok(None),
}
}
/// Set typed state value
pub fn set_state_typed<T: Serialize>(&self, key: &str, value: &T) -> Result<()> {
let json = serde_json::to_string(value)?;
self.set_state(key, &json)
}
/// Delete a state key
pub fn delete_state(&self, key: &str) -> Result<bool> {
let mut wtxn = self.env.write_txn()?;
let deleted = self.state.delete(&mut wtxn, key)?;
wtxn.commit()?;
Ok(deleted)
}
/// List all state keys
pub fn list_state_keys(&self) -> Result<Vec<String>> {
let rtxn = self.env.read_txn()?;
let iter = self.state.iter(&rtxn)?;
let mut keys = Vec::new();
for result in iter {
let (key, _) = result?;
keys.push(key.to_string());
}
Ok(keys)
}
// ========================================================================
// PREFERENCES
// ========================================================================
/// Get Agent preferences
pub fn get_preferences(&self) -> Result<AgentPreferences> {
self.get_state_typed::<AgentPreferences>("preferences")?
.ok_or_else(|| anyhow!("Preferences not initialized"))
.or_else(|_| Ok(AgentPreferences::default()))
}
/// Set Agent preferences
pub fn set_preferences(&self, prefs: &AgentPreferences) -> Result<()> {
self.set_state_typed("preferences", prefs)
}
/// Update a single preference
pub fn set_preference(&self, key: &str, value: &str) -> Result<()> {
let mut prefs = self.get_preferences()?;
prefs.custom.insert(key.to_string(), value.to_string());
self.set_preferences(&prefs)
}
// ========================================================================
// INITIALIZATION
// ========================================================================
/// Initialize with defaults
pub fn init_defaults(&self) -> Result<()> {
// Only init if not already initialized
if self.get_state("initialized")?.is_some() {
return Ok(());
}
// Default preferences
self.set_preferences(&AgentPreferences {
code_style: None,
response_length: "adaptive".to_string(),
show_thinking: false,
preferred_editor: None,
auto_save_context: true,
max_context_entries: 100,
custom: HashMap::new(),
})?;
// Initial memories
self.create_memory(
"SPF Smart Gateway provides AI self-governance with complexity-based enforcement",
MemoryType::Fact,
vec!["spf".to_string(), "system".to_string()],
"system",
)?;
self.create_memory(
"User prefers concise responses without emojis unless requested",
MemoryType::Preference,
vec!["style".to_string()],
"system",
)?;
self.set_state("initialized", "true")?;
self.set_state("version", "1.0.0")?;
log::info!("Agent State LMDB initialized with defaults");
Ok(())
}
/// Get context summary for session start
pub fn get_context_summary(&self) -> Result<String> {
let mut summary = String::new();
// Last session info
if let Some(last) = self.get_latest_session()? {
if !last.summary.is_empty() {
summary.push_str(&format!("Last session: {}\n", last.summary));
}
if !last.files_modified.is_empty() {
summary.push_str(&format!(
"Files modified: {}\n",
last.files_modified.len()
));
}
}
// Active instructions
let instructions = self.get_by_type(MemoryType::Instruction)?;
if !instructions.is_empty() {
summary.push_str("\nActive instructions:\n");
for inst in instructions.iter().take(5) {
summary.push_str(&format!("- {}\n", inst.content));
}
}
// Recent context
let context = self.get_by_type(MemoryType::Context)?;
if !context.is_empty() {
summary.push_str("\nRecent context:\n");
for ctx in context.iter().take(3) {
summary.push_str(&format!("- {}\n", ctx.content));
}
}
Ok(summary)
}
/// Get database stats
pub fn db_stats(&self) -> Result<(u64, u64, u64, u64)> {
let rtxn = self.env.read_txn()?;
let memory_stat = self.memory.stat(&rtxn)?;
let sessions_stat = self.sessions.stat(&rtxn)?;
let state_stat = self.state.stat(&rtxn)?;
let tags_stat = self.tags.stat(&rtxn)?;
Ok((
memory_stat.entries as u64,
sessions_stat.entries as u64,
state_stat.entries as u64,
tags_stat.entries as u64,
))
}
} |