mirror of
https://github.com/storytold/craft-anything.git
synced 2026-11-19 05:50:53 +01:00
Refuse a named backend that is off or unknown (R5.5)
This commit is contained in:
1 parent
8eb7e177c8
commit
a380b3ec68
5 files changed
+120
-23
No files matched your search
@@ -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 => {
|
||||
|
||||
@@ -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,40 @@ 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, and say how to turn it on.
|
||||
#[test]
|
||||
fn a_disabled_backend_is_refused_before_a_project_is_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}");
|
||||
}
|
||||
|
||||
/// 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() {
|
||||
|
||||
@@ -1231,17 +1231,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.")
|
||||
})
|
||||
}
|
||||
@@ -2714,6 +2714,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.
|
||||
|
||||
@@ -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() {
|
||||
@@ -518,6 +523,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();
|
||||
|
||||
Reference in new issue
Block a user