Skip to content

Conversation

dsarno
Copy link
Owner

@dsarno dsarno commented Aug 18, 2025

Summary

  • Avoid invalid substring lengths when auto-upgrading ApplyTextEdits to replace_method
  • Compute replacement snippet relative to the method span with clamped ranges
  • Honor FRAMING=1 handshake when probing Unity MCP ports so framed ping/pong works

Testing

  • pytest -q

https://chatgpt.com/codex/tasks/task_e_68a296f62bcc8327addf3431c744a0da

Copy link

coderabbitai bot commented Aug 18, 2025

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

✨ Finishing Touches
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch codex/update-probe-for-framed-json-response

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@dsarno dsarno merged commit dcf6235 into protocol-framing Aug 18, 2025
2 checks passed
Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

Greptile Summary

This PR addresses two critical fixes in the Unity MCP Bridge system. The first fix resolves a dangerous bug in the ApplyTextEdits method's auto-upgrade logic within ManageScript.cs. The original implementation had a flawed substring calculation that could cause IndexOutOfRangeException or produce incorrect text replacements when converting text edits to structured replace_method operations. The new implementation properly extracts the method text first, converts edit positions to relative coordinates within that method, clamps ranges to prevent out-of-bounds access, and then performs safe replacement operations.

The second fix updates the port discovery mechanism in port_discovery.py to support the new framing protocol (FRAMING=1). The Unity MCP Bridge now sends greeting messages advertising framing capabilities, and the probe function has been enhanced to detect these greetings and handle both framed and unframed ping/pong exchanges. When FRAMING=1 is detected, messages are sent/received with 8-byte big-endian length prefixes, ensuring proper communication with modern Unity MCP servers while maintaining backward compatibility.

These changes integrate well with the existing codebase architecture - the ManageScript.cs fix preserves the auto-upgrade functionality for text editing operations while making them crash-safe, and the port_discovery.py enhancement extends the existing port probing mechanism to work with the new protocol framing system that appears to be part of a broader protocol evolution in this MCP implementation.

Important Files Changed

Click to expand file changes
Filename Score Overview
UnityMcpBridge/Editor/Tools/ManageScript.cs 4/5 Fixed critical substring calculation bug in ApplyTextEdits auto-upgrade logic
UnityMcpBridge/UnityMcpServer~/src/port_discovery.py 4/5 Added FRAMING=1 protocol support to port discovery probe mechanism

Confidence score: 4/5

  • This PR is safe to merge with minimal risk as both fixes address specific, well-defined issues
  • Score reflects solid bug fixes with clear improvements to system reliability and protocol compatibility
  • Pay close attention to UnityMcpBridge/Editor/Tools/ManageScript.cs to ensure the new substring logic handles all edge cases correctly

Sequence Diagram

sequenceDiagram
    participant User
    participant MCP_Server as MCP Server
    participant Port_Discovery as Port Discovery
    participant Unity_Bridge as Unity Bridge
    participant Unity_Editor as Unity Editor
    participant Script_Manager as Script Manager
    participant Validation as Validation System

    User->>MCP_Server: "apply_text_edits request"
    MCP_Server->>Port_Discovery: "discover_unity_port()"
    Port_Discovery->>Port_Discovery: "list_candidate_files()"
    Port_Discovery->>Unity_Bridge: "TCP connect + probe with FRAMING=1"
    Unity_Bridge-->>Port_Discovery: "greeting with FRAMING=1"
    Port_Discovery->>Unity_Bridge: "framed ping (8-byte header + payload)"
    Unity_Bridge-->>Port_Discovery: "framed pong response"
    Port_Discovery-->>MCP_Server: "validated port number"
    
    MCP_Server->>Unity_Bridge: "forward apply_text_edits request"
    Unity_Bridge->>Script_Manager: "HandleCommand(apply_text_edits)"
    Script_Manager->>Script_Manager: "validate precondition_sha256"
    Script_Manager->>Script_Manager: "convert edits to absolute indices"
    
    alt single edit targeting method
        Script_Manager->>Script_Manager: "detect method span for auto-upgrade"
        Script_Manager->>Script_Manager: "compute replacement relative to method span"
        Script_Manager->>Script_Manager: "upgrade to replace_method operation"
    else multiple/other edits
        Script_Manager->>Script_Manager: "apply text edits in reverse order"
    end
    
    Script_Manager->>Validation: "CheckBalancedDelimiters()"
    Validation-->>Script_Manager: "validation result"
    
    opt USE_ROSLYN defined
        Script_Manager->>Validation: "Roslyn syntax validation"
        Validation-->>Script_Manager: "syntax diagnostics"
    end
    
    Script_Manager->>Unity_Editor: "atomic file write with backup"
    Script_Manager->>Unity_Editor: "ScheduleScriptRefresh()"
    Unity_Editor->>Unity_Editor: "debounced asset import and compilation"
    
    Script_Manager-->>Unity_Bridge: "success response with new SHA256"
    Unity_Bridge-->>MCP_Server: "operation result"
    MCP_Server-->>User: "edits applied successfully"
Loading

2 files reviewed, no comments

Edit Code Review Bot Settings | Greptile

@dsarno dsarno deleted the codex/update-probe-for-framed-json-response branch August 19, 2025 09:57
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