Skip to content

fix: skip NEAR AI session check when backend is not nearai - #1388

Closed
doismellburning wants to merge 1 commit into
nearai:stagingfrom
doismellburning:fix/doctor-nearai-skip-non-nearai-backend
Closed

doismellburning wants to merge 1 commit into
nearai:stagingfrom
doismellburning:fix/doctor-nearai-skip-non-nearai-backend

Conversation

@doismellburning

Copy link
Copy Markdown
Contributor

Summary

When a user configures a non-NEAR AI backend (e.g. Anthropic), the
doctor command was incorrectly failing with "session file not found"
even though no NEAR AI session is needed. The check now skips with a
descriptive message when LLM_BACKEND is not nearai/near_ai/near.

Change Type

  • Bug fix
  • New feature
  • Refactor
  • Documentation
  • CI/Infrastructure
  • Security
  • Dependencies

Linked Issue

None

Validation

  • cargo fmt
  • cargo clippy --all --benches --tests --examples --all-features
  • Relevant tests pass:
  • Manual testing:

Security Impact

None

Database Impact

None

Blast Radius

ironclaw doctor - potential breakage risk should be low.

Rollback Plan

Straightforward git revert


Review track: B

Copilot AI review requested due to automatic review settings March 18, 2026 23:37
@github-actions github-actions Bot added scope: channel/cli TUI / CLI channel size: S 10-49 changed lines labels Mar 18, 2026
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a bug in the doctor command where it would erroneously attempt to validate a NEAR AI session file even when a different LLM backend was configured. The changes introduce conditional logic to bypass this check, improving the command's robustness and user experience for those utilizing alternative backends.

Highlights

  • Bug Fix: Resolved an issue where the doctor command incorrectly failed with "session file not found" when a non-NEAR AI backend was configured.
  • Conditional Session Check: Implemented logic to skip the NEAR AI session file check if the LLM_BACKEND environment variable is set to a value other than "nearai", "near_ai", or "near".
  • Test Coverage: Added a new test case to ensure the check_nearai_session function correctly skips when a non-NEAR AI backend is specified.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

@github-actions github-actions Bot added risk: low Changes to docs, tests, or low-risk modules contributor: new First-time contributor labels Mar 18, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request correctly fixes an issue where the doctor command would incorrectly check for a NEAR AI session when a different backend was configured. The change properly skips this check. My review includes a suggestion to make the backend detection logic more robust by aligning it with the application's main configuration resolution, which considers settings.json in addition to environment variables. I've also suggested an improvement to the new test to make it more robust against panics.

