Skip to content
Merged
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
28 changes: 19 additions & 9 deletions src/mcp/tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,25 +164,27 @@ pub fn tool_list() -> Value {
},
{
"name": "trie_snapshot",
"description": "Save both tries (byte + word) and content store to disk.",
"description": "Save both tries (byte + word), content store, and layer store to disk.",
"inputSchema": {
"type": "object",
"properties": {
"path": { "type": "string", "description": "Byte-trie file path (default: ./trie-memory.dat)" },
"word_path": { "type": "string", "description": "Word-trie file path (default: ./word-trie-memory.dat)" },
"content_path": { "type": "string", "description": "Content-store file path (default: ./content-store.json)" }
"content_path": { "type": "string", "description": "Content-store file path (default: ./content-store.json)" },
"layer_path": { "type": "string", "description": "Layer-store file path (default: ./layer-store.json)" }
}
}
},
{
"name": "trie_restore",
"description": "Restore both tries (byte + word) and content store from disk.",
"description": "Restore both tries (byte + word), content store, and layer store from disk.",
"inputSchema": {
"type": "object",
"properties": {
"path": { "type": "string", "description": "Byte-trie file path (default: ./trie-memory.dat)" },
"word_path": { "type": "string", "description": "Word-trie file path (default: ./word-trie-memory.dat)" },
"content_path": { "type": "string", "description": "Content-store file path (default: ./content-store.json)" }
"content_path": { "type": "string", "description": "Content-store file path (default: ./content-store.json)" },
"layer_path": { "type": "string", "description": "Layer-store file path (default: ./layer-store.json)" }
}
}
},
Expand Down Expand Up @@ -987,6 +989,10 @@ fn handle_snapshot(trie: &Trie, word_trie: &Trie, store: &ContentStore, layers:
.get("content_path")
.and_then(|v| v.as_str())
.unwrap_or(DEFAULT_CONTENT_PATH);
let layer_path = args
.get("layer_path")
.and_then(|v| v.as_str())
.unwrap_or(DEFAULT_LAYER_PATH);

let byte = match trie.snapshot(byte_path) {
Ok(r) => r,
Expand All @@ -1000,7 +1006,7 @@ fn handle_snapshot(trie: &Trie, word_trie: &Trie, store: &ContentStore, layers:
Ok(n) => n,
Err(e) => return tool_error(&format!("Content-store snapshot failed: {}", e)),
};
let layer_bytes = match layers.save(DEFAULT_LAYER_PATH) {
let layer_bytes = match layers.save(layer_path) {
Ok(n) => n,
Err(e) => return tool_error(&format!("Layer-store snapshot failed: {}", e)),
};
Expand All @@ -1009,7 +1015,7 @@ fn handle_snapshot(trie: &Trie, word_trie: &Trie, store: &ContentStore, layers:
"byte": { "path": byte.path, "bytes_written": byte.bytes_written },
"word": { "path": word.path, "bytes_written": word.bytes_written },
"content": { "path": content_path, "bytes_written": content_bytes },
"layers": { "path": DEFAULT_LAYER_PATH, "bytes_written": layer_bytes },
"layers": { "path": layer_path, "bytes_written": layer_bytes },
}))
}

