Skip to content

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

Merged
ilblackdragon merged 2 commits into
stagingfrom
fix/doctor-nearai-skip-non-nearai-backend
Mar 19, 2026
Merged

ilblackdragon merged 2 commits into
stagingfrom
fix/doctor-nearai-skip-non-nearai-backend

Conversation

@ilblackdragon

Copy link
Copy Markdown
Member

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.

Continuation of #1388 (by @doismellburning) with a clippy fix: converted
the test from #[tokio::test] to #[test] with block_on() to avoid
holding a sync MutexGuard across an await point.

Change Type

  • Bug fix

Linked Issue

Supersedes #1388

Validation

  • cargo fmt
  • cargo clippy --all --benches --tests --examples --all-features — zero warnings
  • All 19 doctor tests pass
  • clippy::await_holding_lock resolved

Security Impact

None

Database Impact

None

Blast Radius

ironclaw doctor — low risk.

Rollback Plan

Straightforward git revert


Review track: B

🤖 Generated with Claude Code

doismellburning and others added 2 commits March 18, 2026 23:57
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>
Convert check_nearai_session_skips_for_non_nearai_backend from
#[tokio::test] to #[test] with block_on, matching the pattern used by
all other ENV_MUTEX tests. Fixes clippy::await_holding_lock error.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings March 19, 2026 17:02
@github-actions github-actions Bot added scope: channel/cli TUI / CLI channel size: M 50-199 changed lines risk: low Changes to docs, tests, or low-risk modules contributor: core 20+ merged PRs labels Mar 19, 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 fixes a bug in the ironclaw doctor command that caused it to fail when a user configured a non-NEAR AI backend. The changes ensure that the NEAR AI session check is only performed when the configured backend is actually NEAR AI, improving the user experience for those using other LLM backends. Additionally, it refactors a test to avoid a potential deadlock situation.

Highlights

  • Bug Fix: Addresses an issue where the doctor command incorrectly failed when a non-NEAR AI backend was configured, due to an unnecessary NEAR AI session check.
  • Conditional Session Check: The NEAR AI session check is now skipped when the LLM_BACKEND is not set to nearai, near_ai, or near, accompanied by a descriptive message.
  • Test Improvement: Refactored a test to avoid holding a sync MutexGuard across an await point, resolving a clippy warning.
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. ↩

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

This PR fixes ironclaw doctor incorrectly failing the “NEAR AI session” check when the configured LLM backend is not NEAR AI, by skipping the session-file validation unless the resolved backend is nearai.

Changes:

  • Pass Settings into the NEAR AI session check and skip it when the resolved backend isn’t NEAR AI.
  • Add a regression test ensuring the session check returns Skip for a non-NEAR-AI backend.
  • Adjust tests to avoid the clippy await_holding_lock pattern by using block_on() in the env-mutation test.

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

@ilblackdragon
ilblackdragon merged commit 71f9012 into staging Mar 19, 2026
18 checks passed
@ilblackdragon
ilblackdragon deleted the fix/doctor-nearai-skip-non-nearai-backend branch March 19, 2026 17:10

@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

The check_nearai_session function in src/cli/doctor.rs was updated to take a Settings argument. When LlmConfig::resolve(settings) fails, the function returns CheckResult::Skip, but the error e is not logged. The reviewer suggests logging the error at the warn level to provide more context for debugging. The EnvGuard struct uses an unsafe block, and the reviewer suggests documenting the safety invariant. The reviewer also suggests ensuring that all tests that modify environment variables use the same mutex to prevent race conditions. Finally, the reviewer suggests that the test check_nearai_session_skips_for_non_nearai_backend creates a new Tokio runtime, which introduces overhead and complexity, and suggests using block_on directly without creating a new runtime, or marking the test as #[tokio::test] if it's intended to be asynchronous.

Comment thread src/cli/doctor.rs
// ── NEAR AI session ─────────────────────────────────────────