Comment thread src/cli/doctor.rs Outdated
Comment on lines +220 to +226
let backend = std::env::var("LLM_BACKEND")
.unwrap_or_else(|_| "nearai".into())
.to_ascii_lowercase();
let is_nearai = matches!(backend.as_str(), "nearai" | "near_ai" | "near");
if !is_nearai {
return CheckResult::Skip(format!("not using NEAR AI backend (backend={backend})"));
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The current logic for determining the LLM backend only checks the LLM_BACKEND environment variable. This is inconsistent with the main application's configuration resolution, which also considers settings.json. This can lead to incorrect diagnostics, for example, if the backend is configured in settings.json but not as an environment variable.

To ensure the doctor command provides accurate information, it should use the same configuration resolution logic as the rest of the application. You can achieve this by using LlmConfig::resolve. This also simplifies the backend check, as LlmConfig::resolve normalizes backend aliases.

    let settings = crate::settings::Settings::load();
    let llm_config = match crate::llm::LlmConfig::resolve(&settings) {
        Ok(config) => config,
        Err(e) => {
            // `check_llm_config` will also report this, but we can't proceed.
            return CheckResult::Fail(format!("LLM config error: {e}"));
        }
    };

    if llm_config.backend != "nearai" {
        return CheckResult::Skip(format!(
            "not using NEAR AI backend (backend={})",
            llm_config.backend
        ));
    }

Comment thread src/cli/doctor.rs
Comment on lines +639 to +661
async fn check_nearai_session_skips_for_non_nearai_backend() {
let _guard = crate::config::helpers::ENV_MUTEX.lock().expect("env mutex");
// SAFETY: Under ENV_MUTEX, no concurrent env access.
unsafe {
std::env::set_var("LLM_BACKEND", "anthropic");
}
let result = check_nearai_session().await;
// SAFETY: restore env.
unsafe {
std::env::remove_var("LLM_BACKEND");
}
match result {
CheckResult::Skip(msg) => {
assert!(
msg.contains("backend=anthropic"),
"expected backend name in skip message, got: {msg}"
);
}
other => panic!(
"expected Skip for non-nearai backend, got: {}",
format_result(&other)
),
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This test correctly validates the new skipping logic. However, if check_nearai_session().await were to panic, the environment variable LLM_BACKEND would not be cleaned up. This could affect subsequent tests that rely on the environment state.

To make the test more robust, you can use a RAII guard to ensure the environment variable is always removed, even in case of a panic.

    async fn check_nearai_session_skips_for_non_nearai_backend() {
        struct EnvGuard(&'static str);
        impl Drop for EnvGuard {
            fn drop(&mut self) {
                // SAFETY: Under ENV_MUTEX.
                unsafe { std::env::remove_var(self.0); }
            }
        }

        let _guard = crate::config::helpers::ENV_MUTEX.lock().expect("env mutex");

        // SAFETY: Under ENV_MUTEX, no concurrent env access.
        unsafe {
            std::env::set_var("LLM_BACKEND", "anthropic");
        }
        let _env_guard = EnvGuard("LLM_BACKEND");

        let result = check_nearai_session().await;
        match result {
            CheckResult::Skip(msg) => {
                assert!(
                    msg.contains("backend=anthropic"),
                    "expected backend name in skip message, got: {msg}"
                );
            }
            other => panic!(
                "expected Skip for non-nearai backend, got: {}",
                format_result(&other)
            ),
        }
    }

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adjusts ironclaw doctor to avoid reporting a missing NEAR AI session when the user is configured to use a different LLM backend.

Changes:

  • Added a backend gate in the NEAR AI session diagnostic to return Skip when LLM_BACKEND indicates a non-NEAR AI provider.
  • Added a tokio test asserting the session check skips when LLM_BACKEND=anthropic.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Comment thread src/cli/doctor.rs
Comment on lines +219 to +226
// Skip entirely when the configured backend is not NEAR AI.
let backend = std::env::var("LLM_BACKEND")
.unwrap_or_else(|_| "nearai".into())
.to_ascii_lowercase();
let is_nearai = matches!(backend.as_str(), "nearai" | "near_ai" | "near");
if !is_nearai {
return CheckResult::Skip(format!("not using NEAR AI backend (backend={backend})"));
}
Comment thread src/cli/doctor.rs Outdated
Comment on lines +642 to +648
unsafe {
std::env::set_var("LLM_BACKEND", "anthropic");
}
let result = check_nearai_session().await;
// SAFETY: restore env.
unsafe {
std::env::remove_var("LLM_BACKEND");
Comment thread src/cli/doctor.rs Outdated
Comment on lines +220 to +221
let backend = std::env::var("LLM_BACKEND")
.unwrap_or_else(|_| "nearai".into())
@doismellburning
doismellburning force-pushed the fix/doctor-nearai-skip-non-nearai-backend branch from 3a25c84 to 5da0d6b Compare March 18, 2026 23:55
@github-actions github-actions Bot added size: M 50-199 changed lines and removed size: S 10-49 changed lines labels Mar 18, 2026
When a user configures a non-NEAR AI backend (e.g. Anthropic), the
doctor command was incorrectly failing with "session file not found"
even though no NEAR AI session is needed. The check now skips with a
descriptive message when LLM_BACKEND is not nearai/near_ai/near.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@doismellburning
doismellburning force-pushed the fix/doctor-nearai-skip-non-nearai-backend branch from 5da0d6b to c7b363d Compare March 18, 2026 23:57
@ilblackdragon

Copy link
Copy Markdown
Member

The clippy CI failure is clippy::await_holding_lock at src/cli/doctor.rs:660 — a std::sync::MutexGuard (from ENV_MUTEX.lock()) is held across an .await on line 669.

Fix: Convert the test from #[tokio::test] async fn to #[test] fn using tokio::runtime::Runtime::block_on(), matching the pattern used by all other ENV_MUTEX tests in the codebase.

-    #[tokio::test]
-    async fn check_nearai_session_skips_for_non_nearai_backend() {
+    #[test]
+    fn check_nearai_session_skips_for_non_nearai_backend() {
         struct EnvGuard(&'static str, Option<String>);
         // ... (unchanged) ...

         let settings = Settings::default();
-        let result = check_nearai_session(&settings).await;
+        let rt = tokio::runtime::Runtime::new().expect("tokio runtime");
+        let result = rt.block_on(check_nearai_session(&settings));
         match result {

I've verified this passes all clippy targets and the test itself still passes. I've pushed this fix to fix/doctor-nearai-skip-non-nearai-backend on the main repo — @doismellburning you can cherry-pick commit 8115e79 or apply the diff above.

@ilblackdragon

Copy link
Copy Markdown
Member

Closing in favor of a new PR from the main repo branch with the clippy fix included. Thank you @doismellburning for the contribution!

@doismellburning

Copy link
Copy Markdown
Contributor Author

@ilblackdragon Brilliant thanks - didn't get a chance to sit down at a computer to apply the fix

Should I target main in future? I went with staging because it was the project default

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

contributor: new First-time contributor risk: low Changes to docs, tests, or low-risk modules scope: channel/cli TUI / CLI channel size: M 50-199 changed lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants