bada-terminal / src /ipc /codeburn-ipc.js
VanyaJ's picture
Release BADA Terminal v2.1.0 (Enterprise Ontology, NVIDIA auto-ontology, Release Preflight)
d739b1b verified
Raw History Blame Contribute Delete
10.9 kB
// CodeBurn ์ค€๋น„/์‹ค์ œ ์ข…๋ฃŒ ์†Œ์œ  ๊ณ„์•ฝ: docs/specs/app-lifecycle.md v1.11.0
const pty = require('node-pty');
const { dialog } = require('electron');
const fs = require('fs');
const os = require('os');
const terminalCleanup = require('../services/app-terminal-cleanup');
const { assertHostPathSupported } = require('../security/host-launch');
const { scrubAgentEnvironment, MODEL_KEYS, RUNTIME_KEYS } = require('../security/agent-environment');
// 2. CodeBurn
let codeburnPty = null;
let stopping = false, startQueue = Promise.resolve();
const pendingStarts = new Set();
module.exports = {
getCodeburnPty() {
return codeburnPty;
},
setCodeburnPty(val) {
codeburnPty = val;
},
async stopCodeburn() {
stopping = true;
// ์‘๋‹ต์ด ๋Šฆ์€ ์ค€๋น„ ๊ฒฐ๊ณผ๋„ ๋จผ์ € ์ธ์ˆ˜ํ•œ ๋’ค ๊ฐ™์€ ์ข…๋ฃŒ ์žฅ๋ฒฝ์—์„œ ํšŒ์ˆ˜ํ•œ๋‹ค.
await Promise.allSettled([...pendingStarts]);
await terminalCleanup.closeCodeburnTerminal(module.exports.getCodeburnPty, module.exports.setCodeburnPty);
},
register(ipcMain, ctx) {
const start = async () => {
assertHostPathSupported('uncontained-task');
const targetWindow = ctx.codeburnWindow;
const cancelled = () => stopping || ctx.isQuitting || ctx.codeburnWindow !== targetWindow || targetWindow?.isDestroyed();
if (cancelled()) return;
ipcMain.removeAllListeners('codeburn-input');
ipcMain.removeAllListeners('codeburn-resize');
if (codeburnPty) {
try {
await terminalCleanup.closeCodeburnTerminal(module.exports.getCodeburnPty, module.exports.setCodeburnPty);
} catch(error) { console.error('[CodeBurn] ์ด์ „ ํ”„๋กœ์„ธ์Šค ์ข…๋ฃŒ ๋ฏธํ™•์ธ:', error.code || 'TERMINAL_CLOSE_FAILED'); return; }
}
if (cancelled()) return;
// โ”€โ”€ ๋ถ„๊ธฐ A: ์›๊ฒฉ/๋กœ์ปฌ Docker ์ƒŒ๋“œ๋ฐ•์Šค๊ฐ€ ์—ฐ๊ฒฐ๋˜์–ด ๊ฐ€๋™ ์ค‘์ธ ๊ฒฝ์šฐ (์ปจํ…Œ์ด๋„ˆ ๋‚ด์—์„œ codeburn ์ง์ ‘ ์‹คํ–‰) โ”€โ”€
if (ctx.activeDockerSandbox && ctx.activeDockerSandbox.execWrapper) {
const execWrapper = ctx.activeDockerSandbox.execWrapper;
try {
assertHostPathSupported('uncontained-task');
const result = await execWrapper.createInteractiveShell({
cols: 80,
rows: 24,
shell: 'codeburn'
});
const stream = result.stream;
codeburnPty = {
write: (data) => stream.write(data),
resize: (cols, rows) => {
if (result.exec && result.exec.resize) {
result.exec.resize(cols, rows).catch(() => {});
}
},
destroy: () => stream.end()
};
const current = codeburnPty;
if (cancelled()) { await terminalCleanup.closeCodeburnTerminal(module.exports.getCodeburnPty, module.exports.setCodeburnPty); return; }
targetWindow?.once('closed', () => {
if (codeburnPty !== current) return;
terminalCleanup.closeTerminal(current).catch(error => console.error('[CodeBurn] ์ปจํ…Œ์ด๋„ˆ ํ”„๋กœ์„ธ์Šค ์ข…๋ฃŒ ๋ฏธํ™•์ธ:', error.code || 'TERMINAL_CLOSE_FAILED'));
});
stream.on('data', (data) => {
if (ctx.codeburnWindow && !ctx.codeburnWindow.isDestroyed()) {
ctx.codeburnWindow.webContents.send('codeburn-output', data.toString());
}
});
stream.on('end', () => {
if (codeburnPty !== current) return;
ipcMain.removeAllListeners('codeburn-input');
ipcMain.removeAllListeners('codeburn-resize');
// stream.end๋Š” ์›๊ฒฉ exec ์ข…๋ฃŒ ์ฆ๊ฑฐ๊ฐ€ ์•„๋‹ˆ๋‹ค. ํ˜„์žฌ ์†Œ์œ  ์ฐธ์กฐ๋ฅผ ์œ ์ง€ํ•œ๋‹ค.
if (ctx.codeburnWindow && !ctx.codeburnWindow.isDestroyed()) {
ctx.codeburnWindow.webContents.send('codeburn-output', `\r\n\r\n\x1b[33m[์›๊ฒฉ ์—ฐ๊ฒฐ์ด ์ข…๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ํ”„๋กœ์„ธ์Šค ์ข…๋ฃŒ๋Š” ํ™•์ธ๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.]\x1b[0m\r\n`);
ipcMain.once('codeburn-input', () => {
if (ctx.codeburnWindow && !ctx.codeburnWindow.isDestroyed()) {
ctx.codeburnWindow.close();
}
});
}
});
ipcMain.on('codeburn-input', (ev, data) => {
if (!cancelled() && codeburnPty === current) { try { if (data !== '\x03') assertHostPathSupported('uncontained-task'); current.write(data); } catch { /* ์ข…๋ฃŒ ์ค‘ ์ž…๋ ฅ ์‹คํŒจ๋Š” ์ƒˆ ์ž‘์—…์œผ๋กœ ์žฌ์‹œ๋„ํ•˜์ง€ ์•Š๋Š”๋‹ค. */ } }
});
ipcMain.on('codeburn-resize', (ev, { cols, rows }) => {
if (!cancelled() && codeburnPty === current) current.resize(cols, rows);
});
return;
} catch (err) {
if (cancelled() || codeburnPty) {
console.error('[CodeBurn] ์ปจํ…Œ์ด๋„ˆ ํ”„๋กœ์„ธ์Šค ์ข…๋ฃŒ ๋ฏธํ™•์ธ:', err.code || 'TERMINAL_CLOSE_FAILED');
return;
}
console.error('[CodeBurn] Failed to spawn codeburn inside container, falling back to local:', err);
}
}
// โ”€โ”€ ๋ถ„๊ธฐ B: ๋กœ์ปฌ ์„ธ์…˜ ์‹คํ–‰ (๊ธฐ์กด ๋กœ์ปฌ node-pty ํ˜ธ์ถœ) โ”€โ”€
const isWin = process.platform === 'win32';
let shell_cmd = isWin ? 'powershell.exe' : (process.env.SHELL || '/bin/bash');
const args = isWin
? ['-NoLogo', '-Command', 'codeburn']
: ['-c', 'export PATH="/opt/homebrew/bin:/usr/local/bin:$PATH"; codeburn'];
if (!isWin && !fs.existsSync(shell_cmd)) {
if (fs.existsSync('/bin/zsh')) {
shell_cmd = '/bin/zsh';
} else if (fs.existsSync('/bin/bash')) {
shell_cmd = '/bin/bash';
} else {
shell_cmd = '/bin/sh';
}
}
const cleanEnv = scrubAgentEnvironment(process.env, { trustedKeys: [...MODEL_KEYS, ...RUNTIME_KEYS] });
if (ctx.activeDockerSandbox && ctx.activeDockerSandbox.sessionId) {
cleanEnv['CLAUDE_CODE_SESSION_ID'] = ctx.activeDockerSandbox.sessionId;
}
let workDir = process.env.USERPROFILE || process.env.HOME || process.cwd();
if (!fs.existsSync(workDir)) {
workDir = os.homedir() || os.tmpdir() || '/';
}
let spawnSuccess = false, fallbackTimer;
try {
assertHostPathSupported('uncontained-task');
codeburnPty = pty.spawn(shell_cmd, args, {
name: 'xterm-256color',
cols: 80,
rows: 24,
cwd: workDir,
env: cleanEnv
});
spawnSuccess = true;
} catch (error) {
try {
assertHostPathSupported('uncontained-task');
codeburnPty = pty.spawn(shell_cmd, [], {
name: 'xterm-256color',
cols: 80,
rows: 24,
cwd: workDir,
env: cleanEnv
});
spawnSuccess = true;
const fallbackChild = codeburnPty;
fallbackTimer = setTimeout(() => {
if (cancelled() || codeburnPty !== fallbackChild) return;
try { assertHostPathSupported('uncontained-task'); fallbackChild.write('codeburn\r'); } catch(e) { /* ์ข…๋ฃŒ ์ค‘์ธ ์ž์‹์— ์žฌ์ž…๋ ฅํ•˜์ง€ ์•Š๋Š”๋‹ค. */ }
}, 1000);
} catch (fallbackError) {
const errorMessage = `CodeBurn ํ„ฐ๋ฏธ๋„ ํ”„๋กœ์„ธ์Šค๋ฅผ ์‹œ์ž‘ํ•˜์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค.\n\n์˜ค๋ฅ˜: ${fallbackError.message}`;
dialog.showErrorBox('CodeBurn ์‹œ์ž‘ ์˜ค๋ฅ˜', errorMessage);
}
}
if (spawnSuccess && codeburnPty) {
const current = codeburnPty;
let outputDisposable, exitDisposable, exited = false;
const onWindowClosed = () => {
if (codeburnPty !== current) return;
clearTimeout(fallbackTimer);
terminalCleanup.closeTerminal(current).then(() => {
if (codeburnPty === current) codeburnPty = null;
}, error => console.error('[CodeBurn] ํ”„๋กœ์„ธ์Šค ์ข…๋ฃŒ ๋ฏธํ™•์ธ:', error.code || 'TERMINAL_CLOSE_FAILED'));
if (codeburnPty === current) {
ipcMain.removeAllListeners('codeburn-input');
ipcMain.removeAllListeners('codeburn-resize');
}
};
targetWindow?.once('closed', onWindowClosed);
try {
exitDisposable = current.onExit((e) => {
exited = true;
clearTimeout(fallbackTimer);
targetWindow?.removeListener('closed', onWindowClosed);
// ๊ฐ™์€ exit ์ด๋ฒคํŠธ์˜ ์•ฑ ์†Œ์œ  ์žฅ๋ฒฝ์ด ๋จผ์ € ๊ด€์ธกํ•œ ๋’ค ๊ตฌ๋…์„ ํšŒ์ˆ˜ํ•œ๋‹ค.
Promise.resolve().then(() => {
try { outputDisposable?.dispose(); } catch { /* ์ข…๋ฃŒ ๋’ค ์ถœ๋ ฅ ๊ตฌ๋… ์ •๋ฆฌ. */ }
try { exitDisposable?.dispose(); } catch { /* ์ข…๋ฃŒ ๋’ค exit ๊ตฌ๋… ์ •๋ฆฌ. */ }
});
if (codeburnPty !== current) return;
ipcMain.removeAllListeners('codeburn-input');
ipcMain.removeAllListeners('codeburn-resize');
codeburnPty = null;
if (ctx.codeburnWindow && !ctx.codeburnWindow.isDestroyed()) {
ctx.codeburnWindow.webContents.send('codeburn-output', `\r\n\r\n\x1b[33m[ํ”„๋กœ์„ธ์Šค๊ฐ€ ์ข…๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค (Exit Code: ${e.exitCode || 0}). ์ฐฝ์„ ๋‹ซ์œผ๋ ค๋ฉด ์•„๋ฌด ํ‚ค๋‚˜ ๋ˆ„๋ฅด์‹ญ์‹œ์˜ค.]\x1b[0m\r\n`);
ipcMain.once('codeburn-input', () => {
if (ctx.codeburnWindow && !ctx.codeburnWindow.isDestroyed()) {
ctx.codeburnWindow.close();
}
});
}
});
if (exited) return;
outputDisposable = current.onData((data) => {
if (codeburnPty === current && ctx.codeburnWindow && !ctx.codeburnWindow.isDestroyed()) {
ctx.codeburnWindow.webContents.send('codeburn-output', data);
}
});
ipcMain.on('codeburn-input', (ev, data) => {
if (!cancelled() && codeburnPty === current) { try { if (data !== '\x03') assertHostPathSupported('uncontained-task'); current.write(data); } catch { /* ์ข…๋ฃŒ ์ค‘ ์ž…๋ ฅ ์‹คํŒจ๋Š” ์ƒˆ ์ž‘์—…์œผ๋กœ ์žฌ์‹œ๋„ํ•˜์ง€ ์•Š๋Š”๋‹ค. */ } }
});
ipcMain.on('codeburn-resize', (ev, { cols, rows }) => {
if (!cancelled() && codeburnPty === current) current.resize(cols, rows);
});
} catch (error) {
clearTimeout(fallbackTimer);
try { await terminalCleanup.closeCodeburnTerminal(module.exports.getCodeburnPty, module.exports.setCodeburnPty); } catch { /* ์ •๋ฆฌ ์‹คํŒจ๋กœ ์ตœ์ดˆ ๋“ฑ๋ก ์˜ค๋ฅ˜๋ฅผ ๋ฎ์ง€ ์•Š๋Š”๋‹ค. */ }
throw error;
}
}
};
ipcMain.on('codeburn-start', () => {
// ๊ฐ™์€ ์ฐฝ์˜ ์—ฐ์† ์‹œ์ž‘๋„ ์•ž์„  ์ž์‹์˜ ์ข…๋ฃŒ๋ฅผ ์ถ”์›”ํ•˜์ง€ ์•Š๋Š”๋‹ค.
const task = startQueue.then(start); startQueue = task.catch(() => {}); pendingStarts.add(task);
task.then(() => pendingStarts.delete(task), error => { pendingStarts.delete(task); console.error('[CodeBurn] ์‹œ์ž‘ ์‹คํŒจ:', error.code || 'CODEBURN_START_FAILED'); });
return task;
});
}
};