Skip to content

chore: Move Unity process focus helpers into common package#1484

Merged
hatayama merged 3 commits into
v3-betafrom
refactor/common-unityprocess
Jul 4, 2026
Merged

chore: Move Unity process focus helpers into common package#1484
hatayama merged 3 commits into
v3-betafrom
refactor/common-unityprocess

Conversation

@hatayama

@hatayama hatayama commented Jul 4, 2026

Copy link
Copy Markdown
Owner

Summary

  • Move Unity process discovery and focus helpers out of clicore into common/unityprocess while preserving existing focus-window behavior.
  • Store the Windows focus PowerShell bodies as embedded .ps1 templates instead of assembling the long scripts in Go string slices.

User Impact

  • No CLI behavior or output schema is intended to change.
  • The focus helpers are isolated for the next clicore decomposition steps, making future focus behavior changes easier to review in smaller PRs.

Changes

  • Added common/unityprocess for Unity process discovery, macOS focus, Windows focus, and unsupported-platform focus handling.
  • Kept thin clicore compatibility wrappers for existing callers while moving dispatcher/project-runner deps to the new package.
  • Added shared release input coverage for embedded .ps1 assets and refreshed dispatcher/project-runner shared input stamps.

Verification

  • cd cli/common && go test ./unityprocess ./clicore
  • cd cli/dispatcher && go test ./internal/dispatcher
  • cd cli/project-runner && go test ./internal/projectrunner
  • cd cli/release-automation && go test ./internal/automation -run 'TestReleaseTriggerGuard(CommonPackageWhitelistsMatchGoDependencies|RequiresBothTriggersForSharedEmbeddedScripts)' -count=1
  • scripts/check-go-cli.sh
  • cd cli/release-automation && go run ./cmd/check-release-triggers --base origin/v3-beta --head HEAD

Review in cubic

@coderabbitai

coderabbitai Bot commented Jul 4, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Warning

Review limit reached

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

Next review available in: 17 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: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: b4e338c0-d967-40ed-bfa0-3ca6a3acca1d

📥 Commits

Reviewing files that changed from the base of the PR and between 3c0c9c5 and f2303db.

📒 Files selected for processing (7)
  • cli/common/unityprocess/command_error.go
  • cli/common/unityprocess/command_error_test.go
  • cli/common/unityprocess/focus_windows.go
  • cli/common/unityprocess/process.go
  • cli/common/unityprocess/process_test.go
  • cli/dispatcher/shared-inputs-stamp.json
  • cli/project-runner/shared-inputs-stamp.json
📝 Walkthrough

Walkthrough

Unity process discovery and OS-specific window focus/restore logic are extracted from cli/common/clicore/focus.go into a new cli/common/unityprocess package with platform-specific implementations (darwin, windows via PowerShell scripts, unsupported fallback). Consumer packages (clicore, dispatcher, project-runner) and release-automation tooling are updated to reference the new package.

Changes

Unity process/focus extraction and consumer wiring

Layer / File(s) Summary
Shared UnityProcess/RestoreFocusFunc contracts and discovery logic
cli/common/unityprocess/process.go, cli/common/unityprocess/process_test.go
New UnityProcess/RestoreFocusFunc types and FindRunningUnityProcess, with cross-platform process listing/parsing/filtering and path normalization, plus unit tests.
Platform-specific focus and restore implementations
cli/common/unityprocess/focus_darwin.go, focus_windows.go, focus_windows_scripts.go, focus_unsupported.go, process_test.go
macOS (AppleScript/osascript), Windows (embedded PowerShell scripts), and unsupported-platform implementations of FocusUnityProcess/FocusUnityProcessWithRestore, with script builders/parsers and tests.
clicore delegation to unityprocess
cli/common/clicore/focus.go, focus_test.go, tool_readiness.go
focus.go replaces in-file OS logic with type aliases and thin wrapper functions delegating to unityprocess; tool_readiness.go rewires accordingly; focus_test.go now tests only runFocusWindow Vibe logging.
Dispatcher and project-runner consumer rewiring
cli/dispatcher/internal/dispatcher/launch*.go, cli/project-runner/internal/projectrunner/connection_retry*.go, */shared-inputs-stamp.json
Function/dependency signatures switched from clicore.UnityProcess/RestoreFocusFunc to unityprocess equivalents; stamp hashes updated.
Release trigger guard and stamp script updates
cli/release-automation/internal/automation/release_trigger_guard.go, release_trigger_guard_test.go, scripts/stamp-release-inputs.sh
cli/common/unityprocess/ added to shared package roots, .ps1 files now count as release inputs, with a new guard test and stamp script path update.

Estimated code review effort: 4 (Complex) | ~60 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Caller
  participant FocusUnityProcessWithRestore
  participant OSHelper as osascript/PowerShell
  Caller->>FocusUnityProcessWithRestore: request focus(pid)
  FocusUnityProcessWithRestore->>OSHelper: read current frontmost/foreground handle
  OSHelper-->>FocusUnityProcessWithRestore: previous handle
  FocusUnityProcessWithRestore->>OSHelper: set frontmost/foreground(pid)
  FocusUnityProcessWithRestore-->>Caller: RestoreFocusFunc closure
  Caller->>FocusUnityProcessWithRestore: invoke restore()
  FocusUnityProcessWithRestore->>OSHelper: restore previous handle
