All files / agents copilot.ts

92% Statements 23/25
62.5% Branches 5/8
100% Functions 6/6
95.83% Lines 23/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88                    9x 9x 9x                 9x                                         9x     9x     9x             9x 1x     9x 9x           9x 1x 1x 1x     9x 7x 7x 6x   1x       9x 1x 1x        
import { execFileSync, spawn } from 'node:child_process';
import * as fs from 'node:fs';
 
/**
 * On Windows, refreshes the Node process's PATH from the registry. winget
 * updates the registry but not the running process's environment, so freshly
 * installed binaries (like copilot.exe) wouldn't otherwise be discoverable.
 * On Linux, the installer prepends ~/.local/bin to PATH directly.
 */
function refreshPath(): void {
    Iif (process.platform !== 'win32') return;
    try {
        const newPath = execFileSync(
            'powershell',
            [
                '-NoProfile',
                '-Command',
                "[System.Environment]::GetEnvironmentVariable('Path','Machine') + ';' + [System.Environment]::GetEnvironmentVariable('Path','User')",
            ],
            { encoding: 'utf8' },
        ).trim();
        process.env['PATH'] = newPath;
    } catch (err) {
        console.log(`  Warning: Could not refresh PATH: ${err instanceof Error ? err.message : String(err)}`);
    }
}
 
/**
 * Runs the GitHub Copilot CLI.
 *
 * The prompt is read in Node and passed as a direct OS-level argument via
 * child_process.spawn with shell: false.  This avoids all shell quoting /
 * metacharacter issues (cmd.exe and PowerShell both corrupt arguments
 * containing double quotes).  Works because winget installs copilot as a
 * native executable, not a .cmd shim.
 */
export async function runCopilotCli(
    promptFilePath: string,
    model: string | undefined,
    workingDirectory: string,
    timeoutMs: number,
): Promise<void> {
    refreshPath();
 
    // Read prompt in Node — no shell involved
    const promptContent = fs.readFileSync(promptFilePath, 'utf8');
 
    // Each element becomes one OS-level argument — no shell interpretation
    const args = [
        '-p', promptContent,
        '--allow-all-paths',
        '--allow-all-tools',
        '--deny-tool', 'shell(git push)',
        '--no-color',
    ];
    if (model) {
        args.push('--model', model);
    }
 
    return new Promise((resolve, reject) => {
        const proc = spawn('copilot', args, {
            shell: false,
            stdio: 'inherit',
            cwd: workingDirectory,
        });
 
        const timeoutId = setTimeout(() => {
            console.log(`\n  Timeout reached (${timeoutMs / 60000} min). Terminating...`);
            proc.kill('SIGTERM');
            reject(new Error(`Copilot review timed out after ${timeoutMs / 60000} minutes`));
        }, timeoutMs);
 
        proc.on('close', (code) => {
            clearTimeout(timeoutId);
            if (code === 0) {
                resolve();
            } else {
                reject(new Error(`Copilot CLI exited with code: ${code}`));
            }
        });
 
        proc.on('error', (err) => {
            clearTimeout(timeoutId);
            reject(new Error(`Failed to run Copilot CLI: ${err.message}`));
        });
    });
}