Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions config/electron-builder.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ const { verifySkillsCliRuntime } = require('./scripts/verify-skills-cli-runtime.
const isMacHourly = process.env.ORCA_MAC_HOURLY === '1'
const isMacDaily = process.env.ORCA_MAC_DAILY === '1'
const isMacAdhoc = process.env.ORCA_MAC_ADHOC === '1'
const isMacRelease =
process.env.ORCA_MAC_RELEASE === '1' || isMacHourly || isMacDaily || isMacAdhoc
const isMacRelease = process.env.ORCA_MAC_RELEASE === '1' || isMacHourly || isMacDaily || isMacAdhoc
const isLinuxArm64Release = process.env.ORCA_LINUX_ARM64_RELEASE === '1'
const localBuildVersion = isMacRelease ? undefined : process.env.ORCA_LOCAL_BUILD_VERSION
const devChannelBuildVersion = isMacHourly
Expand Down Expand Up @@ -309,6 +308,10 @@ module.exports = {
from: 'native/windows-cli-launcher/.build/orca.exe',
to: 'bin/orca.exe'
},
{
from: 'native/windows-agent-hook-launcher/.build/orca-agent-hook.exe',
to: 'bin/orca-agent-hook.exe'
},
{
from: 'node_modules/agent-browser/bin/agent-browser-win32-x64.exe',
to: 'agent-browser-win32-x64.exe'
Expand Down
53 changes: 38 additions & 15 deletions config/scripts/build-windows-cli-launcher.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,50 @@ if (process.platform !== 'win32') {
const repoRoot = resolve(import.meta.dirname, '../..')
const sourcePath = join(repoRoot, 'native', 'windows-cli-launcher', 'OrcaCliLauncher.cs')
const outputPath = readArg('--output') ?? defaultOutputPath(repoRoot)
const hookSourcePath = join(
repoRoot,
'native',
'windows-agent-hook-launcher',
'OrcaAgentHookLauncher.cs'
)
const hookOutputPath =
readArg('--hook-output') ??
join(repoRoot, 'native', 'windows-agent-hook-launcher', '.build', 'orca-agent-hook.exe')
const compilerPath = findFrameworkCompiler(process.env)

if (!compilerPath) {
throw new Error('Unable to find the .NET Framework C# compiler required for orca.exe.')
throw new Error(
'Unable to find the .NET Framework C# compiler required for orca.exe and orca-agent-hook.exe.'
)
}

mkdirSync(dirname(outputPath), { recursive: true })
const result = spawnSync(
compilerPath,
['/nologo', '/target:exe', '/optimize+', '/warnaserror+', `/out:${outputPath}`, sourcePath],
{ cwd: repoRoot, stdio: 'inherit' }
)
compile(sourcePath, outputPath, 'exe')
compile(hookSourcePath, hookOutputPath, 'winexe')

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 compile(inputPath, destinationPath, target) {
mkdirSync(dirname(destinationPath), { recursive: true })
const result = spawnSync(
compilerPath,
[
'/nologo',
`/target:${target}`,
'/optimize+',
'/warnaserror+',
`/out:${destinationPath}`,
inputPath
],
{ 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) {
Expand Down
144 changes: 144 additions & 0 deletions native/windows-agent-hook-launcher/OrcaAgentHookLauncher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading.Tasks;

internal static class OrcaAgentHookLauncher
{
private const int StandardInputTimeoutMilliseconds = 1000;
private const int ChildExitTimeoutMilliseconds = 2500;

private static int Main(string[] args)
{
bool emitNeutralJson = args.Length == 2 && args[0] == "--neutral-json";
string scriptPath = args.Length == (emitNeutralJson ? 2 : 1) ? args[args.Length - 1] : null;
if (scriptPath == null || !File.Exists(scriptPath))
{
DrainStandardInputWithTimeout();
WriteNeutralJson(emitNeutralJson);
return 0;
}

try
{
string systemDirectory = Environment.GetFolderPath(Environment.SpecialFolder.System);
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = Path.Combine(systemDirectory, "cmd.exe"),
Arguments = "/d /s /c \"\"%ORCA_AGENT_HOOK_SCRIPT%\"\"",
UseShellExecute = false,
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden,
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true
};
startInfo.EnvironmentVariables["ORCA_AGENT_HOOK_SCRIPT"] = scriptPath;

using (Process child = Process.Start(startInfo))
{
Task inputTask = CopyStandardInputAsync(child.StandardInput.BaseStream);
Task<string> outputTask = child.StandardOutput.ReadToEndAsync();
Task<string> errorTask = child.StandardError.ReadToEndAsync();
inputTask.Wait(StandardInputTimeoutMilliseconds);
TryClose(child.StandardInput);

bool childTimedOut = !child.WaitForExit(ChildExitTimeoutMilliseconds);
if (childTimedOut)
{
KillProcessTree(child, systemDirectory);
}
Task.WaitAll(new Task[] { outputTask, errorTask }, ChildExitTimeoutMilliseconds);
WriteNeutralJson(emitNeutralJson);
return childTimedOut ? 0 : child.ExitCode;
}
}
catch
{
DrainStandardInputWithTimeout();
WriteNeutralJson(emitNeutralJson);
return 0;
}
}

private static void KillProcessTree(Process child, string systemDirectory)
{
try
{
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = Path.Combine(systemDirectory, "taskkill.exe"),
Arguments = "/PID " + child.Id + " /T /F",
UseShellExecute = false,
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden,
RedirectStandardOutput = true,
RedirectStandardError = true
};
using (Process treeKiller = Process.Start(startInfo))
{
treeKiller.BeginOutputReadLine();
treeKiller.BeginErrorReadLine();
if (!treeKiller.WaitForExit(ChildExitTimeoutMilliseconds))
{
treeKiller.Kill();
}
}
}
catch { }

if (!child.HasExited)
{
try
{
child.Kill();
}
catch { }
}
child.WaitForExit(ChildExitTimeoutMilliseconds);
}

private static Task CopyStandardInputAsync(Stream destination)
{
return Task.Run(() =>
{
try
{
Console.OpenStandardInput().CopyTo(destination);
destination.Flush();
}
catch
{
// The caller may leave stdin open after the payload is complete.
}
});
}

private static void DrainStandardInputWithTimeout()
{
CopyStandardInputAsync(Stream.Null).Wait(StandardInputTimeoutMilliseconds);
}

private static void TryClose(TextWriter writer)
{
try
{
writer.Close();
}
catch { }
}

private static void WriteNeutralJson(bool enabled)
{
if (!enabled) return;
try
{
byte[] output = Encoding.UTF8.GetBytes("{}\n");
Stream stdout = Console.OpenStandardOutput();
stdout.Write(output, 0, output.Length);
stdout.Flush();
}
catch { }
}
}
Loading