Skip to content

Add shared allow_stubs() helper and use it to gate stub fallbacks#22

Draft
NguyenCuong1989 wants to merge 1 commit into
mainfrom
codex/refactor-to-use-shared-config-helper
Draft

Add shared allow_stubs() helper and use it to gate stub fallbacks#22
NguyenCuong1989 wants to merge 1 commit into
mainfrom
codex/refactor-to-use-shared-config-helper

Conversation

@NguyenCuong1989
Copy link
Copy Markdown
Collaborator

@NguyenCuong1989 NguyenCuong1989 commented May 16, 2026

Motivation

  • Centralize the logic that controls whether fallback stub implementations are permitted.
  • Make stub gating driven by a single environment variable HYPERAI_ALLOW_STUBS to keep behavior consistent across modules.
  • Replace duplicated local stub-checking logic with a single helper to simplify maintenance.

Description

  • Add src/hyperai/config.py with a single allow_stubs() helper that reads HYPERAI_ALLOW_STUBS and treats {"1","true","t","yes","y","on"} as truthy.
  • Update src/hyperai/core/haios_runtime.py to from hyperai.config import allow_stubs as _allow_stubs and gate the stub HAIOSRuntime fallback using _allow_stubs(), raising ModuleNotFoundError if stubs are disabled.
  • Update src/hyperai/protocols/dr_protocol.py to from hyperai.config import allow_stubs as _allow_stubs and gate the DRProtocol stub fallback using _allow_stubs(), raising ModuleNotFoundError if stubs are disabled.

Testing

  • Compiled the modified modules with python -m compileall src/hyperai/config.py src/hyperai/core/haios_runtime.py src/hyperai/protocols/dr_protocol.py and compilation completed successfully.

Codex Task

Tóm tắt bởi Sourcery

Giới thiệu cấu hình tập trung để kiểm soát việc có cho phép sử dụng các triển khai stub dự phòng hay không và áp dụng cho runtime và tải protocol.

Tính năng mới:

  • Thêm helper dùng chung allow_stubs() được điều khiển bởi biến môi trường HYPERAI_ALLOW_STUBS để quản lý việc sử dụng các triển khai stub.

Cải tiến:

  • Đặt điều kiện cho các stub fallback của HAIOSRuntimeDRProtocol thông qua helper dùng chung allow_stubs() thay vì luôn luôn bật chúng.
  • Cải thiện xử lý lỗi bằng cách raise ModuleNotFoundError khi thiếu các triển khai bắt buộc và stub fallback bị vô hiệu hóa.
  • Chỉnh sửa docstring trong các lớp stub cho đồng nhất.
Original summary in English

Summary by Sourcery

Introduce centralized configuration for controlling whether fallback stub implementations are permitted and apply it to runtime and protocol loading.

New Features:

  • Add a shared allow_stubs() helper driven by the HYPERAI_ALLOW_STUBS environment variable to govern use of stub implementations.

Enhancements:

  • Gate HAIOSRuntime and DRProtocol stub fallbacks behind the shared allow_stubs() helper instead of always enabling them.
  • Improve error handling by raising ModuleNotFoundError when required implementations are missing and stub fallbacks are disabled.
  • Polish docstrings in stub classes for consistency.

@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented May 16, 2026

Hướng Dẫn Dành Cho Người Review

Giới thiệu một helper dùng chung allow_stubs() được điều khiển bởi biến môi trường HYPERAI_ALLOW_STUBS và sử dụng nó để kiểm soát việc các triển khai stub của HAIOSRuntimeDRProtocol có khả dụng hay không, đồng thời raise lỗi rõ ràng khi stub bị vô hiệu hóa.

Sơ đồ tuần tự để kiểm soát fallback stub thông qua allow_stubs