Loading

Possibly related PRs

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly matches the main change: moving Unity process focus helpers into a common package.
Description check ✅ Passed The description is directly related to the changeset and accurately summarizes the refactor and supporting updates.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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 refactor/common-unityprocess

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.

Actionable comments posted: 1

🧹 Nitpick comments (1)
cli/common/unityprocess/process_test.go (1)

125-141: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Whitespace-dependent negative assertion is brittle.

The negative check matches an exact 2-space-indented literal ("\n $shown = ...") to prove the call isn't unconditional, while the positive check above expects 4-space indentation. If the .ps1 template's formatting/indentation ever changes without changing behavior, this negative assertion could silently stop testing anything meaningful (false negative) rather than failing loudly.

♻️ Suggested more robust check
-	if strings.Contains(script, "\n  $shown = [Win32Interop]::ShowWindowAsync($handle, 9)") {
-		t.Fatalf("restore script should not unconditionally restore the previous window: %s", script)
-	}
+	if strings.Count(script, "ShowWindowAsync($handle, 9)") != 1 {
+		t.Fatalf("expected exactly one conditional ShowWindowAsync call: %s", script)
+	}
🤖 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 `@cli/common/unityprocess/process_test.go` around lines 125 - 141, The negative
assertion in
TestBuildRestoreWindowsForegroundWindowScriptRestoresOnlyMinimizedWindow is
brittle because it depends on exact whitespace in the generated script. Update
the test to verify the behavior using a whitespace-agnostic check against
buildRestoreWindowsForegroundWindowScript, for example by asserting the
ShowWindowAsync call is guarded inside the IsIconic conditional rather than
matching a literal two-space-indented line.
🤖 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.

Inline comments:
In `@cli/common/unityprocess/focus_windows.go`:
- Around line 10-33: The Windows focus helpers are dropping PowerShell stderr,
so errors from the scripts never reach callers. Update FocusUnityProcess,
FocusUnityProcessWithRestore, and restoreWindowsForegroundWindow to capture
stderr from exec.CommandContext (or unwrap *exec.ExitError) and append it to the
returned error before bubbling it up. Keep the fix localized around
buildFocusUnityProcessWindowsScript,
buildFocusUnityProcessWindowsWithRestoreScript, and
buildRestoreWindowsForegroundWindowScript so downstream err.Error() includes the
PowerShell throw text.

---

Nitpick comments:
In `@cli/common/unityprocess/process_test.go`:
- Around line 125-141: The negative assertion in
TestBuildRestoreWindowsForegroundWindowScriptRestoresOnlyMinimizedWindow is
brittle because it depends on exact whitespace in the generated script. Update
the test to verify the behavior using a whitespace-agnostic check against
buildRestoreWindowsForegroundWindowScript, for example by asserting the
ShowWindowAsync call is guarded inside the IsIconic conditional rather than
matching a literal two-space-indented line.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 73b81a9c-ee06-4225-823c-086933f50d94

📥 Commits

Reviewing files that changed from the base of the PR and between ab93bbd and 3c0c9c5.

⛔ Files ignored due to path filters (3)
  • cli/common/unityprocess/focus_unity_process.ps1 is excluded by none and included by none
  • cli/common/unityprocess/focus_unity_process_with_restore.ps1 is excluded by none and included by none
  • cli/common/unityprocess/restore_windows_foreground_window.ps1 is excluded by none and included by none
📒 Files selected for processing (18)
  • cli/common/clicore/focus.go
  • cli/common/clicore/focus_test.go
  • cli/common/clicore/tool_readiness.go
  • cli/common/unityprocess/focus_darwin.go
  • cli/common/unityprocess/focus_unsupported.go
  • cli/common/unityprocess/focus_windows.go
  • cli/common/unityprocess/focus_windows_scripts.go
  • cli/common/unityprocess/process.go
  • cli/common/unityprocess/process_test.go
  • cli/dispatcher/internal/dispatcher/launch.go
  • cli/dispatcher/internal/dispatcher/launch_deps.go
  • cli/dispatcher/shared-inputs-stamp.json
  • cli/project-runner/internal/projectrunner/connection_retry.go
  • cli/project-runner/internal/projectrunner/connection_retry_flow.go
  • cli/project-runner/shared-inputs-stamp.json
  • cli/release-automation/internal/automation/release_trigger_guard.go
  • cli/release-automation/internal/automation/release_trigger_guard_test.go
  • scripts/stamp-release-inputs.sh
💤 Files with no reviewable changes (1)
  • cli/common/clicore/focus_test.go

Comment thread cli/common/unityprocess/focus_windows.go

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

2 issues found across 21 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread cli/common/unityprocess/process.go Outdated
Comment thread cli/common/unityprocess/focus_windows.go Outdated
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