-
Notifications
You must be signed in to change notification settings - Fork 0
Auto-select sole Unity instance via PluginHub/stdio and add tests #116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Auto-select sole Unity instance via PluginHub/stdio and add tests #116
Conversation
…tion handling ### Motivation - Avoid probing `stdio` when the server transport is `http` and prefer PluginHub session resolution instead. - Prevent transient errors from clearing or flipping the active instance by making auto-selection defensive. - Reduce noisy exception handling by catching expected errors explicitly and re-raising system-level interrupts. - Improve observability by logging successful auto-selection at `info` and probe failures at `debug`. ### Description - Added `async def _maybe_autoselect_instance(self, ctx)` which calls `transport.unity_transport._current_transport()` and tries PluginHub `get_sessions()` first, falling back to stdio discovery only when `transport != "http"`. - Limited stdio discovery to `get_unity_connection_pool().discover_all_instances(force_refresh=True)` and only auto-select when exactly one instance is found, and set the session via `set_active_instance`. - Replaced broad `except Exception:` catches with targeted exception tuples and a final handler that re-raises `SystemExit`/`KeyboardInterrupt`, and added clearer `info`/`debug` logging messages. - Updated `_inject_unity_instance` to call `_maybe_autoselect_instance` when there is no `active_instance` and to validate PluginHub session resolution defensively. ### Testing - No automated tests were executed as part of this rollout.
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the 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. Comment |
Greptile SummaryThis PR implements automatic Unity instance selection when no active instance is set, improving developer experience by removing the need to manually select instances in single-instance scenarios. Key Changes:
Minor Issues:
Confidence Score: 4/5
Important Files Changed
Sequence DiagramsequenceDiagram
participant Client
participant Middleware as UnityInstanceMiddleware
participant PluginHub
participant StdioPool as UnityConnectionPool
participant Context
Client->>Middleware: on_call_tool() / on_read_resource()
Middleware->>Middleware: get_active_instance(ctx)
alt No active instance set
Middleware->>Middleware: _maybe_autoselect_instance(ctx)
alt PluginHub.is_configured()
Middleware->>PluginHub: get_sessions()
PluginHub-->>Middleware: sessions_data
alt Exactly 1 session found
Middleware->>Middleware: set_active_instance(ctx, chosen)
Middleware-->>Middleware: return chosen
else 0 or 2+ sessions
Note over Middleware: Fall through to stdio check
end
end
alt transport != "http"
Middleware->>StdioPool: discover_all_instances(force_refresh=True)
StdioPool-->>Middleware: instances
alt Exactly 1 instance found
Middleware->>Middleware: set_active_instance(ctx, chosen)
Middleware-->>Middleware: return chosen
else 0 or 2+ instances
Middleware-->>Middleware: return None
end
end
end
alt active_instance exists
alt PluginHub.is_configured()
Middleware->>PluginHub: _resolve_session_id(active_instance)
PluginHub-->>Middleware: session_id
Middleware->>Context: set_state("unity_session_id", session_id)
end
Middleware->>Context: set_state("unity_instance", active_instance)
end
Middleware->>Client: call_next(context)
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
3 files reviewed, 1 comment
| asyncio.run(middleware._inject_unity_instance(middleware_context)) | ||
|
|
||
| assert ctx.get_state("unity_instance") == "Ramble@deadbeef" | ||
| assert call_count["sessions"] == 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: Missing stub for PluginHub._resolve_session_id. When _inject_unity_instance is called with PluginHub configured, it will attempt to call _resolve_session_id(active_instance) on line 187 of the middleware. While the exception handling prevents the test from failing, the test should properly stub this method to avoid relying on exception handling and to ensure the happy path is tested.
| asyncio.run(middleware._inject_unity_instance(middleware_context)) | |
| assert ctx.get_state("unity_instance") == "Ramble@deadbeef" | |
| assert call_count["sessions"] == 1 | |
| asyncio.run(middleware._inject_unity_instance(middleware_context)) | |
| assert ctx.get_state("unity_instance") == "Ramble@deadbeef" | |
| # Note: _resolve_session_id is not stubbed, so unity_session_id won't be set | |
| # Consider adding: monkeypatch.setattr(plugin_hub.PluginHub, "_resolve_session_id", lambda inst: "session-1") | |
| assert call_count["sessions"] == 1 |
Prompt To Fix With AI
This is a comment left during a code review.
Path: Server/tests/integration/test_instance_autoselect.py
Line: 61:64
Comment:
**style:** Missing stub for `PluginHub._resolve_session_id`. When `_inject_unity_instance` is called with PluginHub configured, it will attempt to call `_resolve_session_id(active_instance)` on line 187 of the middleware. While the exception handling prevents the test from failing, the test should properly stub this method to avoid relying on exception handling and to ensure the happy path is tested.
```suggestion
asyncio.run(middleware._inject_unity_instance(middleware_context))
assert ctx.get_state("unity_instance") == "Ramble@deadbeef"
# Note: _resolve_session_id is not stubbed, so unity_session_id won't be set
# Consider adding: monkeypatch.setattr(plugin_hub.PluginHub, "_resolve_session_id", lambda inst: "session-1")
assert call_count["sessions"] == 1
```
How can I resolve this? If you propose a fix, please make it concise.
Motivation
stdiounnecessarily.Description
async def _maybe_autoselect_instance(self, ctx)which prefersPluginHub.get_sessions()for HTTP transport and falls back to stdio discovery only when transport !="http".get_unity_connection_pool().discover_all_instances(force_refresh=True)and only auto-select when exactly one instance is found, storing it viaset_active_instance.SystemExit/KeyboardInterrupt, and add clearerinfo/debuglogs for probe results.Server/tests/integration/test_instance_autoselect.pyand updatedServer/tests/integration/conftest.pyto includesrconsys.pathand stub minimalstarlettemodules to allow isolated test runs.Testing
pytest tests/integration/test_instance_autoselect.py -vwhich executed the new tests and reported2 passed.asyncio.run(...)to execute middleware async paths and stubstransportsubmodules to isolate the unit under test.Codex Task