Skip to content

Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors

Rust

fix(cli): refuse empty scope flags on write-page

Vinícius DS Andrade87c302ee918c
diff --git a/crates/ai-memory-cli/src/commands/write_page.rs b/crates/ai-memory-cli/src/commands/write_page.rs
index 7fcd164b..d0e60d33 100644
--- a/crates/ai-memory-cli/src/commands/write_page.rs
+++ b/crates/ai-memory-cli/src/commands/write_page.rs
@@ -6,7 +6,7 @@
use std::io::Read;
-use anyhow::{Context, Result};
+use anyhow::{Context, Result, bail};
use serde::{Deserialize, Serialize};
use crate::cli::WritePageArgs;
@@ -43,9 +43,16 @@ struct WritePageResponseBody {
/// Run the `write-page` subcommand.
///
/// # Errors
-/// Returns an error if stdin cannot be read (when `body == "-"`), or if
-/// the POST to `/admin/write-page` fails.
+/// Returns an error if an explicit scope flag is empty, `--path` is empty,
+/// stdin cannot be read (when `body == "-"`), or if the POST to
+/// `/admin/write-page` fails.
pub async fn run(config: &Config, args: WritePageArgs) -> Result<()> {
+ super::refuse_empty_explicit(args.workspace.as_deref(), "--workspace")?;
+ super::refuse_empty_explicit(args.project.as_deref(), "--project")?;
+ if args.path.trim().is_empty() {
+ bail!("--path was passed empty; specify the wiki page path to write");
+ }
+
let body_text = if args.body == "-" {
let mut buf = String::new();
std::io::stdin()
@@ -106,7 +113,7 @@ pub(crate) fn short_page_id(id: &str) -> &str {
#[cfg(test)]
mod tests {
- use super::short_page_id;
+ use super::*;
#[test]
fn short_page_id_keeps_the_random_tail() {
@@ -120,4 +127,66 @@ mod tests {
);
assert_eq!(short_page_id("abc"), "abc");
}
+
+ fn write_args(path: &str, project: Option<&str>) -> WritePageArgs {
+ WritePageArgs {
+ path: path.into(),
+ body: "test body".into(),
+ workspace: None,
+ project: project.map(str::to_string),
+ title: None,
+ kind: None,
+ tier: "semantic".into(),
+ tag: Vec::new(),
+ pinned: false,
+ applies_to: Vec::new(),
+ enforced_by: None,
+ }
+ }
+
+ #[tokio::test]
+ async fn an_empty_project_flag_is_refused_before_any_request() {
+ let config = Config::default();
+ let a = write_args("notes/example.md", Some(" "));
+ let error = run(&config, a).await.unwrap_err();
+ assert!(
+ error.to_string().contains("--project was passed empty"),
+ "unexpected error: {error}"
+ );
+ }
+
+ #[tokio::test]
+ async fn an_empty_workspace_flag_is_refused_before_any_request() {
+ let config = Config::default();
+ let mut a = write_args("notes/example.md", Some("app"));
+ a.workspace = Some("".into());
+ let error = run(&config, a).await.unwrap_err();
+ assert!(
+ error.to_string().contains("--workspace was passed empty"),
+ "unexpected error: {error}"
+ );
+ }
+
+ #[tokio::test]
+ async fn an_empty_path_is_refused_before_any_request() {
+ let config = Config::default();
+ let a = write_args(" ", Some("app"));
+ let error = run(&config, a).await.unwrap_err();
+ assert!(
+ error.to_string().contains("--path was passed empty"),
+ "unexpected error: {error}"
+ );
+ }
+
+ #[tokio::test]
+ async fn an_empty_project_flag_is_refused_before_reading_stdin() {
+ let config = Config::default();
+ let mut a = write_args("notes/example.md", Some(""));
+ a.body = "-".into();
+ let error = run(&config, a).await.unwrap_err();
+ assert!(
+ error.to_string().contains("--project was passed empty"),
+ "unexpected error: {error}"
+ );
+ }
}