Expand All @@ -1026,6 +1032,10 @@ fn handle_restore(trie: &mut Trie, word_trie: &mut Trie, store: &mut ContentStor
.get("content_path")
.and_then(|v| v.as_str())
.unwrap_or(DEFAULT_CONTENT_PATH);
let layer_path = args
.get("layer_path")
.and_then(|v| v.as_str())
.unwrap_or(DEFAULT_LAYER_PATH);

let byte_restored = match Trie::restore_from(byte_path) {
Ok(r) => r,
Expand Down Expand Up @@ -1056,8 +1066,8 @@ fn handle_restore(trie: &mut Trie, word_trie: &mut Trie, store: &mut ContentStor
};

// Restore layer store if file exists
let layer_count = if LayerStore::exists(DEFAULT_LAYER_PATH) {
match LayerStore::load(DEFAULT_LAYER_PATH) {
let layer_count = if LayerStore::exists(layer_path) {
match LayerStore::load(layer_path) {
Ok(restored) => {
let count = restored.count();
*layers = restored;
Expand All @@ -1073,7 +1083,7 @@ fn handle_restore(trie: &mut Trie, word_trie: &mut Trie, store: &mut ContentStor
"byte": { "path": byte_path, "nodes_restored": byte_count },
"word": { "path": word_path, "nodes_restored": word_count },
"content": { "path": content_path, "entries_restored": content_entries },
"layers": { "path": DEFAULT_LAYER_PATH, "layers_restored": layer_count },
"layers": { "path": layer_path, "layers_restored": layer_count },
}))
}

Expand Down
5 changes: 5 additions & 0 deletions src/store/concept.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,11 @@ impl ConceptStore {
pub fn snapshot(&self, path: &str) -> Result<usize, String> {
let json = serde_json::to_string_pretty(self).map_err(|e| e.to_string())?;
let len = json.len();
if let Some(parent) = std::path::Path::new(path).parent() {
if !parent.as_os_str().is_empty() {
std::fs::create_dir_all(parent).map_err(|e| e.to_string())?;
}
}
std::fs::write(path, &json).map_err(|e| e.to_string())?;
Ok(len)
}
Expand Down
5 changes: 5 additions & 0 deletions src/store/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ impl LayerStore {
let json = serde_json::to_string_pretty(self)
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?;
let len = json.len();
if let Some(parent) = std::path::Path::new(path).parent() {
if !parent.as_os_str().is_empty() {
std::fs::create_dir_all(parent)?;
}
}
std::fs::write(path, &json)?;
Ok(len)
}
Expand Down
5 changes: 5 additions & 0 deletions src/store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ impl ContentStore {
let json = serde_json::to_string_pretty(self)
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?;
let len = json.len();
if let Some(parent) = std::path::Path::new(path).parent() {
if !parent.as_os_str().is_empty() {
std::fs::create_dir_all(parent)?;
}
}
std::fs::write(path, &json)?;
Ok(len)
}
Expand Down
5 changes: 5 additions & 0 deletions src/trie/persistence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ impl Trie {
pub fn snapshot(&self, path: &str) -> io::Result<SnapshotResult> {
let bytes = bincode::serialize(self).map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
let len = bytes.len();
if let Some(parent) = Path::new(path).parent() {
if !parent.as_os_str().is_empty() {
fs::create_dir_all(parent)?;
}
}
fs::write(path, &bytes)?;
Ok(SnapshotResult {
path: path.to_string(),
Expand Down
8 changes: 6 additions & 2 deletions tests/honest_agent_scenarios.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,23 +202,25 @@ fn scenario_2_within_session_learning() {

// ---------- Scenario 3 ----------

fn snapshot_paths(suffix: &str) -> (String, String, String) {
fn snapshot_paths(suffix: &str) -> (String, String, String, String) {
let dir = std::env::temp_dir().join(format!("trie-memory-scenario-{}", suffix));
std::fs::create_dir_all(&dir).ok();
(
dir.join("trie.dat").to_string_lossy().into_owned(),
dir.join("word-trie.dat").to_string_lossy().into_owned(),
dir.join("content.json").to_string_lossy().into_owned(),
dir.join("layer.json").to_string_lossy().into_owned(),
)
}

#[test]
fn scenario_3_cross_session_learning() {
let (trie_path, word_path, content_path) = snapshot_paths("cross-session");
let (trie_path, word_path, content_path, layer_path) = snapshot_paths("cross-session");
// Ensure clean start.
std::fs::remove_file(&trie_path).ok();
std::fs::remove_file(&word_path).ok();
std::fs::remove_file(&content_path).ok();
std::fs::remove_file(&layer_path).ok();

// ---- Session 1: prime, teach, snapshot ----
{
Expand All @@ -241,6 +243,7 @@ fn scenario_3_cross_session_learning() {
"path": trie_path,
"word_path": word_path,
"content_path": content_path,
"layer_path": layer_path,
}),
);
assert!(
Expand All @@ -259,6 +262,7 @@ fn scenario_3_cross_session_learning() {
"path": trie_path.clone(),
"word_path": word_path.clone(),
"content_path": content_path.clone(),
"layer_path": layer_path.clone(),
}),
);
assert!(
Expand Down
Loading