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
25 changes: 18 additions & 7 deletions cli/src/cmd/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,24 @@ pub async fn init_database(
}

// Check if agent already exists
let db_path = agentfs_dir().join(format!("{}.db", id));
if db_path.exists() && !force {
anyhow::bail!(
"Agent '{}' already exists at '{}'. Use --force to overwrite.",
id,
db_path.display()
);
let db_path = agentfs_dir().join(format!("{}.db", &id));
if db_path.exists() {
if force {
for entry in std::fs::read_dir(agentfs_dir())? {
let entry = entry?;
let file_name = entry.file_name();
if file_name.to_string_lossy().starts_with(&id) {
std::fs::remove_file(entry.path())
.context("Failed to remove existing database file(s)")?;
}
}
} else {
anyhow::bail!(
"Agent '{}' already exists at '{}'. Use --force to overwrite.",
id,
db_path.display()
);
}
}

// Use the SDK to initialize the database - this ensures consistency
Expand Down
18 changes: 18 additions & 0 deletions cli/tests/test-init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,24 @@ echo "$output" | grep -q "Created agent filesystem: .agentfs/$TEST_AGENT_ID.db"
exit 1
}

# Test: Verify --force actually recreates the file (inode should change)
inode_before=$(stat -c %i ".agentfs/${TEST_AGENT_ID}.db")
echo "inode before --force: $inode_before"
if ! output=$(cargo run -- init "$TEST_AGENT_ID" --force 2>&1); then
echo "FAILED: init --force command failed (inode test)"
echo "Output was: $output"
rm -f ".agentfs/${TEST_AGENT_ID}.db" ".agentfs/${TEST_AGENT_ID}.db-shm" ".agentfs/${TEST_AGENT_ID}.db-wal"
exit 1
fi
inode_after=$(stat -c %i ".agentfs/${TEST_AGENT_ID}.db")
echo "inode after --force: $inode_after"
if [ "$inode_before" = "$inode_after" ]; then
echo "FAILED: init --force did not recreate the database file (inode unchanged)"
echo "inode before: $inode_before, inode after: $inode_after"
rm -f ".agentfs/${TEST_AGENT_ID}.db" ".agentfs/${TEST_AGENT_ID}.db-shm" ".agentfs/${TEST_AGENT_ID}.db-wal"
exit 1
fi

# Cleanup test database only
rm -f ".agentfs/${TEST_AGENT_ID}.db" ".agentfs/${TEST_AGENT_ID}.db-shm" ".agentfs/${TEST_AGENT_ID}.db-wal"

Expand Down