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 and targets on read-page

Vinícius DS Andradefb4b5e6b7266
diff --git a/crates/ai-memory-cli/src/commands/read_page.rs b/crates/ai-memory-cli/src/commands/read_page.rs
index 7b115fc7..63894fd5 100644
--- a/crates/ai-memory-cli/src/commands/read_page.rs
+++ b/crates/ai-memory-cli/src/commands/read_page.rs
@@ -37,37 +37,58 @@ struct PageContent {
/// Run the `read-page` subcommand.
///
/// # Errors
-/// Returns an error if the server is unreachable, returns non-2xx,
-/// or neither `--path` nor a query is supplied.
+/// Returns an error if an explicit scope flag, `--path`, or the query is empty;
+/// the server is unreachable; returns non-2xx; or neither `--path` nor a query
+/// is supplied.
pub async fn run(config: &Config, args: ReadPageArgs) -> Result<()> {
- let ep = ServerEndpoint::from_config_resolving_auth(config).await;
+ super::refuse_empty_explicit(args.workspace.as_deref(), "--workspace")?;
+ super::refuse_empty_explicit(args.project.as_deref(), "--project")?;
+ let path = match args.path.as_deref() {
+ Some(p) if p.trim().is_empty() => {
+ bail!("--path was passed empty; specify the wiki page path to read");
+ }
+ Some(p) => Some(p),
+ None => None,
+ };
+ let query = match args.query.as_deref() {
+ Some(q) if q.trim().is_empty() => {
+ bail!("search query was passed empty; specify a query or --path <wiki-path>");
+ }
+ Some(q) => Some(q),
+ None => None,
+ };
+ if path.is_none() && query.is_none() {
+ bail!("provide a query (positional) or --path <wiki-path>");
+ }
+
let (workspace, project) =
super::resolve_scope(config, args.workspace.as_deref(), args.project.as_deref())?;
+ let ep = ServerEndpoint::from_config_resolving_auth(config).await;
- let page: PageContent = if let Some(ref raw_path) = args.path {
+ let page: PageContent = if let Some(raw_path) = path {
get_json(
&ep,
"/admin/read-page",
&[
("workspace", workspace.as_str()),
("project", project.as_str()),
- ("path", raw_path.as_str()),
+ ("path", raw_path),
],
)
.await?
- } else if let Some(ref query) = args.query {
+ } else if let Some(q) = query {
get_json(
&ep,
"/admin/read-page",
&[
("workspace", workspace.as_str()),
("project", project.as_str()),
- ("q", query.as_str()),
+ ("q", q),
],
)
.await?
} else {
- bail!("provide a query (positional) or --path <wiki-path>");
+ unreachable!();
};
if args.json {
@@ -134,4 +155,92 @@ mod tests {
.unwrap();
assert!(!parsed.expired);
}
+
+ fn args(
+ path: Option<&str>,
+ query: Option<&str>,
+ workspace: Option<&str>,
+ project: Option<&str>,
+ ) -> ReadPageArgs {
+ ReadPageArgs {
+ query: query.map(Into::into),
+ path: path.map(Into::into),
+ workspace: workspace.map(Into::into),
+ project: project.map(Into::into),
+ json: false,
+ }
+ }
+
+ #[tokio::test]
+ async fn an_empty_workspace_is_refused_before_scope_or_network_work() {
+ for empty in ["", " \t "] {
+ let error = run(
+ &Config::default(),
+ args(Some("notes/example.md"), None, Some(empty), None),
+ )
+ .await
+ .unwrap_err();
+ assert!(
+ error.to_string().contains("--workspace was passed empty"),
+ "unexpected error: {error}"
+ );
+ }
+ }
+
+ #[tokio::test]
+ async fn an_empty_project_is_refused_before_scope_or_network_work() {
+ for empty in ["", " \t "] {
+ let error = run(
+ &Config::default(),
+ args(Some("notes/example.md"), None, None, Some(empty)),
+ )
+ .await
+ .unwrap_err();
+ assert!(
+ error.to_string().contains("--project was passed empty"),
+ "unexpected error: {error}"
+ );
+ }
+ }
+
+ #[tokio::test]
+ async fn an_empty_path_is_refused_before_scope_or_network_work() {
+ for empty in ["", " \t "] {
+ let error = run(&Config::default(), args(Some(empty), None, None, None))
+ .await
+ .unwrap_err();
+ assert!(
+ error.to_string().contains("--path was passed empty"),
+ "unexpected error: {error}"
+ );
+ }
+ }
+
+ #[tokio::test]
+ async fn an_empty_query_is_refused_before_scope_or_network_work() {
+ for empty in ["", " \t "] {
+ let error = run(&Config::default(), args(None, Some(empty), None, None))
+ .await
+ .unwrap_err();
+ assert!(
+ error
+ .to_string()
+ .contains("search query was passed empty"),
+ "unexpected error: {error}"
+ );
+ }
+ }
+
+ #[tokio::test]
+ async fn missing_both_path_and_query_is_refused_before_scope_or_network_work() {
+ let error = run(&Config::default(), args(None, None, None, None))
+ .await
+ .unwrap_err();
+ assert!(
+ error
+ .to_string()
+ .contains("provide a query (positional) or --path <wiki-path>"),
+ "unexpected error: {error}"
+ );
+ }
}