Skip to content

test(local-ai): serialize Ollama env mutations#1656

Merged
senamakel merged 1 commit into
tinyhumansai:mainfrom
Zavianx:fix/local-ai-env-test-isolation
May 13, 2026
Merged

test(local-ai): serialize Ollama env mutations#1656
senamakel merged 1 commit into
tinyhumansai:mainfrom
Zavianx:fix/local-ai-env-test-isolation

Conversation

@Zavianx
Copy link
Copy Markdown
Contributor

@Zavianx Zavianx commented May 13, 2026

Summary

  • add a poison-recovering local_ai_test_guard() wrapper around the existing local-AI test mutex
  • route Ollama-related test env mutations through the same local-AI mutex, including OPENHUMAN_OLLAMA_BASE_URL, OLLAMA_BIN, and local PATH lookups
  • align memory/voice/local-ai tests with the domain mutex documented in src/openhuman/local_ai/README.md

Why

While validating #1589 locally, the Rust coverage path exposed a flaky race where one test could mutate or clear Ollama process-global env vars while another local-AI test was reading them. This keeps the existing test-only isolation model but applies it consistently across the affected modules. No production behavior changes.

Test plan

  • cargo fmt --check
  • CARGO_INCREMENTAL=0 cargo test -p openhuman openhuman::local_ai -- --nocapture
  • CARGO_INCREMENTAL=0 cargo test -p openhuman openhuman::memory::store::factories::tests -- --nocapture
  • sudo -n docker run ... ghcr.io/tinyhumansai/openhuman_ci:rust-1.93.0 ... cargo llvm-cov -p openhuman --lcov --output-path /tmp/lcov-core.info
  • pre-push hook: format, lint, TypeScript compile, Tauri cargo check

Summary by CodeRabbit

  • Tests
    • Improved test synchronization and reliability by consolidating test lock management across multiple test suites. This reduces potential test flakiness when running tests in parallel and strengthens the testing infrastructure for AI and voice processing modules.

Review Change Stack

@Zavianx Zavianx requested a review from a team May 13, 2026 15:08
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 13, 2026

📝 Walkthrough

Walkthrough

This PR introduces a centralized test-only helper function local_ai_test_guard() that serializes access to process-global local-AI state across the test suite. Multiple test files are refactored to use this single guard instead of direct mutex locking, reducing duplication and preventing test flakiness from concurrent environment variable and health-status mutations.

Changes

Test synchronization via centralized local-AI guard

Layer / File(s) Summary
Central guard helper definition
src/openhuman/local_ai/mod.rs
Introduces pub(crate) fn local_ai_test_guard() that locks LOCAL_AI_TEST_MUTEX with poison recovery via into_inner(), providing a unified guard for all test synchronization.
Local-AI module test synchronization
src/openhuman/local_ai/install.rs, src/openhuman/local_ai/ollama_api.rs
Updates helper functions in install.rs and ollama_api.rs test modules to delegate to the new centralized guard, consolidating environment and Ollama URL synchronization.
Service and inference test synchronization
src/openhuman/local_ai/service/ollama_admin_tests.rs, src/openhuman/local_ai/service/public_infer_tests.rs, src/openhuman/local_ai/service/vision_embed.rs
Refactors 12 async test cases in ollama_admin_tests.rs and 3 test entry points in public_infer_tests.rs and vision_embed.rs to acquire the guard at test start, serializing health checks, model queries, and embedding API calls.
Cross-module test synchronization refactor
src/openhuman/memory/store/factories.rs, src/openhuman/voice/postprocess.rs
Updates EnvGuard RAII logic and test entry points to use the centralized guard instead of module-local locks, including race condition prevention for the Ollama health-status latch during parallel execution; 7 transcription cleanup tests and 2 embedding tests refactored.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • tinyhumansai/openhuman#1443: Both PRs focus on fixing test flakiness by serializing access to process-global singletons/mutable state via test-only mutex guards in their respective modules (local-AI env/ollama guards in main PR vs autocomplete global engine guards in #1443).
  • tinyhumansai/openhuman#911: Both PRs update the voice postprocess test synchronization to serialize shared local AI/Ollama state using LOCAL_AI_TEST_MUTEX (direct lock vs the new local_ai_test_guard() wrapper).

Suggested reviewers

  • senamakel

Poem

🐰 A scattered lock, once many and wide,
Now gathers in one where tests safely hide.
Guard local_ai threads that race through the night—
One mutex, one truth, one test gleaming bright! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'test(local-ai): serialize Ollama env mutations' accurately summarizes the main change: a refactoring to centralize test environment variable locking around Ollama-related mutations through a new test guard wrapper.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
src/openhuman/local_ai/mod.rs (1)

7-12: ⚡ Quick win

Move test guard implementation out of mod.rs to keep it export-focused.

Line 8 adds operational logic in a domain mod.rs. Prefer re-exporting from a sibling test-support module and keeping this file as declarations/re-exports only.

Proposed refactor (module stays export-focused)
 #[cfg(test)]
-pub(crate) fn local_ai_test_guard() -> std::sync::MutexGuard<'static, ()> {
-    LOCAL_AI_TEST_MUTEX
-        .lock()
-        .unwrap_or_else(|p| p.into_inner())
-}
+mod test_support;
+#[cfg(test)]
+pub(crate) use test_support::local_ai_test_guard;

As per coding guidelines: src/openhuman/*/mod.rs: Keep domain mod.rs files light and export-focused; place operational code in sibling files (e.g., ops.rs, store.rs, schedule.rs, types.rs) and re-export the public API from mod.rs.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/openhuman/local_ai/mod.rs` around lines 7 - 12, The function
local_ai_test_guard and its use of LOCAL_AI_TEST_MUTEX implement operational
test logic inside mod.rs; move that implementation into a sibling module (e.g.,
test_support.rs or ops.rs) where you define pub(crate) fn local_ai_test_guard()
-> std::sync::MutexGuard<'static, ()> and the LOCAL_AI_TEST_MUTEX there, then in
mod.rs replace the implementation with a simple re-export (pub(crate) use
test_support::local_ai_test_guard;) so mod.rs remains export-focused and the
operational code lives in the sibling module.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@src/openhuman/local_ai/mod.rs`:
- Around line 7-12: The function local_ai_test_guard and its use of
LOCAL_AI_TEST_MUTEX implement operational test logic inside mod.rs; move that
implementation into a sibling module (e.g., test_support.rs or ops.rs) where you
define pub(crate) fn local_ai_test_guard() -> std::sync::MutexGuard<'static, ()>
and the LOCAL_AI_TEST_MUTEX there, then in mod.rs replace the implementation
with a simple re-export (pub(crate) use test_support::local_ai_test_guard;) so
mod.rs remains export-focused and the operational code lives in the sibling
module.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 434a8e87-7543-47f8-8f2e-777c522a7edd

📥 Commits

Reviewing files that changed from the base of the PR and between df189c8 and 8d51c8d.

📒 Files selected for processing (8)
  • src/openhuman/local_ai/install.rs
  • src/openhuman/local_ai/mod.rs
  • src/openhuman/local_ai/ollama_api.rs
  • src/openhuman/local_ai/service/ollama_admin_tests.rs
  • src/openhuman/local_ai/service/public_infer_tests.rs
  • src/openhuman/local_ai/service/vision_embed.rs
  • src/openhuman/memory/store/factories.rs
  • src/openhuman/voice/postprocess.rs

@senamakel senamakel merged commit b7032e6 into tinyhumansai:main May 13, 2026
18 of 21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants