Skip to content

Commit ac46e2e

Browse files
penbergclaude
andcommitted
Move is_overlay_enabled() from CLI to SDK
The function checks if an overlay filesystem is configured and returns the base path. This belongs in the SDK so it can be used by SDK consumers, not just the CLI. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent d93467e commit ac46e2e

2 files changed

Lines changed: 41 additions & 43 deletions

File tree

cli/src/cmd/fs.rs

Lines changed: 4 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::collections::{HashSet, VecDeque};
22

3-
use agentfs_sdk::AgentFSOptions;
3+
use agentfs_sdk::{AgentFS, AgentFSOptions};
44
use anyhow::{Context, Result as AnyhowResult};
55
use turso::{Builder, Connection, Value};
66

@@ -210,39 +210,6 @@ impl std::fmt::Display for ChangeType {
210210
}
211211
}
212212

213-
/// Check if overlay is enabled for this filesystem
214-
async fn is_overlay_enabled(conn: &Connection) -> AnyhowResult<Option<String>> {
215-
// Check if fs_overlay_config table exists and has base_path
216-
let result = conn
217-
.query(
218-
"SELECT value FROM fs_overlay_config WHERE key = 'base_path'",
219-
(),
220-
)
221-
.await;
222-
223-
match result {
224-
Ok(mut rows) => {
225-
if let Some(row) = rows.next().await? {
226-
let base_path: String = row
227-
.get_value(0)
228-
.ok()
229-
.and_then(|v| {
230-
if let Value::Text(s) = v {
231-
Some(s.clone())
232-
} else {
233-
None
234-
}
235-
})
236-
.unwrap_or_default();
237-
Ok(Some(base_path))
238-
} else {
239-
Ok(None)
240-
}
241-
}
242-
Err(_) => Ok(None), // Table doesn't exist
243-
}
244-
}
245-
246213
/// Get all paths in the delta layer (files in fs_dentry)
247214
async fn get_delta_paths(conn: &Connection) -> AnyhowResult<HashSet<String>> {
248215
let mut paths = HashSet::new();
@@ -403,18 +370,13 @@ fn path_exists_in_base(base_path: &str, rel_path: &str) -> bool {
403370

404371
pub async fn diff_filesystem(id_or_path: String) -> AnyhowResult<()> {
405372
let options = AgentFSOptions::resolve(&id_or_path)?;
406-
let db_path = options.path.context("No database path resolved")?;
407373
eprintln!("Using agent: {}", id_or_path);
408374

409-
let db = Builder::new_local(&db_path)
410-
.build()
411-
.await
412-
.context("Failed to open filesystem")?;
413-
414-
let conn = db.connect().context("Failed to connect to filesystem")?;
375+
let agent = AgentFS::open(options).await.context("Failed to open agent")?;
376+
let conn = agent.get_connection();
415377

416378
// Check if overlay is enabled
417-
let base_path = match is_overlay_enabled(&conn).await? {
379+
let base_path = match agent.is_overlay_enabled().await? {
418380
Some(path) => path,
419381
None => {
420382
println!("No diff (non-overlay filesystem)");

sdk/rust/src/lib.rs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pub mod toolcalls;
44

55
use anyhow::Result;
66
use std::{path::Path, sync::Arc};
7-
use turso::{Builder, Connection};
7+
use turso::{Builder, Connection, Value};
88

99
// Re-export filesystem types
1010
#[cfg(unix)]
@@ -196,6 +196,42 @@ impl AgentFS {
196196
self.conn.clone()
197197
}
198198

199+
/// Check if overlay is enabled for this filesystem
200+
///
201+
/// Returns the base path if overlay is enabled, None otherwise.
202+
pub async fn is_overlay_enabled(&self) -> Result<Option<String>> {
203+
// Check if fs_overlay_config table exists and has base_path
204+
let result = self
205+
.conn
206+
.query(
207+
"SELECT value FROM fs_overlay_config WHERE key = 'base_path'",
208+
(),
209+
)
210+
.await;
211+
212+
match result {
213+
Ok(mut rows) => {
214+
if let Some(row) = rows.next().await? {
215+
let base_path: String = row
216+
.get_value(0)
217+
.ok()
218+
.and_then(|v| {
219+
if let Value::Text(s) = v {
220+
Some(s.clone())
221+
} else {
222+
None
223+
}
224+
})
225+
.unwrap_or_default();
226+
Ok(Some(base_path))
227+
} else {
228+
Ok(None)
229+
}
230+
}
231+
Err(_) => Ok(None), // Table doesn't exist
232+
}
233+
}
234+
199235
/// Validates an agent ID to prevent path traversal and ensure safe filesystem operations.
200236
/// Returns true if the ID contains only alphanumeric characters, hyphens, and underscores.
201237
pub fn validate_agent_id(id: &str) -> bool {

0 commit comments

Comments
 (0)