← Back to Pull requests

Refactor implementation and add unit tests. #107

Closed
unknown closed this pull request without merging on Nov 07, 2026 02:05 UTC
View on GitHub
unknown unknown commented on Nov 07, 2026 00:42 UTC
Author

Refactoring of the implementation with no change in capabilities, command line arguments, options or printed output. ros2 ai -h, status -h, query -h and exec -h are byte-identical before and after.

Code changes

| Before | After |
| :-- | :-- |
| ros2ai/api/__init__.py held add_global_arguments(), curl_get_request() and the unused ollama_get_models() | ros2ai/api/arguments.py holds add_global_arguments() and the new add_role_argument() shared by query/exec. Model listing uses the OpenAI python client (client.models.list()) instead of spawning curl, so the curl exec dependency is dropped. |
| ros2ai/api/openai.py (name shadowed the openai package), ChatCompletionClient inherited OpenAiConfig, ChatCompletionParameters was a hand written property class with a mutable default | ros2ai/api/client.py: ChatCompletionClient(config) takes the config by composition, call() returns success, list_models() added, ChatCompletionParameters is a dataclass, chat_messages() builds the system/user messages. |
| OpenAiConfig had unused set_value()/remove_key(), and is_api_key_valid() duplicated the client code | OpenAiConfig is configuration only. resolve_role_system() implements the argument > env > default role resolution that query and exec duplicated. The availability check lives in status and uses the client. |
| utils.py had the unused run_command() and unused argv/prefix parameters | removed, extract_ros2_command() composes truncate_before_substring() / remove_backticks() / ros2_single_command() for exec. |
| verbs duplicated role handling, message building and the -r/--role argument | verbs are thin: parse, resolve role, build parameters, call client, print. |

Whole code base now passes ament_flake8, ament_pep257, ament_copyright and ament_xmllint.

Behavior notes (intentional, error path only)

  • When the API call fails (bad key, endpoint down) query and exec used to print Failed to call OpenAI API: ... and then crash with an AttributeError/TypeError traceback. They now print the same message and return 1.

  • When exec gets a completion that did not finish (finish_reason != 'stop') it used to crash with a traceback, it now prints Failed chat completion with: <reason> and returns 1.

  • status -l model listing failures print Failed to list models: <error> instead of the raw curl/json error, the [FAILURE] Could not retrieved list of models. line is unchanged.
  • Tests

    New test/ directory, 43 tests, no network access required:

  • test_utils.py, test_config.py: pure helpers and env/argument precedence.

  • test_cli.py: every option of every verb and ros2 ai -h through ros2cli entry points.

  • test_verbs.py: status/query/exec end to end against a fake OpenAI client, asserting the exact printed output and the arguments passed to the API.

  • standard test_flake8.py, test_pep257.py, test_copyright.py, test_xmllint.py.
  • The behavior tests were written first and pass against the original code on rolling as well, so they lock the existing behavior.

    scripts/github_workflows.sh now installs the test dependencies and runs colcon test + colcon test-result, so the suite runs for every distribution on each PR. Verified locally by running that script in the ros:humble, ros:jazzy, ros:kilted, ros:lyrical and ros:rolling containers: 43 passed on each.

    Other files

  • pytest.ini: disables the anyio pytest plugin pulled in by openai, it requires pytest >= 7 and breaks test collection on humble (pytest 6.2.5). Also registers the lint markers.

  • CONTRIBUTING.md: standard Apache 2.0 text generated by ament_copyright, which requires it next to LICENSE.

  • package.xml: curl exec dependency removed. README/CHANGELOG untouched.
  • 🤖 Generated with Claude Code

    Diff: 489 lines (+366 / -123) across 6 files Reviewer: fujitatomoya Review duration: 83m
    All Vouch review confidence checks passed
    Residual risk 0.23 = Change Risk (0.52) × (1 − Review Confidence (0.57)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 489 lines diff across 6 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    56%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.371
    Shallow Review Depth shallow_review_depth
    0.289
    Ai Authored ai_authored
    0.050
    Real PR Conversation Timeline
    1 comments
    "Review active on #107"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 56.8% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52.4% (MEDIUM) · Higher = larger blast radius
    52.4%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 56.8% · Lower = rushed or shallow review
    56.8%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 22.7% · Safe to merge
    22.7%