Skip to content

fix(windows): guard grok-hook.cmd GROK_HOME substring when empty - #10049

Closed
innocarpe wants to merge 2 commits into
stablyai:mainfrom
innocarpe:fix/windows-grok-hook-empty-grok-home
Closed

innocarpe wants to merge 2 commits into
stablyai:mainfrom
innocarpe:fix/windows-grok-hook-empty-grok-home

Conversation

@innocarpe

Copy link
Copy Markdown
Contributor

Description

User-facing (Windows): Grok SessionStart hooks no longer fail with a grok-hook.cmd syntax error when GROK_HOME is empty/unset (the common default).

Technical: The managed Windows hook always ran:

set "ORCA_GROK_HOME=%GROK_HOME%"
if not "%GROK_HOME:~4096,1%"=="" set "ORCA_GROK_HOME="
if "%ORCA_GROK_HOME:~-1%"=="\" set "ORCA_GROK_HOME=%ORCA_GROK_HOME%."

cmd.exe substring expansion (%VAR:~n,m%) on an undefined/empty variable is unsafe and can abort the script during SessionStart. POSIX already guarded with [ -n "${GROK_HOME:-}" ].

Fix

set "ORCA_GROK_HOME="
if defined GROK_HOME (
  set "ORCA_GROK_HOME=%GROK_HOME%"
  if not "%ORCA_GROK_HOME:~4096,1%"=="" set "ORCA_GROK_HOME="
  if defined ORCA_GROK_HOME if "%ORCA_GROK_HOME:~-1%"=="\" set "ORCA_GROK_HOME=%ORCA_GROK_HOME%."
)

Length + trailing-backslash protection still apply when GROK_HOME is set; empty path simply leaves ORCA_GROK_HOME blank for curl.

Evidence

pnpm exec vitest run --config config/vitest.config.ts src/main/grok/hook-service.test.ts

→ 4 passed / 1 skipped (skip = existing Windows-only space-in-path test on non-Windows hosts). New cross-platform unit test asserts the generated script never does bare top-level %GROK_HOME:~…% / %ORCA_GROK_HOME:~-1% without if defined.

ELI5

On Windows, the Grok helper script looked at the end of a folder path even when no folder was set — like measuring the last letter of a blank sticky note. Windows choked. Now it only measures the path when one actually exists.

User-regression-tradeoffs

Change Risk Mitigation
Substring checks nested under if defined Low — same behavior when GROK_HOME is set Unit test locks script shape
Empty GROK_HOME → empty grokHome form field Same intended default as POSIX Unchanged curl URL/auth

Checklist notes

  • Windows: primary fix
  • macOS/Linux: script generation path unchanged (POSIX already guarded)
  • SSH/remote: remote installers that embed this script pick up the same body
  • Tests: pure buildWindowsGrokHookScript() export for host-independent coverage

AI review summary

  • Minimal cmd ordering fix + export for testability
  • Mirrors existing POSIX empty-env guard semantics

Fixes #9941

@coderabbitai

coderabbitai Bot commented Jul 23, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Warning

Review limit reached

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

Next review available in: 14 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: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 8353be00-3ef1-45b4-a63c-f25c9438b4ef

📥 Commits

Reviewing files that changed from the base of the PR and between 02a2a4daba53bc30b8d3e3363890f61e561ff464 and 2da6a9c1a483ef4315016096ac5ebb2d985232ae.

📒 Files selected for processing (2)
  • src/main/grok/hook-service.test.ts
  • src/main/grok/hook-service.ts
📝 Walkthrough

Walkthrough

Windows Grok hook script construction was extracted into the exported buildWindowsGrokHookScript() helper. The generated batch script now initializes ORCA_GROK_HOME and performs GROK_HOME expansion and substring operations only when GROK_HOME is defined. Windows managed-script generation delegates to the helper, and tests verify the guarded output and related script assertions.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description is missing several required template sections, including Summary, Screenshots, Testing, Security Audit, and Notes. Add the missing template sections and fill in testing, screenshot/no-visual-change, security audit, and notes details.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the Windows GROK_HOME guard fix.
Linked Issues check ✅ Passed The changes directly address #9941 by guarding Windows substring expansion when GROK_HOME is empty or unset.
Out of Scope Changes check ✅ Passed The helper export and new tests are directly related to the Windows hook fix and do not appear out of scope.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

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


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: d3eddf61-1657-4adc-bf15-a3c7e82eaeab

📥 Commits

Reviewing files that changed from the base of the PR and between 6d55c7f and 02a2a4daba53bc30b8d3e3363890f61e561ff464.

📒 Files selected for processing (2)
  • src/main/grok/hook-service.test.ts
  • src/main/grok/hook-service.ts

Comment thread src/main/grok/hook-service.ts Resolved
@nwparker nwparker added the bug Something isn't working label Jul 27, 2026
@innocarpe

Copy link
Copy Markdown
Contributor Author

Status ping: narrow Windows grok-hook.cmd guard when GROK_HOME is empty/unset (avoids the substring syntax error). Unit coverage + no open review threads.

cmd.exe %VAR:~n,m% on an undefined/empty GROK_HOME can syntax-fail
SessionStart. Only apply length and trailing-backslash checks inside
if defined GROK_HOME / if defined ORCA_GROK_HOME (stablyai#9941).
CodeRabbit: parenthesized if-blocks expand ORCA_GROK_HOME before set;
use goto-separated lines so length/trailing checks see GROK_HOME.
@AmethystLiang

Copy link
Copy Markdown
Contributor

Duplicate of #11782 (fixes #9358 / #9941).

Same root cause: managed grok-hook.cmd parse-time %GROK_HOME:~…% / trailing-backslash guards when GROK_HOME is empty/unset.

#11782 is the preferred survivor because it also targets both issues and replaces if \"%x:~-1%\"==\"\\\" with findstr (avoids the quote-parser failure when GROK_HOME is set). This PR was the earlier focused fix for #9941 — thank you @innocarpe.

If this branch has a meaningfully different implementation detail worth cherry-picking onto #11782, call it out there. Tracking / merge continues on #11782; issue tracking on #9358.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Windows: grok-hook.cmd syntax error when GROK_HOME is empty (SessionStart fails)

4 participants