sequenceDiagram
    actor User
    participant haios_runtime as haios_runtime
    participant dr_protocol as dr_protocol
    participant config as config
    participant importlib as importlib

    User->>haios_runtime: import HAIOSRuntime
    activate haios_runtime
    haios_runtime->>importlib: _load_runtime_module()
    alt [runtime module found]
        importlib-->>haios_runtime: _runtime_impl
        haios_runtime-->>User: HAIOSRuntime = _runtime_impl
    else [runtime module missing]
        haios_runtime->>config: allow_stubs()
        config-->>haios_runtime: bool
        alt [allow_stubs() is true]
            haios_runtime-->>User: HAIOSRuntime stub class
        else [allow_stubs() is false]
            haios_runtime-->>User: ModuleNotFoundError
        end
    end
    deactivate haios_runtime

    User->>dr_protocol: import DRProtocol
    activate dr_protocol
    dr_protocol->>importlib: _load_framework_module()
    alt [framework module found]
        importlib-->>dr_protocol: _dr_impl
        dr_protocol-->>User: DRProtocol = _dr_impl
    else [framework module missing]
        dr_protocol->>config: allow_stubs()
        config-->>dr_protocol: bool
        alt [allow_stubs() is true]
            dr_protocol-->>User: DRProtocol stub class
        else [allow_stubs() is false]
            dr_protocol-->>User: ModuleNotFoundError
        end
    end
    deactivate dr_protocol
Loading

Thay Đổi Cấp Độ File

Thay đổi Chi tiết Tệp
Thêm helper cấu hình tập trung để kiểm soát việc có cho phép các triển khai stub hay không.
  • Tạo module hyperai.config với helper allow_stubs() đọc HYPERAI_ALLOW_STUBS từ môi trường.
  • Chuẩn hóa giá trị biến môi trường thông qua strip().lower() và coi một tập hợp cố định các chuỗi là giá trị truthy.
  • Đóng gói các giá trị truthy trong hằng số private _TRUTHY_VALUES để tái sử dụng và rõ ràng hơn.
src/hyperai/config.py
Kiểm soát fallback stub của HAIOSRuntime thông qua helper dùng chung allow_stubs() và raise khi bị vô hiệu hóa.
  • Import allow_stubs() từ hyperai.config dưới một alias private để tránh làm ô nhiễm public API.
  • Cập nhật logic chọn HAIOSRuntime để chỉ định nghĩa triển khai stub khi module runtime bị thiếu và allow_stubs() trả về True.
  • Thêm ModuleNotFoundError với thông điệp rõ ràng khi triển khai runtime bị thiếu và stub bị không cho phép.
  • Điều chỉnh nhỏ dấu câu trong docstring của lớp stub để nhất quán.
src/hyperai/core/haios_runtime.py
Kiểm soát fallback stub của DRProtocol thông qua helper dùng chung allow_stubs() và raise khi bị vô hiệu hóa.
  • Loại bỏ pattern import try/except ở top-level trước đây, thay bằng việc load module thông qua helper rõ ràng.
  • Import allow_stubs() từ hyperai.config dưới một alias private để tránh làm ô nhiễm public API.
  • Cập nhật logic chọn DRProtocol để chỉ định nghĩa triển khai stub khi triển khai protocol bị thiếu và allow_stubs() trả về True.
  • Raise ModuleNotFoundError với hướng dẫn rõ ràng khi triển khai protocol bị thiếu và stub bị không cho phép, thay thế luồng AttributeError trước đó.
  • Dọn lại docstring trong stub DRProtocol để dùng dấu câu nhất quán.
src/hyperai/protocols/dr_protocol.py

Mẹo và câu lệnh

Tương tác với Sourcery

  • Kích hoạt một lượt review mới: Comment @sourcery-ai review trên pull request.
  • Tiếp tục thảo luận: Trả lời trực tiếp vào các comment review của Sourcery.
  • Tạo GitHub issue từ một review comment: Yêu cầu Sourcery tạo một issue từ một review comment bằng cách trả lời comment đó. Bạn cũng có thể trả lời một review comment với @sourcery-ai issue để tạo issue từ comment đó.
  • Tạo tiêu đề pull request: Viết @sourcery-ai ở bất kỳ đâu trong tiêu đề pull request để tạo tiêu đề bất kỳ lúc nào. Bạn cũng có thể comment @sourcery-ai title trên pull request để (tái) tạo tiêu đề bất kỳ lúc nào.
  • Tạo tóm tắt pull request: Viết @sourcery-ai summary ở bất kỳ đâu trong phần nội dung pull request để tạo tóm tắt PR tại đúng vị trí bạn muốn. Bạn cũng có thể comment @sourcery-ai summary trên pull request để (tái) tạo tóm tắt bất kỳ lúc nào.
  • Tạo reviewer's guide: Comment @sourcery-ai guide trên pull request để (tái) tạo reviewer's guide bất kỳ lúc nào.
  • Resolve tất cả comment của Sourcery: Comment @sourcery-ai resolve trên pull request để resolve tất cả comment của Sourcery. Hữu ích nếu bạn đã xử lý hết comment và không muốn thấy chúng nữa.
  • Dismiss tất cả review của Sourcery: Comment @sourcery-ai dismiss trên pull request để dismiss tất cả review hiện có của Sourcery. Đặc biệt hữu ích nếu bạn muốn bắt đầu lại với một lượt review mới – đừng quên comment @sourcery-ai review để kích hoạt review mới!

