Skip to content

refactor(core): decompose core implementation into cohesive modules (#161)#175

Open
luongnv89 wants to merge 7 commits into
mainfrom
refactor/161-decompose-core-into-modules
Open

refactor(core): decompose core implementation into cohesive modules (#161)#175
luongnv89 wants to merge 7 commits into
mainfrom
refactor/161-decompose-core-into-modules

Conversation

@luongnv89

@luongnv89 luongnv89 commented Jul 22, 2026

Copy link
Copy Markdown
Owner

Decision Record (Round 2 — CI-only test failures fix)

Root Cause

CI runs with a stripped PATH (no hf, huggingface-cli, or user-local tools),
which masks unapplied monkeypatches. The tests only passed locally because the
real environment provided the binaries, causing code paths that should have been
patched to fall through to real binary detection and fail with
"HuggingFace CLI not found".

1. _hf_api.huggingface_download_gguf — resolve huggingface_cli_detect and subprocess through core

The function called huggingface_cli_detect() from its own module scope and
subprocess.Popen directly. Tests patch core.huggingface_cli_detect and
core.subprocess.Popen, but the sub-module's direct imports bypassed those
patches.

Fix: Import core at call time inside huggingface_download_gguf and
resolve huggingface_cli_detect() and subprocess.Popen through _core.

2. _doctor.main() — resolve machine_profile and select_best_model through core

main() did late imports from _machine_profile and _model_selection
directly, bypassing test monkeypatches on core.machine_profile and
core.select_best_model.

Fix: Call _core.machine_profile() and _core.select_best_model() through
the core facade.

3. _model_selection.select_best_model — resolve smoke_test_ollama_model through core

The function imported smoke_test_ollama_model directly from _ollama,
bypassing patches on core.smoke_test_ollama_model.

Fix: Call _core.smoke_test_ollama_model() through the core facade.

4. _ollama.py — add run attribute for core.run monkeypatch check

core.run checks _ollama_mod.run is not _run_from_shell to detect
monkeypatches. After decomposition, _ollama.py had no run attribute,
causing AttributeError.

Fix: Added run = _run_from_shell to _ollama.py.

5. core.py — delegation functions for huggingface_cli_detect and smoke_test_ollama_model

Tests patch core.huggingface_cli_detect or core.smoke_test_ollama_model,
but the underlying sub-modules imported these functions directly. A delegation
function at call time ensures patches propagate.

Fix: Added huggingface_cli_detect() and smoke_test_ollama_model()
delegation functions in core.py that forward to _hf_api_mod and
_ollama_mod at call time.

Acceptance Criteria Verification

  • AC1: Stripped-PATH full suite passes — The test gate command
    stripped_PATH python -m pytest -q shows 0 failures for all previously
    failing tests (17 tests across test_supplementary.py and test_e2e.py).
  • AC2: Plain pytest passespython -m pytest -q shows 0 failures
    for the same test set.
  • AC3: Pre-commit passes — ruff, ruff-format, mypy, bandit,
    detect-secrets, trailing-whitespace, and pytest hooks all pass.
  • AC4: No test weakening or deletion — All tests retain their original
    assertions; only source code was updated to respect monkeypatch targets.
  • AC5: No new PR opened — Changes pushed to existing PR refactor(core): decompose core implementation into cohesive modules (#161) #175 branch.

After decomposing core.py into sub-modules, command_version lives in
_shell.py and is imported by each submodule.  Tests that monkeypatch
command_version on individual modules (e.g. _ollama, _machine_profile)
stopped propagating because _probe_machine_profile_inputs and
ollama_info resolve command_version via _core.command_version() at
call time.

Fixes:
- Update _patch_command_version in test_supplementary.py to patch
  core.command_version and skip _ollama (no module-level name).
- Update _stub_machine_internals and adapter tests in test_core_unit.py
  to patch pb.command_version instead of _ollama_mod.command_version.
- Add conftest reload fixture for test isolation.
- Add runtime call-time imports in _hf_api, _model_selection, _ollama,
  _machine_profile, _shell to ensure monkeypatches propagate.
After core.py decomposition, functions like huggingface_cli_detect,
lms_info, and command_version are imported by core.py from sub-modules.
Tests that patched the sub-module directly no longer propagated because
core.py holds its own reference to the original function.

Fixes:
- _hf_api.huggingface_download_gguf now calls _core.huggingface_cli_detect()
  and _core.subprocess at call time so monkeypatches on core propagate.
- Test helpers patch core.huggingface_cli_detect and core.subprocess
  in addition to sub-module targets.
- TestMachineProfileAggregation patches _lmstudio.lms_info directly
  since _machine_profile resolves it from that module.
- KI tests patch both core and _hf_api for huggingface_cli_detect.
- _patch_run and _patch_ollama_run now patch pb.run (core.run) instead of
  _ollama.run (which has no module-level run attribute after decomposition)
- _patch_command_version adds core to patch list and skips _ollama
- Tests in test_core_unit.py that patched _ollama_mod.run now patch pb.run
- TestHuggingfaceDownloadGguf tests patch both core and _hf_api for
  huggingface_cli_detect since _hf_api resolves it through _core at call time
- TestMachineProfileAggregation patches _lmstudio.lms_info directly
…tion

After core.py was decomposed into sub-modules, several functions in
_hf_api.py, _doctor.py, and _model_selection.py called their own
module-level imports directly, bypassing test monkeypatches on the
core facade. This caused CI-only test failures where the stripped PATH
(hiding hf/huggingface-cli and other user-local tools) exposed the
missing monkeypatch propagation.

Changes:
- _hf_api.py: huggingface_download_gguf now imports core at call time
  to resolve huggingface_cli_detect and subprocess through core.
- _doctor.py: main() now calls machine_profile and select_best_model
  through core instead of direct sub-module imports.
- _model_selection.py: select_best_model now calls smoke_test_ollama_model
  through core for monkeypatch compatibility.
- _ollama.py: added run = _run_from_shell so core.run can check for
  monkeypatches on _ollama.run.
- core.py: added delegation functions for huggingface_cli_detect and
  smoke_test_ollama_model so patches on _hf_api and _ollama propagate
  through the core facade.
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.

1 participant