Skip to content
Merged
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
20 changes: 8 additions & 12 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,20 @@ jobs:
restore-keys: |
${{ runner.os }}-bun-

- name: Cache OpenCode binary
id: cache-opencode
uses: actions/cache@v4
with:
path: ~/.bun/bin/opencode
key: ${{ runner.os }}-opencode-${{ hashFiles('.github/workflows/ci.yml') }}
restore-keys: |
${{ runner.os }}-opencode-

- name: Install dependencies
run: bun install

- name: Install OpenCode CLI
if: steps.cache-opencode.outputs.cache-hit != 'true'
run: bun install -g opencode-ai
run: |
bun install -g opencode-ai
echo "$HOME/.bun/bin" >> $GITHUB_PATH
shell: bash

- name: Verify OpenCode installation
run: opencode --version
run: |
export PATH="$HOME/.bun/bin:$PATH"
opencode --version
shell: bash

- name: Type check
run: bun run tsc -noEmit -skipLibCheck
Expand Down
103 changes: 100 additions & 3 deletions src/server/process/WindowsProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,130 @@ import { ChildProcess, spawn, SpawnOptions } from "child_process";
import { OpenCodeProcess } from "./OpenCodeProcess";

export class WindowsProcess implements OpenCodeProcess {
// Static state to track the current process for cleanup
private static currentProcess: ChildProcess | null = null;
private static cleanupHandlerRegistered = false;

start(
command: string,
args: string[],
options: SpawnOptions
): ChildProcess {
return spawn(command, args, {
const process = spawn(command, args, {
...options,
shell: true,
windowsHide: true,
});

// Store process for cleanup
WindowsProcess.currentProcess = process;
WindowsProcess.registerCleanupHandler();

return process;
}

async stop(process: ChildProcess): Promise<void> {
const pid = process.pid;
if (!pid) {
WindowsProcess.currentProcess = null;
return;
}

console.log("[OpenCode] Stopping server process tree, PID:", pid);

// Use taskkill with /T flag to kill process tree
await this.execAsync(`taskkill /T /F /PID ${pid}`);
// Method 1: Find and kill child processes (actual node.exe) using PowerShell
// This is necessary because shell: true spawns cmd.exe -> node.exe, and
// killing cmd.exe leaves node.exe orphaned
try {
const { execSync } = require("child_process");
const output = execSync(
`powershell -Command "Get-CimInstance Win32_Process -Filter \\"ParentProcessId=${pid}\\" | Select-Object ProcessId"`,
{ encoding: "utf8", stdio: ["pipe", "pipe", "ignore"] }
);

const lines = output.split("\n").slice(3); // Skip headers
for (const line of lines) {
const childPid = line.trim();
if (childPid && !isNaN(parseInt(childPid))) {
try {
execSync(`taskkill /F /PID ${childPid}`, { stdio: "ignore" });
} catch {
// Child may already be gone
}
}
}
} catch {
// PowerShell lookup failed, continue to other methods
}

// Method 2: Kill the parent process (cmd.exe)
try {
await this.execAsync(`taskkill /F /PID ${pid}`);
} catch {
// Parent may already be gone
}

// Clear stored process
WindowsProcess.currentProcess = null;

// Wait for process to exit
await this.waitForExit(process, 5000);
}

private static registerCleanupHandler(): void {
if (WindowsProcess.cleanupHandlerRegistered) {
return;
}

// Register beforeunload handler for window close cleanup
// Skip in CI/test environments to avoid interfering with test lifecycle
if (typeof window !== "undefined" && !process.env.CI) {
window.addEventListener("beforeunload", () => {
if (WindowsProcess.currentProcess?.pid) {
WindowsProcess.killProcessSync(WindowsProcess.currentProcess.pid);
}
});
WindowsProcess.cleanupHandlerRegistered = true;
}
}

private static killProcessSync(pid: number): void {
try {
const { execSync } = require("child_process");

// Method 1: Kill child processes using PowerShell
try {
const output = execSync(
`powershell -Command "Get-CimInstance Win32_Process -Filter \\"ParentProcessId=${pid}\\" | Select-Object ProcessId"`,
{ encoding: "utf8", stdio: ["pipe", "pipe", "ignore"] }
);

const lines = output.split("\n").slice(3);
for (const line of lines) {
const childPid = line.trim();
if (childPid && !isNaN(parseInt(childPid))) {
try {
execSync(`taskkill /F /PID ${childPid}`, { stdio: "ignore" });
} catch {
// Child may already be gone
}
}
}
} catch {
// PowerShell lookup failed
}

// Method 2: Kill parent process
try {
execSync(`taskkill /F /PID ${pid}`, { stdio: "ignore" });
} catch {
// Parent may already be gone
}
} catch {
// Process may already be gone
}
}

async verifyCommand(command: string): Promise<string | null> {
// Use 'where' command to check if executable exists in PATH
try {
Expand Down
4 changes: 2 additions & 2 deletions tests/process/PosixProcess.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ describe.skipIf(process.platform === "win32")("PosixProcess", () => {
});

test("returns null for existing absolute path", async () => {
// Use a binary that exists on this system (found via `which ls`)
const existingBinary = "/etc/profiles/per-user/mat/bin/ls";
// Use a binary that exists on most Unix systems
const existingBinary = "/bin/ls";
const result = await processImpl.verifyCommand(existingBinary);
expect(result).toBeNull();
});
Expand Down