async fn check_nearai_session() -> CheckResult {
async fn check_nearai_session(settings: &Settings) -> CheckResult {

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

The function check_nearai_session is now taking settings: &Settings as an argument. This is good for accessing configuration values, but it's important to ensure that all call sites are updated to pass the settings. Double check to make sure all the tests are passing the settings.

Comment thread src/cli/doctor.rs
Comment on lines +222 to +224
Err(e) => {
// check_llm_config will report the full error; just skip here.
return CheckResult::Skip(format!("LLM config error: {e}"));

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

If LlmConfig::resolve(settings) fails, the function returns CheckResult::Skip. However, the error e is not logged. It would be helpful to log the error at warn level to provide more context for debugging, as the skip message only contains a generic "LLM config error". This aligns with the principle of logging warnings when skipping items, as seen in resource cleanup scenarios.

References
  1. Log errors effectively to track application health in production.
  2. When calculating the age of a resource for cleanup, if the creation timestamp is unknown or cannot be parsed, treat it as un-reapable by logging a warning and skipping it.

Comment thread src/cli/doctor.rs
Comment on lines +646 to +657
fn check_nearai_session_skips_for_non_nearai_backend() {
struct EnvGuard(&'static str, Option<String>);
impl Drop for EnvGuard {
fn drop(&mut self) {
// SAFETY: Under ENV_MUTEX.
unsafe {
match &self.1 {
Some(val) => std::env::set_var(self.0, val),
None => std::env::remove_var(self.0),
}
}
}

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

The EnvGuard struct is used to manage environment variables within the test. While this is a common pattern, the unsafe block within the drop function could be concerning. As per repository rules, if unsafe std::env::set_var is used, its safety invariant (e.g., single-threaded context) must be documented in a SAFETY comment. Additionally, ensure that the access to environment variables is properly synchronized to prevent data races. Consider using a more robust library for managing environment variables in tests, if available, to avoid unsafe code.

References
  1. Ensure proper synchronization to prevent data races when accessing shared resources like environment variables.
  2. The use of unsafe std::env::set_var is permissible during application startup if it is guaranteed to be single-threaded (e.g., before tokio::spawn is called) and this safety invariant is documented in a SAFETY comment.
  3. The use of unsafe std::env::set_var is permissible within a single-threaded, sequential context like an interactive setup wizard that runs before any background tasks are spawned. This safety invariant must be documented in a SAFETY comment.

Comment thread src/cli/doctor.rs
Comment on lines +660 to +666
let _mutex = crate::config::helpers::ENV_MUTEX.lock().expect("env mutex");
let prev = std::env::var("LLM_BACKEND").ok();
// SAFETY: Under ENV_MUTEX, no concurrent env access.
unsafe {
std::env::set_var("LLM_BACKEND", "anthropic");
}
let _env_guard = EnvGuard("LLM_BACKEND", prev);

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

The test sets the LLM_BACKEND environment variable to "anthropic" and then restores it using EnvGuard. It relies on crate::config::helpers::ENV_MUTEX for synchronization. While this approach works, it's important to ensure that all tests that modify environment variables use the same mutex to prevent race conditions, as tests that modify shared global state should be serialized. Consider creating a helper function or macro to encapsulate this pattern and ensure consistency across all tests.

References
  1. Ensure proper synchronization to prevent data races when accessing shared resources like environment variables.
  2. Tests that modify shared global state should be serialized using a mutex to prevent race conditions and flakiness when run in parallel.

Comment thread src/cli/doctor.rs
Comment on lines +669 to +670
let rt = tokio::runtime::Runtime::new().expect("tokio runtime");
let result = rt.block_on(check_nearai_session(&settings));

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

The test check_nearai_session_skips_for_non_nearai_backend creates a new Tokio runtime using tokio::runtime::Runtime::new(). Since this test is not marked with #[tokio::test], it's running in a synchronous context. Creating a new Tokio runtime within a synchronous test can introduce overhead and complexity. Consider using block_on directly without creating a new runtime, or mark the test as #[tokio::test] if it's intended to be asynchronous.

jr42 pushed a commit to jr42/ironclaw that referenced this pull request Mar 22, 2026
* fix: skip NEAR AI session check when backend is not nearai

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>

* fix(ci): avoid holding sync MutexGuard across await in doctor test

Convert check_nearai_session_skips_for_non_nearai_backend from
#[tokio::test] to #[test] with block_on, matching the pattern used by
all other ENV_MUTEX tests. Fixes clippy::await_holding_lock error.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Kristian Glass <git@doismellburning.co.uk>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
bkutasi pushed a commit to bkutasi/ironclaw that referenced this pull request Mar 28, 2026
* fix: skip NEAR AI session check when backend is not nearai

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>

* fix(ci): avoid holding sync MutexGuard across await in doctor test

Convert check_nearai_session_skips_for_non_nearai_backend from
#[tokio::test] to #[test] with block_on, matching the pattern used by
all other ENV_MUTEX tests. Fixes clippy::await_holding_lock error.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Kristian Glass <git@doismellburning.co.uk>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
drchirag1991 pushed a commit to drchirag1991/ironclaw that referenced this pull request Apr 8, 2026
* fix: skip NEAR AI session check when backend is not nearai

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>

* fix(ci): avoid holding sync MutexGuard across await in doctor test

Convert check_nearai_session_skips_for_non_nearai_backend from
#[tokio::test] to #[test] with block_on, matching the pattern used by
all other ENV_MUTEX tests. Fixes clippy::await_holding_lock error.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Kristian Glass <git@doismellburning.co.uk>
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

contributor: core 20+ merged PRs 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