Tùy Biến Trải Nghiệm Của Bạn

Truy cập dashboard của bạn để:

  • Bật hoặc tắt các tính năng review như tóm tắt pull request do Sourcery tạo, reviewer's guide, và các tính năng khác.
  • Thay đổi ngôn ngữ review.
  • Thêm, xóa hoặc chỉnh sửa hướng dẫn review tùy chỉnh.
  • Điều chỉnh các thiết lập review khác.

Nhận Hỗ Trợ

Original review guide in English

Reviewer's Guide

Introduces a shared allow_stubs() helper controlled by the HYPERAI_ALLOW_STUBS environment variable and uses it to gate whether stub implementations of HAIOSRuntime and DRProtocol are available, raising explicit errors when stubs are disabled.

Sequence diagram for gating stub fallbacks via allow_stubs

sequenceDiagram
    actor User
    participant haios_runtime as haios_runtime
    participant dr_protocol as dr_protocol
    participant config as config
    participant importlib as importlib

    User->>haios_runtime: import HAIOSRuntime
    activate haios_runtime
    haios_runtime->>importlib: _load_runtime_module()
    alt [runtime module found]
        importlib-->>haios_runtime: _runtime_impl
        haios_runtime-->>User: HAIOSRuntime = _runtime_impl
    else [runtime module missing]
        haios_runtime->>config: allow_stubs()
        config-->>haios_runtime: bool
        alt [allow_stubs() is true]
            haios_runtime-->>User: HAIOSRuntime stub class
        else [allow_stubs() is false]
            haios_runtime-->>User: ModuleNotFoundError
        end
    end
    deactivate haios_runtime

    User->>dr_protocol: import DRProtocol
    activate dr_protocol
    dr_protocol->>importlib: _load_framework_module()
    alt [framework module found]
        importlib-->>dr_protocol: _dr_impl
        dr_protocol-->>User: DRProtocol = _dr_impl
    else [framework module missing]
        dr_protocol->>config: allow_stubs()
        config-->>dr_protocol: bool
        alt [allow_stubs() is true]
            dr_protocol-->>User: DRProtocol stub class
        else [allow_stubs() is false]
            dr_protocol-->>User: ModuleNotFoundError
        end
    end
    deactivate dr_protocol
Loading

File-Level Changes

Change Details Files
Add centralized configuration helper to control whether stub implementations are allowed.
  • Create hyperai.config module with allow_stubs() helper that reads HYPERAI_ALLOW_STUBS from the environment.
  • Normalize the environment variable value via strip().lower() and treat a fixed set of strings as truthy values.
  • Encapsulate truthy values in a private _TRUTHY_VALUES constant for reuse and clarity.
src/hyperai/config.py
Gate HAIOSRuntime stub fallback behind the shared allow_stubs() helper and raise when disabled.
  • Import allow_stubs() from hyperai.config as a private alias to avoid polluting the public API.
  • Update the HAIOSRuntime selection logic to only define the stub implementation when the runtime module is missing and allow_stubs() returns True.
  • Add a ModuleNotFoundError with a clear message when the runtime implementation is missing and stubs are disallowed.
  • Minorly adjust the stub class docstring punctuation to be consistent.
src/hyperai/core/haios_runtime.py
Gate DRProtocol stub fallback behind the shared allow_stubs() helper and raise when disabled.
  • Remove the previous top-level try/except import pattern in favor of an explicit helper-based module load.
  • Import allow_stubs() from hyperai.config as a private alias to avoid polluting the public API.
  • Update the DRProtocol selection logic to only define the stub implementation when the protocol implementation is missing and allow_stubs() returns True.
  • Raise ModuleNotFoundError with clear guidance when the protocol implementation is missing and stubs are disallowed, replacing the prior AttributeError path.
  • Tidy docstrings in the stub DRProtocol to use consistent punctuation.
src/hyperai/protocols/dr_protocol.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant