Skip to content

[cli] fix: reject malformed rpc error messages - #240

Merged
Wangmerlyn merged 2 commits into
mainfrom
codex/cli-rpc-error-message
Jul 1, 2026
Merged

[cli] fix: reject malformed rpc error messages#240
Wangmerlyn merged 2 commits into
mainfrom
codex/cli-rpc-error-message

Conversation

@Wangmerlyn

@Wangmerlyn Wangmerlyn commented Jul 1, 2026

Copy link
Copy Markdown
Owner

Summary

  • reject JSON-RPC error envelopes with missing or non-string error.message as malformed service responses
  • prevent malformed start_keep error envelopes from triggering startup-unavailable daemon rollback
  • update AGENTS.md and CLI docs to keep the protocol/rollback contract explicit

Verification

  • PYTHONPATH=src pytest tests/test_cli_service_commands.py -q
  • PYTHONPATH=src pytest tests -q
  • mkdocs build --strict
  • pre-commit run --all-files --show-diff-on-failure
  • git diff --check

Notes

  • README keeps the Zenodo DOI badge in the top badge block.

Summary by CodeRabbit

  • Bug Fixes

    • Tightened CLI error handling so malformed JSON-RPC responses are treated as errors instead of being mistaken for successful results.
    • Ensured startup cleanup is skipped when the service error response is malformed, preventing unintended stop attempts.
  • Documentation

    • Clarified CLI behavior for start, status, stop, and list-gpus when responses or records are malformed.
    • Added a new planning document outlining the expected error-validation and testing approach.

@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

This pull request introduces validation for JSON-RPC error envelopes in the CLI, ensuring that error.message is a valid string. If the message is missing or malformed, a ServiceResponseError is raised, which prevents the CLI from triggering a daemon rollback. The changes include updates to the CLI implementation in src/keep_gpu/cli.py, comprehensive unit tests in tests/test_cli_service_commands.py, and updated documentation across AGENTS.md, docs/guides/cli.md, and docs/reference/cli.md. A new validation plan has also been added. There are no review comments to evaluate, and the implementation looks solid.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

@coderabbitai

coderabbitai Bot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Warning

Review limit reached

@Wangmerlyn, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 52 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: b5681ac2-18f0-47e4-914c-2a4488ecb4e9

📥 Commits

Reviewing files that changed from the base of the PR and between 102c73a and 85b3f5d.

📒 Files selected for processing (1)
  • tests/test_cli_service_commands.py
📝 Walkthrough

Walkthrough

This PR tightens JSON-RPC error handling in the CLI's _rpc_call function, requiring error.message to be a present string before raising ServiceRPCError; otherwise a ServiceResponseError is raised. Documentation across AGENTS.md and CLI reference/guide docs is updated to reflect this, and a new plan document is added. Tests validate the new behavior.

Changes

Error message validation

Layer / File(s) Summary
Implement error.message validation
src/keep_gpu/cli.py
_rpc_call now checks that error.message exists and is a string, raising ServiceResponseError otherwise instead of falling back loosely to ServiceRPCError.
Tests for validation and rollback behavior
tests/test_cli_service_commands.py
New parameterized test rejects non-string error.message in _rpc_call; new start test confirms malformed envelopes skip auto-stop rollback.
Architecture and CLI docs updates
AGENTS.md, docs/guides/cli.md, docs/reference/cli.md
Clarifies that malformed JSON-RPC envelopes (missing/non-string error.message) are treated as response errors, do not trigger rollback, and malformed records return JSON errors instead of empty success.
Implementation plan document
docs/plans/cli-rpc-error-message.md
New plan document defines goals, architecture constraints, task list, and verification checklist for the error.message validation change.

Estimated code review effort: 2 (Simple) | ~15 minutes

Possibly related PRs

  • Wangmerlyn/KeepGPU#62: Both PRs modify AGENTS.md's architecture boundaries guidance related to CLI JSON-RPC behavior.
  • Wangmerlyn/KeepGPU#167: Both PRs directly affect the keep-gpu start best-effort rollback/cleanup path triggered by RPC failures.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: rejecting malformed CLI JSON-RPC error messages.
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.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch codex/cli-rpc-error-message

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.

@coderabbitai coderabbitai 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.

🧹 Nitpick comments (1)
tests/test_cli_service_commands.py (1)

1632-1635: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Escape regex metacharacters in pytest.raises(match=...).

. in "error.message must be a string" is an unescaped regex metacharacter. Works today only because the message happens to contain a literal . at that position; use re.escape() for correctness.

🔧 Proposed fix
+import re
+
     with pytest.raises(
-        cli.ServiceResponseError, match="error.message must be a string"
+        cli.ServiceResponseError, match=re.escape("error.message must be a string")
     ):
🤖 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 `@tests/test_cli_service_commands.py` around lines 1632 - 1635, The
`pytest.raises(..., match=...)` assertion in the `cli._rpc_call` test uses a raw
regex string, so the `.` in `error.message` is treated as a metacharacter.
Update the test to escape the expected message properly, using `re.escape()` or
an equivalent escaped pattern, while keeping the `ServiceResponseError`
assertion and the existing `_rpc_call` call unchanged.

Source: Linters/SAST tools

🤖 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 `@tests/test_cli_service_commands.py`:
- Around line 1632-1635: The `pytest.raises(..., match=...)` assertion in the
`cli._rpc_call` test uses a raw regex string, so the `.` in `error.message` is
treated as a metacharacter. Update the test to escape the expected message
properly, using `re.escape()` or an equivalent escaped pattern, while keeping
the `ServiceResponseError` assertion and the existing `_rpc_call` call
unchanged.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 3812b0d9-bfb2-4752-987f-59c5632338a1

📥 Commits

Reviewing files that changed from the base of the PR and between cb1aade and 102c73a.

📒 Files selected for processing (6)
  • AGENTS.md
  • docs/guides/cli.md
  • docs/plans/cli-rpc-error-message.md
  • docs/reference/cli.md
  • src/keep_gpu/cli.py
  • tests/test_cli_service_commands.py

@Wangmerlyn

Copy link
Copy Markdown
Owner Author

Resolved CodeRabbit nit in 85b3f5d: the pytest.raises(..., match=...) assertion now uses re.escape("error.message must be a string").

Final local verification after the follow-up:

  • PYTHONPATH=src pytest tests/test_cli_service_commands.py -q -> 223 passed
  • PYTHONPATH=src pytest tests -q -> 900 passed, 11 skipped
  • mkdocs build --strict -> passed
  • pre-commit run --all-files --show-diff-on-failure -> passed
  • git diff --check -> passed

Final local subagent delta review also reported no findings.

@Wangmerlyn
Wangmerlyn merged commit 8d24a72 into main Jul 1, 2026
6 checks passed
@Wangmerlyn
Wangmerlyn deleted the codex/cli-rpc-error-message branch July 1, 2026 10:54
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