Repository navigation
fix(windows): spawn Grok status hooks via a GUI launcher #15530
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| #!/usr/bin/env node | ||
|
|
||
| import { spawnSync } from 'node:child_process' | ||
| import { existsSync, mkdirSync } from 'node:fs' | ||
| import { dirname, join, resolve } from 'node:path' | ||
|
|
||
| if (process.platform !== 'win32') { | ||
| throw new Error( | ||
| 'Windows Grok hook launcher compilation requires a Windows host; refusing to package without it.' | ||
| ) | ||
| } | ||
|
|
||
| const repoRoot = resolve(import.meta.dirname, '../..') | ||
| const sourcePath = join(repoRoot, 'native', 'windows-grok-hook-launcher', 'OrcaGrokHookLauncher.cs') | ||
| const outputPath = readArg('--output') ?? defaultOutputPath(repoRoot) | ||
| const compilerPath = findFrameworkCompiler(process.env) | ||
|
|
||
| if (!compilerPath) { | ||
| throw new Error('Unable to find the .NET Framework C# compiler required for orca-grok-hook.exe.') | ||
| } | ||
|
|
||
| mkdirSync(dirname(outputPath), { recursive: true }) | ||
| const result = spawnSync( | ||
| compilerPath, | ||
| ['/nologo', '/target:winexe', '/optimize+', '/warnaserror+', `/out:${outputPath}`, sourcePath], | ||
| { cwd: repoRoot, stdio: 'inherit' } | ||
| ) | ||
|
|
||
| if (result.signal) { | ||
| process.kill(process.pid, result.signal) | ||
| } | ||
| if (result.error) { | ||
| throw result.error | ||
| } | ||
| if (result.status !== 0) { | ||
| process.exit(result.status ?? 1) | ||
| } | ||
|
|
||
| function defaultOutputPath(projectRoot) { | ||
| return join(projectRoot, 'native', 'windows-grok-hook-launcher', '.build', 'orca-grok-hook.exe') | ||
| } | ||
|
|
||
| function findFrameworkCompiler(env) { | ||
| const windowsDirectory = env.WINDIR ?? env.SystemRoot | ||
| if (!windowsDirectory) { | ||
| return null | ||
| } | ||
| const candidates = [ | ||
| join(windowsDirectory, 'Microsoft.NET', 'Framework64', 'v4.0.30319', 'csc.exe'), | ||
| join(windowsDirectory, 'Microsoft.NET', 'Framework', 'v4.0.30319', 'csc.exe') | ||
| ] | ||
| return candidates.find((candidate) => existsSync(candidate)) ?? null | ||
| } | ||
|
|
||
| function readArg(name) { | ||
| const index = process.argv.indexOf(name) | ||
| return index !== -1 ? process.argv[index + 1] : undefined | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| .build/ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| using System; | ||
| using System.Diagnostics; | ||
| using System.IO; | ||
| using System.Reflection; | ||
| using System.Threading; | ||
|
|
||
| internal static class OrcaGrokHookLauncher | ||
| { | ||
| private const int ChildExitTimeoutMilliseconds = 4000; | ||
|
|
||
| [STAThread] | ||
| private static int Main() | ||
| { | ||
| try | ||
| { | ||
| string launcherDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); | ||
| string scriptPath = Path.Combine(launcherDirectory ?? ".", "grok-hook.cmd"); | ||
| if (!File.Exists(scriptPath)) | ||
| { | ||
| string home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); | ||
| scriptPath = Path.Combine(home, ".orca", "agent-hooks", "grok-hook.cmd"); | ||
| } | ||
| if (!File.Exists(scriptPath)) | ||
| { | ||
| return 0; | ||
| } | ||
|
|
||
| ProcessStartInfo startInfo = new ProcessStartInfo(); | ||
| startInfo.FileName = Environment.GetEnvironmentVariable("ComSpec") ?? "cmd.exe"; | ||
| startInfo.Arguments = "/d /c \"" + scriptPath + "\""; | ||
| startInfo.UseShellExecute = false; | ||
| startInfo.CreateNoWindow = true; | ||
| startInfo.WindowStyle = ProcessWindowStyle.Hidden; | ||
| startInfo.RedirectStandardInput = true; | ||
| startInfo.RedirectStandardOutput = true; | ||
| startInfo.RedirectStandardError = true; | ||
| using (Process child = Process.Start(startInfo)) | ||
| { | ||
| if (child == null) | ||
| { | ||
| return 0; | ||
| } | ||
|
|
||
| Thread stdinThread = new Thread(() => | ||
| { | ||
| try | ||
| { | ||
| using (Stream input = Console.OpenStandardInput()) | ||
| { | ||
| input.CopyTo(child.StandardInput.BaseStream); | ||
| } | ||
| child.StandardInput.Close(); | ||
| } | ||
| catch | ||
| { | ||
| } | ||
| }); | ||
| stdinThread.IsBackground = true; | ||
| stdinThread.Start(); | ||
|
|
||
| Drain(child.StandardOutput.BaseStream); | ||
| Drain(child.StandardError.BaseStream); | ||
| if (!child.WaitForExit(ChildExitTimeoutMilliseconds)) | ||
| { | ||
| try | ||
| { | ||
| child.Kill(); | ||
| } | ||
| catch | ||
| { | ||
| } | ||
| } | ||
| } | ||
| } | ||
| catch | ||
| { | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| private static void Drain(Stream stream) | ||
| { | ||
| Thread thread = new Thread(() => | ||
| { | ||
| try | ||
| { | ||
| byte[] buffer = new byte[4096]; | ||
| while (stream.Read(buffer, 0, buffer.Length) > 0) | ||
| { | ||
| } | ||
| } | ||
| catch | ||
| { | ||
| } | ||
| }); | ||
| thread.IsBackground = true; | ||
| thread.Start(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,4 +1,6 @@ | ||||||||||||||||||||||||||||||||||
| import { join } from 'node:path' | ||||||||||||||||||||||||||||||||||
| import { copyFileSync, existsSync, mkdirSync } from 'node:fs' | ||||||||||||||||||||||||||||||||||
| import { homedir } from 'node:os' | ||||||||||||||||||||||||||||||||||
| import { dirname, join } from 'node:path' | ||||||||||||||||||||||||||||||||||
| import type { SFTPWrapper } from 'ssh2' | ||||||||||||||||||||||||||||||||||
| import type { AgentHookInstallState, AgentHookInstallStatus } from '../../shared/agent-hook-types' | ||||||||||||||||||||||||||||||||||
| import { resolveGrokHomeDir } from '../../shared/grok-session-paths' | ||||||||||||||||||||||||||||||||||
|
|
@@ -101,10 +103,49 @@ function getManagedScriptPath(): string { | |||||||||||||||||||||||||||||||||
| return getSharedManagedScriptPath(getManagedScriptFileName()) | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| const WINDOWS_GROK_HOOK_LAUNCHER_NAME = 'orca-grok-hook.exe' | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| function isGrokManagedCommand(command: string | undefined): boolean { | ||||||||||||||||||||||||||||||||||
| if (createManagedCommandMatcher(getManagedScriptFileName())(command)) { | ||||||||||||||||||||||||||||||||||
| return true | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| return Boolean(command?.replaceAll('\\', '/').includes(WINDOWS_GROK_HOOK_LAUNCHER_NAME)) | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+108
to
+113
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win Restrict launcher matching to the managed launcher path. Line 112 matches any command that contains Proposed fix- return Boolean(command?.replaceAll('\\', '/').includes(WINDOWS_GROK_HOOK_LAUNCHER_NAME))
+ return Boolean(
+ command
+ ?.replaceAll('\\', '/')
+ .endsWith(`/agent-hooks/${WINDOWS_GROK_HOOK_LAUNCHER_NAME}`)
+ )📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| function bundledWindowsGrokHookLauncherPath(): string | null { | ||||||||||||||||||||||||||||||||||
| const candidates = [ | ||||||||||||||||||||||||||||||||||
| ...(process.resourcesPath | ||||||||||||||||||||||||||||||||||
| ? [join(process.resourcesPath, 'bin', WINDOWS_GROK_HOOK_LAUNCHER_NAME)] | ||||||||||||||||||||||||||||||||||
| : []), | ||||||||||||||||||||||||||||||||||
| join(process.cwd(), 'native', 'windows-grok-hook-launcher', '.build', WINDOWS_GROK_HOOK_LAUNCHER_NAME) | ||||||||||||||||||||||||||||||||||
| ] | ||||||||||||||||||||||||||||||||||
| return candidates.find((candidate) => existsSync(candidate)) ?? null | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| function publishWindowsGrokHookLauncher(): string | null { | ||||||||||||||||||||||||||||||||||
| const source = bundledWindowsGrokHookLauncherPath() | ||||||||||||||||||||||||||||||||||
| if (!source) { | ||||||||||||||||||||||||||||||||||
| return null | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| const destination = join(homedir(), '.orca', 'agent-hooks', WINDOWS_GROK_HOOK_LAUNCHER_NAME) | ||||||||||||||||||||||||||||||||||
| mkdirSync(dirname(destination), { recursive: true }) | ||||||||||||||||||||||||||||||||||
| copyFileSync(source, destination) | ||||||||||||||||||||||||||||||||||
| return destination | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+125
to
+133
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🩺 Stability & Availability | 🟠 Major | ⚡ Quick win Do not overwrite the launcher during status checks.
|
||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| function getManagedCommand(scriptPath: string): string { | ||||||||||||||||||||||||||||||||||
| return process.platform === 'win32' | ||||||||||||||||||||||||||||||||||
| ? wrapWindowsHookCommand(scriptPath) | ||||||||||||||||||||||||||||||||||
| : wrapPosixHookCommand(scriptPath) | ||||||||||||||||||||||||||||||||||
| if (process.platform !== 'win32') { | ||||||||||||||||||||||||||||||||||
| return wrapPosixHookCommand(scriptPath) | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| // Why: Grok treats a command string with spaces as a pwsh -Command script | ||||||||||||||||||||||||||||||||||
| // (#14828). A single space-free GUI-subsystem exe is spawned directly, so | ||||||||||||||||||||||||||||||||||
| // no console window appears. Profile paths with spaces still need the | ||||||||||||||||||||||||||||||||||
| // encoded PowerShell launcher (#6078). | ||||||||||||||||||||||||||||||||||
| const launcher = publishWindowsGrokHookLauncher() | ||||||||||||||||||||||||||||||||||
| if (launcher && !/\s/.test(launcher)) { | ||||||||||||||||||||||||||||||||||
| return launcher.replaceAll('\\', '/') | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| return wrapWindowsHookCommand(scriptPath) | ||||||||||||||||||||||||||||||||||
|
Comment on lines
136
to
+148
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win Recognize stale PowerShell commands in When the executable is available, this function returns its path. Lines 252-254 then use exact equality, so existing encoded PowerShell hook commands report |
||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| function getManagedScript(target: 'local' | 'posix' = 'local'): string { | ||||||||||||||||||||||||||||||||||
|
|
@@ -152,7 +193,7 @@ function buildInstalledConfig( | |||||||||||||||||||||||||||||||||
| scriptFileName: string | ||||||||||||||||||||||||||||||||||
| ): void { | ||||||||||||||||||||||||||||||||||
| const nextHooks = { ...config.hooks } | ||||||||||||||||||||||||||||||||||
| const isManagedCommand = createManagedCommandMatcher(scriptFileName) | ||||||||||||||||||||||||||||||||||
| const isManagedCommand = isGrokManagedCommand | ||||||||||||||||||||||||||||||||||
| const managedEvents = new Set<string>(GROK_EVENTS.map((event) => event.eventName)) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| // Why: Orca owns only grok-hook.* entries. Sweep stale managed commands out | ||||||||||||||||||||||||||||||||||
|
|
@@ -312,7 +353,7 @@ export class GrokHookService { | |||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| const nextHooks = { ...config.hooks } | ||||||||||||||||||||||||||||||||||
| const isManagedCommand = createManagedCommandMatcher(getManagedScriptFileName()) | ||||||||||||||||||||||||||||||||||
| const isManagedCommand = isGrokManagedCommand | ||||||||||||||||||||||||||||||||||
| for (const [eventName, definitions] of Object.entries(nextHooks)) { | ||||||||||||||||||||||||||||||||||
| if (!Array.isArray(definitions)) { | ||||||||||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
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.
🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
Drain stdin before returning when the script is missing.
The existing PowerShell fallback drains stdin for this case. Returning immediately closes the hook input pipe. Grok can then receive a broken-pipe error while it writes the event payload.
Proposed fix
if (!File.Exists(scriptPath)) { + DrainStandardInput(); return 0; }Add
DrainStandardInputwith the same best-effortConsole.OpenStandardInput().CopyTo(Stream.Null)behavior used by the child-input forwarding path.