Repository navigation
fix(windows): make managed grok-hook.cmd safe when GROK_HOME is unset - #11782
Conversation
Greptile SummaryThis PR fixes a Windows-only crash (
Confidence Score: 5/5Safe to merge — the change is scoped entirely to Windows cmd.exe script generation for the Grok hook, with no impact on POSIX paths or other hook services. The fix correctly addresses both cmd.exe parse-time failures: the goto-based guard prevents substring expansion on an undefined variable, and the findstr approach eliminates the broken quote-parser comparison. The extraFormLines refactor removes the only fragile coupling between the Grok template and the shared post-command builder. Template-assertion and Windows spawn tests cover the unset-GROK_HOME, trailing-backslash, and structural contract cases. No logic issues found across all four changed files. Files Needing Attention: No files require special attention.
|
| Filename | Overview |
|---|---|
| src/main/grok/windows-grok-hook-script.ts | New module containing the corrected Windows grok-hook.cmd generator; uses goto-based guard and findstr for trailing-backslash detection — core of the fix. |
| src/main/agent-hooks/installer-utils.ts | Adds optional extraFormLines parameter to buildWindowsAgentHookPostCommand, removing the need for fragile string-replace to inject grokHome; backward-compatible with default []. |
| src/main/grok/hook-service.ts | Simplified to delegate Windows script generation to buildWindowsGrokHookScript(); imports GROK_HOME_ENVELOPE_MAX_LENGTH for use in the POSIX branch. |
| src/main/grok/hook-service.test.ts | New template-assertion test (cross-platform) plus two Windows-only spawn tests covering unset GROK_HOME and trailing-backslash path; spawn tests correctly delete the key rather than empty it. |
Flowchart
%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[grok-hook.cmd starts] --> B[setlocal / env hook call]
B --> C{ORCA_AGENT_HOOK_PORT\nORCA_AGENT_HOOK_TOKEN\nORCA_PANE_KEY set?}
C -- no --> D[drain stdin → exit 0]
C -- yes --> E[set ORCA_GROK_HOME= empty]
E --> F{GROK_HOME defined?}
F -- no --> K[:orca_grok_home_ready]
F -- yes --> G[set ORCA_GROK_HOME=%GROK_HOME%]
G --> H{len > 4096?}
H -- yes --> I[clear ORCA_GROK_HOME]
H -- no --> J{ORCA_GROK_HOME defined\nand ends with backslash?\nfindstr /r /c:\\\$}
I --> K
J -- yes --> L[append . to strip backslash]
J -- no --> K
L --> K
K[curl POST /hook/grok\nwith grokHome=%ORCA_GROK_HOME%] --> M[exit /b 0]
Reviews (2): Last reviewed commit: "fix(windows): harden grok-hook GROK_HOME..." | Re-trigger Greptile
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (5)
🚧 Files skipped from review as they are similar to previous changes (5)
📝 WalkthroughWalkthroughThis PR extracts Windows Grok hook script generation into a new module. The module exports the 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
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 |
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (1)
src/main/grok/hook-service.test.ts (1)
47-47: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winAvoid hardcoding the length-cap literal in assertions.
expect(script).toContain('%ORCA_GROK_HOME:~4096,1%')hardcodes4096instead of interpolating the importedGROK_HOME_ENVELOPE_MAX_LENGTHconstant. If the constant changes later, this assertion keeps checking the old value instead of failing loudly or tracking the new one.♻️ Proposed fix
-import { buildWindowsGrokHookScript } from './windows-grok-hook-script' +import { buildWindowsGrokHookScript, GROK_HOME_ENVELOPE_MAX_LENGTH } from './windows-grok-hook-script' ... - expect(script).toContain('%ORCA_GROK_HOME:~4096,1%') + expect(script).toContain(`%ORCA_GROK_HOME:~${GROK_HOME_ENVELOPE_MAX_LENGTH},1%`)Also applies to: 160-160
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 8e7d5824-e70b-4489-9171-216625987846
📥 Commits
Reviewing files that changed from the base of the PR and between 79251d7 and b8822be4824d1c0944108c4238c367317e1a4773.
📒 Files selected for processing (3)
src/main/grok/hook-service.test.tssrc/main/grok/hook-service.tssrc/main/grok/windows-grok-hook-script.ts
| const WINDOWS_HOOK_PAYLOAD_FORM_LINE = ' --data-urlencode "payload@-" >nul 2>nul' | ||
|
|
||
| const WINDOWS_GROK_HOOK_POST_COMMAND = buildWindowsAgentHookPostCommand('grok').replace( | ||
| WINDOWS_HOOK_PAYLOAD_FORM_LINE, | ||
| ` --data-urlencode "grokHome=%ORCA_GROK_HOME%" ^\r\n${WINDOWS_HOOK_PAYLOAD_FORM_LINE}` | ||
| ) |
There was a problem hiding this comment.
🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win
Harden the grokHome injection against silent drift.
WINDOWS_GROK_HOOK_POST_COMMAND depends on an exact literal-string match between WINDOWS_HOOK_PAYLOAD_FORM_LINE and the last line produced by buildWindowsAgentHookPostCommand('grok') in installer-utils.ts. If that shared builder's output changes (whitespace, flag order, new line), .replace() finds no match and returns the string unchanged. The script would then post to /hook/grok without a grokHome parameter, with no error and no test failure, since no test currently asserts that grokHome=%ORCA_GROK_HOME% appears in the built command.
Add a guard that fails loudly if the replacement did not occur, and add a test asserting the grokHome data-urlencode line is present in the generated script.
🛡️ Proposed fix to fail loudly on drift
-const WINDOWS_GROK_HOOK_POST_COMMAND = buildWindowsAgentHookPostCommand('grok').replace(
- WINDOWS_HOOK_PAYLOAD_FORM_LINE,
- ` --data-urlencode "grokHome=%ORCA_GROK_HOME%" ^\r\n${WINDOWS_HOOK_PAYLOAD_FORM_LINE}`
-)
+const WINDOWS_GROK_HOOK_POST_COMMAND_BASE = buildWindowsAgentHookPostCommand('grok')
+const WINDOWS_GROK_HOOK_POST_COMMAND = WINDOWS_GROK_HOOK_POST_COMMAND_BASE.replace(
+ WINDOWS_HOOK_PAYLOAD_FORM_LINE,
+ ` --data-urlencode "grokHome=%ORCA_GROK_HOME%" ^\r\n${WINDOWS_HOOK_PAYLOAD_FORM_LINE}`
+)
+if (WINDOWS_GROK_HOOK_POST_COMMAND === WINDOWS_GROK_HOOK_POST_COMMAND_BASE) {
+ throw new Error(
+ 'buildWindowsAgentHookPostCommand output no longer matches WINDOWS_HOOK_PAYLOAD_FORM_LINE; grokHome injection failed.'
+ )
+}Fixes stablyai#9358 and stablyai#9941. cmd.exe expands %VAR:~n,m% at parse time. When GROK_HOME is unset (default outside Orca terminals), the generated length/trailing-backslash guards became a syntax error and every Grok hook event failed with exit 255. - Skip substring work when GROK_HOME is undefined (if defined + goto) - Replace if "%x:~-1%"=="\" (itself a quote-parser bug) with findstr - Extract Windows script builder; add template + spawn tests
Address review on stablyai#11782: - Inject grokHome via buildWindowsAgentHookPostCommand extra form lines (no fragile string replace of the shared payload line) - Spawn tests delete GROK_HOME and keep PORT/TOKEN/PANE_KEY set so the GROK_HOME path actually runs before curl
b8822be to
db5b578
Compare
Dedupe note#9941 was closed as Duplicate of #9358 (same Windows Related earlier PR for the same root cause: #10049 (@innocarpe, claimed on #9941 with a narrower |
|
Taking a look! |
|
Reviewed and hardened the Windows Grok hook fix.
Validated with the targeted Windows suite (9/9), typecheck, focused lint, a fresh independent review/performance pass, and Electron headless status-lifecycle coverage. |
|
Note GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer. |
|
Thank you for the PR! |
…ace (#14221) On Windows any open read handle on the target - AV scanner, backup agent, search indexer, editor - fails rename() with EPERM. refreshExistingManagedScripts only logs, so the script stays frozen at whatever an older Orca generated and the agent keeps invoking it, with no user-visible signal and no self-heal on relaunch. That is how #14221 reports a pre-#11782 grok-hook.cmd on v1.4.180: the builder is correct, the file on disk was never replaced. An in-place write still lands against that handle, so fall back to it rather than strand a stale script. Verified on Windows: rename EPERMs against an open read handle where writeFile succeeds; cmd.exe executing the script does not block either.
…stablyai#11782) * fix(windows): make managed grok-hook.cmd safe when GROK_HOME is unset Fixes stablyai#9358 and stablyai#9941. cmd.exe expands %VAR:~n,m% at parse time. When GROK_HOME is unset (default outside Orca terminals), the generated length/trailing-backslash guards became a syntax error and every Grok hook event failed with exit 255. - Skip substring work when GROK_HOME is undefined (if defined + goto) - Replace if "%x:~-1%"=="\" (itself a quote-parser bug) with findstr - Extract Windows script builder; add template + spawn tests * fix(windows): harden grok-hook GROK_HOME guards and tests Address review on stablyai#11782: - Inject grokHome via buildWindowsAgentHookPostCommand extra form lines (no fragile string replace of the shared payload line) - Spawn tests delete GROK_HOME and keep PORT/TOKEN/PANE_KEY set so the GROK_HOME path actually runs before curl * fix(windows): cover Grok hook home boundaries --------- Co-authored-by: OrcaWin <[email protected]>
Summary
Fixes #9358 and #9941.
Managed
~/.orca/agent-hooks/grok-hook.cmdfails with exit code 1/255 andThe syntax of the command is incorrecton every Grok hook event(
SessionStart,UserPromptSubmit, tool lifecycle, …) whenGROK_HOMEisunset — the default for Grok launched outside an Orca-managed terminal.
Root cause
cmd.exe expands
%VAR:~n,m%at parse time. The generated template did:When
GROK_HOMEis undefined:~…junk instead of empty."\"as the RHS, which itself is acmd quote-parser syntax error even when the variable is defined.
So every managed Grok hook aborted before the
curlPOST to/hook/grok.Fix
ORCA_GROK_HOME=andif not defined GROK_HOME gotopast allsubstring work (not a parenthesized block — those still parse-expand early).
%ORCA_GROK_HOME:~4096,1%only after assignment.\detection viafindstr /r /c:"\\$"— never=="\".windows-grok-hook-script.tsfor testability.Why not just the existing open PRs?
Several open PRs guard with
if definedbut still emitif "%ORCA_GROK_HOME:~-1%"=="\", which fails as soon asGROK_HOMEis set(e.g. inside Orca, or after the user applies the documented workaround). This
PR removes that comparison entirely.
Related Issue
Closes #9358
Closes #9941
Type of Change
Changes Made
src/main/grok/windows-grok-hook-script.ts— Windows script templatesrc/main/grok/hook-service.ts— use the safe buildersrc/main/grok/hook-service.test.ts— template assertions + Windowscmdspawn tests (unset
GROK_HOME, trailing-backslash path)How to Test
On Windows (covered by the spawn tests):
GROK_HOMEglobal/orca-status:user_prompt_submitshould no longerfail with exit code 1
Platform / SSH
grok-hook.shunchangedAI Review Notes
Checklist
7/7on Windows host)