diff --git a/.agents/AGENTS.md b/.agents/AGENTS.md index 3d671e3..e8db9b6 100644 --- a/.agents/AGENTS.md +++ b/.agents/AGENTS.md @@ -13,7 +13,9 @@ cargo clippy --all-targets -- -D warnings cargo fmt -- --check ``` -The build **requires** these env vars (read from `.env` via `build.rs`): `ONDE_APP_ID`, `ONDE_APP_SECRET`, `GRESIQ_API_KEY`, `GRESIQ_API_SECRET`. `HF_TOKEN` is optional. They are baked in at compile time via `env!(...)` in `src/app.rs`. Changing `.env` triggers a full rebuild. +`build.rs` reads these env vars (from `.env` or the environment) and bakes them in at compile time with `env!(...)` in `src/config.rs`: `ONDE_APP_ID`, `ONDE_APP_SECRET`, `GRESIQ_API_KEY`, `GRESIQ_API_SECRET`, and the optional `HF_TOKEN`. Changing `.env` triggers a full rebuild. + +They are optional. A build without them (a contributor's checkout, `cargo install`) still fine-tunes, merges, exports and evaluates, and `config::is_connected()` is false, so sign-in, publishing and app assignment answer with `config::NOT_CONNECTED`. Official builds must carry them, so the release workflows set `ONDE_REQUIRE_CREDENTIALS=1`, which makes a missing value fail the build. Any new cloud entry point has to check `is_connected()` (or go through `gresiq.rs`, which does). On macOS, candle's `metal` + `accelerate` features are added automatically. **Fine-tuning always runs on CPU** — Metal's backward pass in candle 0.10.2 produces corrupt gradients (1e6–1e29 in magnitude, where CPU gives ~40–65 for identical losses). Use `ONDE_FINETUNE_METAL=1` only to test newer candle versions, and verify with `ONDE_FINETUNE_GRAD_DEBUG=1` (per-step loss/max_abs/norm). diff --git a/.github/workflows/release-crates.yml b/.github/workflows/release-crates.yml index 252a409..47b7689 100644 --- a/.github/workflows/release-crates.yml +++ b/.github/workflows/release-crates.yml @@ -24,6 +24,8 @@ concurrency: env: CARGO_TERM_COLOR: always + # A release must never ship without the credentials, so a missing one fails the build. + ONDE_REQUIRE_CREDENTIALS: "1" ONDE_APP_ID: ${{ secrets.ONDE_APP_ID }} ONDE_APP_SECRET: ${{ secrets.ONDE_APP_SECRET }} GRESIQ_API_KEY: ${{ secrets.GRESIQ_API_KEY }} diff --git a/.github/workflows/release-desktop-direct.yml b/.github/workflows/release-desktop-direct.yml index 6ccec20..9866d5c 100644 --- a/.github/workflows/release-desktop-direct.yml +++ b/.github/workflows/release-desktop-direct.yml @@ -36,6 +36,8 @@ jobs: permissions: contents: write env: + # A release must never ship without the credentials, so a missing one fails the build. + ONDE_REQUIRE_CREDENTIALS: "1" ONDE_APP_ID: ${{ secrets.ONDE_APP_ID }} ONDE_APP_SECRET: ${{ secrets.ONDE_APP_SECRET }} GRESIQ_API_KEY: ${{ secrets.GRESIQ_API_KEY }} diff --git a/.github/workflows/release-desktop-macos-appstore.yml b/.github/workflows/release-desktop-macos-appstore.yml index 2a4ea1b..7caa580 100644 --- a/.github/workflows/release-desktop-macos-appstore.yml +++ b/.github/workflows/release-desktop-macos-appstore.yml @@ -37,6 +37,8 @@ jobs: runs-on: macos-26 timeout-minutes: 120 env: + # A release must never ship without the credentials, so a missing one fails the build. + ONDE_REQUIRE_CREDENTIALS: "1" ONDE_APP_ID: ${{ secrets.ONDE_APP_ID }} ONDE_APP_SECRET: ${{ secrets.ONDE_APP_SECRET }} GRESIQ_API_KEY: ${{ secrets.GRESIQ_API_KEY }} diff --git a/.github/workflows/release-github.yml b/.github/workflows/release-github.yml index 533e36c..c7f3fcd 100644 --- a/.github/workflows/release-github.yml +++ b/.github/workflows/release-github.yml @@ -15,6 +15,8 @@ permissions: env: PROJECT_NAME: onde CARGO_TERM_COLOR: always + # A release must never ship without the credentials, so a missing one fails the build. + ONDE_REQUIRE_CREDENTIALS: "1" ONDE_APP_ID: ${{ secrets.ONDE_APP_ID }} ONDE_APP_SECRET: ${{ secrets.ONDE_APP_SECRET }} GRESIQ_API_KEY: ${{ secrets.GRESIQ_API_KEY }} diff --git a/.github/workflows/release-npm.yml b/.github/workflows/release-npm.yml index 0200df4..0408e13 100644 --- a/.github/workflows/release-npm.yml +++ b/.github/workflows/release-npm.yml @@ -20,6 +20,8 @@ permissions: env: CARGO_TERM_COLOR: always + # A release must never ship without the credentials, so a missing one fails the build. + ONDE_REQUIRE_CREDENTIALS: "1" ONDE_APP_ID: ${{ secrets.ONDE_APP_ID }} ONDE_APP_SECRET: ${{ secrets.ONDE_APP_SECRET }} GRESIQ_API_KEY: ${{ secrets.GRESIQ_API_KEY }} diff --git a/.github/workflows/release-nuget.yml b/.github/workflows/release-nuget.yml index b8d0f47..6a99209 100644 --- a/.github/workflows/release-nuget.yml +++ b/.github/workflows/release-nuget.yml @@ -23,6 +23,8 @@ env: CARGO_TERM_COLOR: always DOTNET_VERSION: 8.0.x NUGET_PACKAGE_ID: Onde.Cli + # A release must never ship without the credentials, so a missing one fails the build. + ONDE_REQUIRE_CREDENTIALS: "1" ONDE_APP_ID: ${{ secrets.ONDE_APP_ID }} ONDE_APP_SECRET: ${{ secrets.ONDE_APP_SECRET }} GRESIQ_API_KEY: ${{ secrets.GRESIQ_API_KEY }} diff --git a/.github/workflows/release-pypi.yml b/.github/workflows/release-pypi.yml index 717ec87..28712e7 100644 --- a/.github/workflows/release-pypi.yml +++ b/.github/workflows/release-pypi.yml @@ -14,6 +14,8 @@ permissions: env: CARGO_TERM_COLOR: always + # A release must never ship without the credentials, so a missing one fails the build. + ONDE_REQUIRE_CREDENTIALS: "1" ONDE_APP_ID: ${{ secrets.ONDE_APP_ID }} ONDE_APP_SECRET: ${{ secrets.ONDE_APP_SECRET }} GRESIQ_API_KEY: ${{ secrets.GRESIQ_API_KEY }} diff --git a/README.md b/README.md index 6b9ea03..cbaa261 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,8 @@ cargo install onde-cli ``` More ways, including pip/uv, .NET, Dart pub, winget, Scoop and pre-built binaries, are in -[docs/install.md](docs/install.md). +[docs/install.md](docs/install.md). A `cargo install` build works on your machine but has cloud +sign-in off; [why](docs/install.md#build-from-source). ## Use it diff --git a/build.rs b/build.rs index d09371d..cc98af5 100644 --- a/build.rs +++ b/build.rs @@ -2,7 +2,14 @@ fn main() { // Load .env for local builds. In CI these vars already come from the environment. let _ = dotenvy::dotenv(); - // These must be present or the build should fail. + // Official builds carry the Onde credentials and must never ship without + // them, so the release workflows set ONDE_REQUIRE_CREDENTIALS and a missing + // value stops the build. Anyone else (a contributor, `cargo install`) gets a + // build with empty credentials: fine-tuning, merging, export and eval all + // work, and the cloud features say that this build is not connected. + println!("cargo:rerun-if-env-changed=ONDE_REQUIRE_CREDENTIALS"); + let required = std::env::var("ONDE_REQUIRE_CREDENTIALS").is_ok_and(|v| !v.is_empty()); + let mut missing = Vec::new(); for var in [ "ONDE_APP_ID", "ONDE_APP_SECRET", @@ -11,17 +18,38 @@ fn main() { ] { println!("cargo:rerun-if-env-changed={var}"); - match std::env::var(var) { - Ok(val) => { - println!("cargo:rustc-env={var}={val}"); - } - Err(_) => { - eprintln!("error: required build variable `{var}` is not set."); - eprintln!(" → Add it to .env for local builds."); - eprintln!(" → Add it as a GitHub Actions secret for CI builds."); - std::process::exit(1); - } + // An unset variable and one set to an empty string both leave the + // build disconnected, so treat them the same. Otherwise `ONDE_APP_ID=` + // in .env or an empty CI secret would slip past the required-creds gate + // and ship a release that cannot reach the backend. + let val = std::env::var(var).unwrap_or_default(); + println!("cargo:rustc-env={var}={val}"); + if val.is_empty() { + missing.push(var); + } + } + if !missing.is_empty() { + if required { + eprintln!( + "error: required build variable(s) not set or empty: {}.", + missing.join(", ") + ); + eprintln!(" → Add them to .env for local builds."); + eprintln!(" → Add them as GitHub Actions secrets for CI builds."); + std::process::exit(1); } + // Name only the features the missing vars actually gate. The Onde auth + // keys gate sign-in (and everything behind it); the GresIQ keys gate + // publishing and app assignment on their own. + let off = if missing.iter().any(|v| v.starts_with("ONDE_")) { + "cloud sign-in, publishing and app assignment are off" + } else { + "publishing and app assignment are off" + }; + println!( + "cargo:warning=building without some Onde credentials ({}); {off} in this build", + missing.join(", ") + ); } // Optional vars: bake them in when present, use an empty string otherwise. diff --git a/docs/install.md b/docs/install.md index 6657b9a..405d4ef 100644 --- a/docs/install.md +++ b/docs/install.md @@ -53,6 +53,16 @@ scoop bucket add onde https://github.com/ondeinference/scoop-bucket scoop install onde ``` +## Build from source + +```sh +cargo install onde-cli # from crates.io +# or, in a checkout: +cargo build --release +``` + +A build you make yourself has no Onde credentials. It fine-tunes, merges, exports to GGUF and evaluates models on your machine, and chat works. Cloud sign-in, publishing to Hugging Face through your Onde account and assigning models to apps are off, and say so when you try them. The binaries on GitHub Releases and in the package managers include the credentials. To get them in a build of your own you need the Onde values in a `.env` file or the environment. + ## Pre-built binary Download a release from [GitHub Releases](https://github.com/ondeinference/onde-cli/releases): diff --git a/packaging/app/src-tauri/src/commands/oauth.rs b/packaging/app/src-tauri/src/commands/oauth.rs index 477f4e6..b230d64 100644 --- a/packaging/app/src-tauri/src/commands/oauth.rs +++ b/packaging/app/src-tauri/src/commands/oauth.rs @@ -46,6 +46,7 @@ pub async fn oauth_sign_in( pending: State<'_, PendingSignIn>, provider: Provider, ) -> Result { + crate::desktop::auth::ensure_connected()?; let state = oauth::new_state(); let cancelled = pending.begin(); diff --git a/packaging/app/src-tauri/src/desktop/auth.rs b/packaging/app/src-tauri/src/desktop/auth.rs index f1ad033..3187151 100644 --- a/packaging/app/src-tauri/src/desktop/auth.rs +++ b/packaging/app/src-tauri/src/desktop/auth.rs @@ -1,5 +1,8 @@ use crate::error::CommandError; -use onde_cli::{config::credentials, token}; +use onde_cli::{ + config::{NOT_CONNECTED, credentials, is_connected}, + token, +}; use serde::Serialize; use smbcloud_auth_sdk::{ login::login_with_client, logout::logout_with_client, me::me_with_client, @@ -25,6 +28,9 @@ fn error_text(e: &ErrorResponse) -> String { /// The signed-in user, if a saved token is still valid. A rejected token is /// cleared so the next launch starts signed out. pub async fn restore() -> Option { + if !is_connected() { + return None; + } let saved = token::load()?; match me_with_client(Environment::Production, credentials(), &saved).await { Ok(user) => Some(Profile { email: user.email }), @@ -61,7 +67,17 @@ pub async fn finish_sign_in( } } +/// Refuses cloud sign-in in a build that carries no Onde credentials. +pub fn ensure_connected() -> Result<(), CommandError> { + if is_connected() { + Ok(()) + } else { + Err(CommandError::Auth(NOT_CONNECTED.into())) + } +} + pub async fn sign_in(email: String, password: String) -> Result { + ensure_connected()?; let status = login_with_client( Environment::Production, credentials(), @@ -83,6 +99,7 @@ pub async fn sign_in(email: String, password: String) -> Result Result { + ensure_connected()?; signup_with_client(Environment::Production, credentials(), email, password) .await .map(|r| r.message) diff --git a/src/app.rs b/src/app.rs index 01a0c3c..f34e262 100644 --- a/src/app.rs +++ b/src/app.rs @@ -853,7 +853,9 @@ pub async fn run(terminal: &mut ratatui::Terminal< let mut events = EventStream::new(); // pick up where we left off if there is a saved token - if let Some(saved_token) = token::load() { + if crate::config::is_connected() + && let Some(saved_token) = token::load() + { app.busy = true; app.status = Status::neutral("Restoring session…"); let tx2 = tx.clone(); @@ -2795,6 +2797,11 @@ fn submit(app: &mut App, tx: mpsc::UnboundedSender) { let email = app.email.trim().to_string(); let password = app.password.clone(); + if !crate::config::is_connected() { + app.status = Status::error(crate::config::NOT_CONNECTED); + return; + } + if email.is_empty() || password.is_empty() { app.status = Status::error("Email and password are required."); return; diff --git a/src/config.rs b/src/config.rs index bdf3d7b..d5423bd 100644 --- a/src/config.rs +++ b/src/config.rs @@ -13,6 +13,30 @@ pub const HF_TOKEN: &str = env!("HF_TOKEN"); pub(crate) const GRESIQ_API_KEY: &str = env!("GRESIQ_API_KEY"); pub(crate) const GRESIQ_API_SECRET: &str = env!("GRESIQ_API_SECRET"); +/// What a cloud feature says when this build carries no Onde auth credentials: +/// a source build made without a `.env`, or `cargo install`. Sign-in is off, +/// and with no way to sign in, publishing and app assignment are too. +pub const NOT_CONNECTED: &str = "This build has no Onde credentials, so cloud sign-in, publishing and app assignment are off. Fine-tuning, merging, export and eval still work. Official releases include the credentials."; + +/// What the apps-and-models features say when sign-in works (the Onde auth +/// credentials are present) but the GresIQ keys are missing. Reaching this +/// path means the user already signed in, so it does not claim sign-in is off. +pub const NOT_CONNECTED_GRESIQ: &str = "This build has no GresIQ credentials, so publishing and app assignment are off. Sign-in and the local tools still work. Official releases include the credentials."; + +fn connected(app_id: &str, app_secret: &str) -> bool { + !app_id.is_empty() && !app_secret.is_empty() +} + +/// True when this build can talk to Onde's auth backend. +pub fn is_connected() -> bool { + connected(ONDE_APP_ID, ONDE_APP_SECRET) +} + +/// True when this build can talk to GresIQ (apps and models). +pub fn gresiq_connected() -> bool { + connected(GRESIQ_API_KEY, GRESIQ_API_SECRET) +} + /// The Auth client credentials identifying this build to the backend. pub fn credentials() -> ClientCredentials<'static> { ClientCredentials { @@ -20,3 +44,16 @@ pub fn credentials() -> ClientCredentials<'static> { app_secret: ONDE_APP_SECRET, } } + +#[cfg(test)] +mod tests { + use super::connected; + + #[test] + fn a_build_is_connected_only_with_both_values() { + assert!(connected("id", "secret")); + assert!(!connected("", "secret")); + assert!(!connected("id", "")); + assert!(!connected("", "")); + } +} diff --git a/src/gresiq.rs b/src/gresiq.rs index 4808cdf..1e9c12e 100644 --- a/src/gresiq.rs +++ b/src/gresiq.rs @@ -2,34 +2,34 @@ //! need to pass the user's access token. use smbcloud_gresiq_sdk::{CreateModelParams, Environment, OndeApp, OndeModel}; -fn api_key() -> &'static str { - crate::config::GRESIQ_API_KEY -} - -fn api_secret() -> &'static str { - crate::config::GRESIQ_API_SECRET +/// The GresIQ key and secret, or an error saying this build has none. +fn keys() -> anyhow::Result<(&'static str, &'static str)> { + if !crate::config::gresiq_connected() { + anyhow::bail!(crate::config::NOT_CONNECTED_GRESIQ); + } + Ok(( + crate::config::GRESIQ_API_KEY, + crate::config::GRESIQ_API_SECRET, + )) } pub async fn load_apps(token: &str) -> anyhow::Result> { - smbcloud_gresiq_sdk::list_apps(&Environment::Production, api_key(), api_secret(), token) + let (api_key, api_secret) = keys()?; + smbcloud_gresiq_sdk::list_apps(&Environment::Production, api_key, api_secret, token) .await .map_err(|e| anyhow::anyhow!("{e}")) } pub async fn create_app(token: &str, name: &str) -> anyhow::Result { - smbcloud_gresiq_sdk::create_app( - &Environment::Production, - api_key(), - api_secret(), - token, - name, - ) - .await - .map_err(|e| anyhow::anyhow!("{e}")) + let (api_key, api_secret) = keys()?; + smbcloud_gresiq_sdk::create_app(&Environment::Production, api_key, api_secret, token, name) + .await + .map_err(|e| anyhow::anyhow!("{e}")) } pub async fn load_models(token: &str) -> anyhow::Result> { - smbcloud_gresiq_sdk::list_models(&Environment::Production, api_key(), api_secret(), token) + let (api_key, api_secret) = keys()?; + smbcloud_gresiq_sdk::list_models(&Environment::Production, api_key, api_secret, token) .await .map_err(|e| anyhow::anyhow!("{e}")) } @@ -46,10 +46,11 @@ pub async fn create_model( gguf_file: Option<&str>, approx_size_bytes: Option, ) -> anyhow::Result { + let (api_key, api_secret) = keys()?; smbcloud_gresiq_sdk::create_model( &Environment::Production, - api_key(), - api_secret(), + api_key, + api_secret, token, CreateModelParams { hf_repo_id, @@ -69,10 +70,11 @@ pub async fn create_model( } pub async fn assign_model(token: &str, onde_app_id: &str, model_id: &str) -> anyhow::Result<()> { + let (api_key, api_secret) = keys()?; smbcloud_gresiq_sdk::assign_model( &Environment::Production, - api_key(), - api_secret(), + api_key, + api_secret, token, onde_app_id, model_id, @@ -82,10 +84,11 @@ pub async fn assign_model(token: &str, onde_app_id: &str, model_id: &str) -> any } pub async fn rename_app(token: &str, onde_app_id: &str, new_name: &str) -> anyhow::Result { + let (api_key, api_secret) = keys()?; smbcloud_gresiq_sdk::rename_app( &Environment::Production, - api_key(), - api_secret(), + api_key, + api_secret, token, onde_app_id, new_name, diff --git a/src/mcp.rs b/src/mcp.rs index 8b0775c..b2531f6 100644 --- a/src/mcp.rs +++ b/src/mcp.rs @@ -58,6 +58,18 @@ impl OndeMcpServer { } } +/// The auth tools need the credentials baked into official builds. +fn require_connected() -> Result<(), ErrorData> { + if crate::config::is_connected() { + Ok(()) + } else { + Err(ErrorData::invalid_request( + crate::config::NOT_CONNECTED, + None, + )) + } +} + impl Default for OndeMcpServer { fn default() -> Self { Self::new() @@ -147,6 +159,7 @@ impl OndeMcpServer { &self, Parameters(args): Parameters, ) -> Result { + require_connected()?; let status = login_with_client( Environment::Production, credentials(), @@ -189,6 +202,7 @@ impl OndeMcpServer { `login`; returns the user as JSON." )] async fn me(&self) -> Result { + require_connected()?; let token = self.access_token()?; let user = me_with_client(Environment::Production, credentials(), &token) .await