Skip to content

Craft Anything: understand how any software works, and craft your own.

Rust

Refuse a switched-off or unknown --backend name (#840)

Alex Joaquim Pereira8314b7e12779
diff --git a/apps/craft-anything-cli/src/rebuild_cmd.rs b/apps/craft-anything-cli/src/rebuild_cmd.rs
index 9359517f..d2b495fc 100644
--- a/apps/craft-anything-cli/src/rebuild_cmd.rs
+++ b/apps/craft-anything-cli/src/rebuild_cmd.rs
@@ -38,7 +38,8 @@ pub struct RebuildArgs {
/// the computer stopped); the request is ignored.
#[arg(long, requires = "continue_in")]
pub resume: bool,
- /// The coding agent to use (a backend name from `ai list`, or an installed agent such as `Claude Code`).
+ /// The coding agent to use (an enabled backend name from `ai list`, or an installed agent
+ /// such as `Claude Code`).
#[arg(long)]
pub backend: Option<String>,
/// Refinement rounds after the first build.
@@ -78,8 +79,8 @@ fn ensure_agent(hub: &AiHub, requested: Option<&str>) -> Result<(), ToolError> {
if requested.is_none() && configured.iter().any(|b| b.enabled) {
return Ok(());
}
- // A name that is configured is used as it is (including a backend that is off): don't add a
- // second one under the same name.
+ // A name that is configured is left alone (including a backend that is off): don't add a
+ // second one under the same name; the run itself refuses a backend that is off.
if requested.is_some_and(|name| configured.iter().any(|b| b.name == name)) {
return Ok(());
}
@@ -103,6 +104,11 @@ fn run(cli: &Cli, a: &RebuildArgs) -> Result<bool, ToolError> {
return Err(ToolError::invalid("--generate-assets is for new projects.").with_remediation("Use project-assets-allow for an existing project."));
}
ensure_agent(&hub, a.backend.as_deref())?;
+ // Refuse an unknown or switched-off name before opening or making a project: a command that
+ // can't build must not leave a new folder behind.
+ if let Some(name) = a.backend.as_deref() {
+ rebuild::builder_backend(&hub.config(), Some(name))?;
+ }
let (ws, message) = match &a.continue_in {
Some(dir) => (Workspace::open(&std::path::absolute(dir).map_err(|e| ToolError::io(dir.display(), e))?)?, (!a.resume).then(|| a.request.clone())),
None => {
diff --git a/apps/craft-anything-cli/tests/rebuild_choice.rs b/apps/craft-anything-cli/tests/rebuild_choice.rs
index 283bd8db..644a0637 100644
--- a/apps/craft-anything-cli/tests/rebuild_choice.rs
+++ b/apps/craft-anything-cli/tests/rebuild_choice.rs
@@ -2,9 +2,10 @@
//! Each test gives the binary its own config, home and `PATH`, so the person's settings and the
//! agents installed on the machine don't decide the result.
//!
-//! The command stops when it opens `--continue-in`'s empty folder, right after the builder check:
-//! the test sees exactly the choice `rebuild` made before doing any work. An invalid command is
-//! refused before that check, so it cannot add an agent either.
+//! A run with no `--backend`, or an enabled one, stops when it opens `--continue-in`'s empty
+//! folder, right after the builder check: the test sees exactly the choice `rebuild` made before
+//! doing any work. An invalid command, an unknown name or a switched-off backend is refused before
+//! that, so it cannot add an agent or leave a new project folder behind.
#![cfg(unix)]
use craft_anything_platform::process::{Output, Process};
@@ -42,6 +43,15 @@ fn api_config(config: &Path) {
.unwrap();
}
+/// A configured backend that is switched off, plus an enabled one for the default path.
+fn disabled_config(config: &Path) {
+ std::fs::write(
+ config.join("config.toml"),
+ "[[ai.backends]]\nname = \"Off\"\nkind = \"anthropic\"\nmodel = \"claude-test\"\nenabled = false\n\n[[ai.backends]]\nname = \"API\"\nkind = \"openai_compatible\"\nmodel = \"test-model\"\nbase_url = \"http://127.0.0.1:9/v1\"\nenabled = true\n",
+ )
+ .unwrap();
+}
+
fn stopped_run(project: &Path, backend: Option<&str>) -> Vec<String> {
let mut args = vec![
"rebuild".to_string(),
@@ -123,9 +133,10 @@ fn a_named_installed_agent_is_added_when_missing() {
assert!(saved.contains("agent_cli") && saved.contains("name = \"Claude Code\""), "the named agent must be saved:\n{saved}");
}
-/// An unknown name must not add a different agent that happens to be installed.
+/// An unknown name is named in the error, and must not add a different agent that happens to be
+/// installed or blame a missing setup.
#[test]
-fn an_unknown_backend_does_not_add_an_agent() {
+fn an_unknown_backend_is_named_and_does_not_add_an_agent() {
let config = tempfile::tempdir().unwrap();
let home = tempfile::tempdir().unwrap();
let bin = tempfile::tempdir().unwrap();
@@ -136,13 +147,53 @@ fn an_unknown_backend_does_not_add_an_agent() {
let refs: Vec<&str> = args.iter().map(String::as_str).collect();
let out = cli(&refs, config.path(), home.path(), bin.path());
let log = out.stderr_text();
- assert!(!out.success, "the empty project folder must stop the run:\n{log}");
- assert!(log.contains("craft.json"), "the run must stop at the missing project, before any build:\n{log}");
+ assert!(!out.success, "an unknown backend must stop the run:\n{log}");
+ assert!(log.contains("No AI backend named") && log.contains("No Such AI"), "the error must name the name:\n{log}");
+ assert!(!log.contains("Building needs a coding agent"), "the error must not blame the setup:\n{log}");
assert!(!log.contains("Using "), "an unknown name must not pick an installed agent:\n{log}");
let saved = std::fs::read_to_string(config.path().join("config.toml")).unwrap_or_default();
assert!(!saved.contains("agent_cli"), "an unknown name must not save an agent:\n{saved}");
}
+/// A backend the person switched off must not be used just because it was named: refuse it before
+/// a project is made or opened, and say how to turn it on.
+#[test]
+fn a_disabled_backend_is_refused_before_a_project_is_opened_or_created() {
+ let config = tempfile::tempdir().unwrap();
+ let home = tempfile::tempdir().unwrap();
+ let bin = tempfile::tempdir().unwrap();
+ let parent = tempfile::tempdir().unwrap();
+ disabled_config(config.path());
+ fake_claude(bin.path());
+
+ let project = parent.path().join("new-project");
+ let out = cli(
+ &["rebuild", "-", "make a small page", "--project-folder", project.to_str().unwrap(), "--backend", "Off", "--rounds", "0", "--unattended"],
+ config.path(),
+ home.path(),
+ bin.path(),
+ );
+ let log = out.stderr_text();
+ assert!(!out.success, "the switched-off backend must stop the run:\n{log}");
+ assert!(log.contains("Off") && log.contains("turned off"), "the error must name the backend and its state:\n{log}");
+ assert!(log.contains("Turn it on"), "the error must say how to fix it:\n{log}");
+ assert!(!log.contains("Project:"), "it must stop before a project is made:\n{log}");
+ assert!(!project.exists(), "a refused command must not leave a project folder:\n{log}");
+
+ // An existing project is refused before it is opened or a build starts.
+ let existing = tempfile::tempdir().unwrap();
+ let out = cli(
+ &["rebuild", "-", "make a small page", "--continue-in", existing.path().to_str().unwrap(), "--backend", "Off", "--rounds", "0", "--unattended"],
+ config.path(),
+ home.path(),
+ bin.path(),
+ );
+ let log = out.stderr_text();
+ assert!(!out.success, "the switched-off backend must stop an existing project too:\n{log}");
+ assert!(log.contains("Off") && log.contains("turned off"), "the error must name the backend and its state:\n{log}");
+ assert!(!log.contains("craft.json") && !log.contains("Couldn't reach"), "it must stop before opening or building:\n{log}");
+}
+
/// A command refused before any build starts must not change the person's AI settings either.
#[test]
fn an_invalid_command_does_not_add_an_agent() {
diff --git a/crates/ai/src/rebuild/mod.rs b/crates/ai/src/rebuild/mod.rs
index fbd43077..48d129d0 100644
--- a/crates/ai/src/rebuild/mod.rs
+++ b/crates/ai/src/rebuild/mod.rs
@@ -1236,17 +1236,28 @@ pub fn project_builder<'a>(cfg: &crate::AiConfig, ws: &'a Workspace) -> Option<&
ws.manifest.builder.as_deref().filter(|n| cfg.get(n).is_some_and(|b| b.config.enabled))
}
-/// The backend that builds: the one named, else the Builder role's, else an agent app, else any
-/// enabled AI backend (which builds with our own agent loop and the Build tools).
+/// The backend that builds: the one named while it is set up and on, else the Builder role's,
+/// else an agent app, else any enabled AI backend (which builds with our own agent loop and the
+/// Build tools).
pub fn builder_backend(cfg: &crate::AiConfig, named: Option<&str>) -> Result<crate::BackendEntry, ToolError> {
- let found = match named {
- Some(n) => cfg.get(n).cloned(),
- None => {
- cfg.backend_for(Role::Builder).or_else(|| cfg.enabled().find(|b| b.config.kind == BackendKind::AgentCli)).or_else(|| cfg.enabled().next()).cloned()
+ if let Some(n) = named {
+ let Some(entry) = cfg.get(n) else {
+ return Err(
+ ToolError::invalid(format!("No AI backend named {n:?}")).with_remediation("See `craft-anything-cli ai list`, or add one in Settings → AI.")
+ );
+ };
+ if !entry.config.enabled {
+ return Err(ToolError::new(ErrorCode::BadSettings, format!("{} is turned off.", entry.config.name))
+ .with_remediation("Turn it on in Settings → AI, or choose another backend for this build."));
}
- };
- found.ok_or_else(|| {
- ToolError::new(ErrorCode::BadSettings, "Building needs a coding agent (such as Claude Code) or an AI backend with an API key.")
- .with_remediation("Install a coding agent or add an API key in Settings → AI, then try again.")
- })
+ return Ok(entry.clone());
+ }
+ cfg.backend_for(Role::Builder)
+ .or_else(|| cfg.enabled().find(|b| b.config.kind == BackendKind::AgentCli))
+ .or_else(|| cfg.enabled().next())
+ .cloned()
+ .ok_or_else(|| {
+ ToolError::new(ErrorCode::BadSettings, "Building needs a coding agent (such as Claude Code) or an AI backend with an API key.")
+ .with_remediation("Install a coding agent or add an API key in Settings → AI, then try again.")
+ })
}
diff --git a/crates/ai/src/rebuild/tests.rs b/crates/ai/src/rebuild/tests.rs
index aeeb5942..2913566d 100644
--- a/crates/ai/src/rebuild/tests.rs
+++ b/crates/ai/src/rebuild/tests.rs
@@ -2822,6 +2822,28 @@ fn a_project_is_built_by_the_ai_it_was_started_with_while_that_one_is_on() {
assert_eq!(builder_backend(&cfg, super::project_builder(&cfg, &ws)).unwrap().config.name, "Claude Code");
}
+/// A name given on purpose is never ignored: a backend that is off is refused in words that say
+/// how to fix it, and a name that matches nothing names the name instead of blaming the setup.
+#[test]
+fn a_named_backend_that_is_off_or_unknown_is_refused() {
+ use crate::{AiConfig, BackendConfig, BackendEntry, BackendKind};
+ let mut cfg = AiConfig::default();
+ for (name, enabled) in [("Claude Code", true), ("Codex", false)] {
+ let c = BackendConfig { name: name.into(), kind: BackendKind::AgentCli, model: String::new(), base_url: None, ssh_host: None, enabled };
+ cfg.upsert(BackendEntry { config: c, options: Default::default() });
+ }
+ assert_eq!(builder_backend(&cfg, Some("Claude Code")).unwrap().config.name, "Claude Code");
+
+ let off = builder_backend(&cfg, Some("Codex")).unwrap_err();
+ assert_eq!(off.code, ErrorCode::BadSettings);
+ assert!(off.message.contains("Codex") && off.message.contains("turned off"), "{off:?}");
+ assert!(off.remediation.as_deref().is_some_and(|r| r.contains("Turn it on")), "{off:?}");
+
+ let unknown = builder_backend(&cfg, Some("No Such AI")).unwrap_err();
+ assert!(unknown.message.contains("No AI backend named"), "{unknown:?}");
+ assert!(unknown.remediation.as_deref().is_some_and(|r| r.contains("ai list")), "{unknown:?}");
+}
+
/// #446: a project copied from another computer keeps the id of a Claude Code session that lives
/// only there. The builder (a stand-in that answers like Claude Code 2.1) can't resume it, so the
/// turn starts a new session from the brief and what's in app/, and keeps the new id.
diff --git a/crates/ai/src/rebuild/tools.rs b/crates/ai/src/rebuild/tools.rs
index 9b65612f..18a45ee3 100644
--- a/crates/ai/src/rebuild/tools.rs
+++ b/crates/ai/src/rebuild/tools.rs
@@ -149,8 +149,8 @@ pub struct StartParams {
/// project_assets_allow.
#[serde(default)]
pub generate_assets: bool,
- /// The coding agent to use (a backend name), kept with a new project for its later changes;
- /// default: the one set up for building.
+ /// The coding agent to use (an enabled backend name), kept with a new project for its later
+ /// changes; default: the one set up for building.
#[serde(default)]
pub backend: Option<String>,
/// Refinement rounds after the first build (default 2).
@@ -301,6 +301,11 @@ fn rebuild_start(ctx: &mut Ctx<'_>, params: Value) -> Result<ToolOutput, ToolErr
})?;
let runs = runs(ctx);
let settings: &Settings = &ctx.session.settings;
+ // Refuse an unknown or switched-off name before opening or making a project or adding pictures:
+ // a refused start must not leave anything behind.
+ if let Some(name) = p.backend.as_deref() {
+ builder_backend(&crate::AiConfig::from_settings(settings), Some(name))?;
+ }
let (ws, message) = match &p.project {
Some(dir) => {
if p.build_with.is_some() {
@@ -520,6 +525,21 @@ mod tests {
assert!(e.call("rebuild_stop", json!({"project": ws.root}), ok()).is_err());
}
+ #[test]
+ fn a_disabled_or_unknown_backend_is_refused_before_the_tool_makes_a_project() {
+ let d = tempfile::tempdir().unwrap();
+ let mut s = craft_anything_engine::testing::session();
+ s.settings.ai = toml::from_str("[[backends]]\nname = \"Off\"\nkind = \"anthropic\"\nmodel = \"claude-test\"\nenabled = false\n").unwrap();
+ let e = Engine::shared(s, specs());
+ let ok = || CallOptions::new(Caller::Cli).with_consent(Consent::Granted);
+ for (backend, expected) in [("Off", "turned off"), ("No Such AI", "No AI backend named")] {
+ let p = json!({"request": "A page", "target": "-", "projects_dir": d.path(), "backend": backend});
+ let err = e.call("rebuild_start", p, ok()).unwrap_err();
+ assert!(err.message.contains(expected), "{backend}: {err:?}");
+ assert_eq!(std::fs::read_dir(d.path()).unwrap().count(), 0, "{backend}: a refused start must not make a project");
+ }
+ }
+
#[test]
fn the_builder_asks_the_running_build_for_a_check_and_gets_its_report() {
let d = tempfile::tempdir().unwrap();
diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md
index b4df2fe3..cca4db69 100644
--- a/docs/ROADMAP.md
+++ b/docs/ROADMAP.md
@@ -339,8 +339,13 @@ Owner: open · Status: todo
settings and prints "Using Claude Code (…)": `ensure_agent` (`rebuild_cmd.rs`) runs whenever no
agent app is set up, though this run won't use one. Run it only when the build needs one
— claimed: AlexJoaquimPereira, branch `bugfix/r5.5-no-agent-with-api-backend`
-- [ ] `--backend <a name that is turned off>` still builds with it: `builder_backend`'s named
- branch doesn't check `enabled`, while Settings treats an AI that is off as unusable
+- [x] `--backend <a name that is turned off or unknown>` is refused with a specific message: a
+ turned-off backend is named and must be turned on, and a name that matches nothing is named
+ instead of saying to install an agent or add a key
+ — claimed: AlexJoaquimPereira, branch `bugfix/r5.5-disabled-backend-name`; commit a380b3e
+- [ ] When a project's saved builder was turned off, Start builds with the default AI without
+ saying so: the UI picker filters the stale name out (`ui-egui/src/builders.rs`) instead of
+ reporting that the chosen AI was replaced
- [x] Bound accumulated data across all lines of one server-sent event, so short lines cannot
grow a streamed API response without limit
— claimed: Afloat16, branch bugfix/r5.5-bounded-sse-events-20261103
@@ -694,6 +699,8 @@ Gaps found while using the app for real projects.
- [x] A prompt brought back with Up keeps its pictures (their paths are absolute, so they don't
come back after the project is moved)
- [ ] Change a project's builder from its page (a tool, so the CLI and MCP can too) — in PR #245
+- [ ] `chat --backend <a name that is turned off>` (and `compare`) still talks to it:
+ `AiHub::start` accepts a named backend without checking `enabled`, unlike a build
- [x] Ctrl+V (Shift+Insert, Cmd+V) in a prompt box that takes pictures pastes a copied image (saved
as a PNG) or copied picture files; attachments show as square picture tiles, the whole
picture on hover, decoded on a worker