mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-07-23 00:01:24 -07:00
Implement background process monitoring and inspection tools (#23799)
This commit is contained in:
@@ -128,6 +128,7 @@ const mockProcessKill = vi
|
||||
.mockImplementation(() => true);
|
||||
|
||||
const shellExecutionConfig: ShellExecutionConfig = {
|
||||
sessionId: 'default',
|
||||
terminalWidth: 80,
|
||||
terminalHeight: 24,
|
||||
pager: 'cat',
|
||||
@@ -483,6 +484,7 @@ describe('ShellExecutionService', () => {
|
||||
ptyProcess: mockPtyProcess as any,
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
headlessTerminal: mockHeadlessTerminal as any,
|
||||
command: 'some-command',
|
||||
});
|
||||
});
|
||||
|
||||
@@ -753,6 +755,8 @@ describe('ShellExecutionService', () => {
|
||||
(ShellExecutionService as any).activePtys.clear();
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
(ShellExecutionService as any).activeChildProcesses.clear();
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
(ShellExecutionService as any).backgroundProcessHistory.clear();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
@@ -783,7 +787,11 @@ describe('ShellExecutionService', () => {
|
||||
]);
|
||||
|
||||
// Background the process
|
||||
ShellExecutionService.background(handle.pid!);
|
||||
ShellExecutionService.background(
|
||||
handle.pid!,
|
||||
'default',
|
||||
'long-running-pty',
|
||||
);
|
||||
|
||||
const result = await handle.result;
|
||||
expect(result.backgrounded).toBe(true);
|
||||
@@ -791,7 +799,7 @@ describe('ShellExecutionService', () => {
|
||||
|
||||
expect(mockMkdirSync).toHaveBeenCalledWith(
|
||||
expect.stringContaining('background-processes'),
|
||||
{ recursive: true },
|
||||
{ recursive: true, mode: 0o700 },
|
||||
);
|
||||
|
||||
// Verify initial output was written
|
||||
@@ -822,7 +830,11 @@ describe('ShellExecutionService', () => {
|
||||
mockBgChildProcess.stdout?.emit('data', Buffer.from('initial cp output'));
|
||||
await new Promise((resolve) => process.nextTick(resolve));
|
||||
|
||||
ShellExecutionService.background(handle.pid!);
|
||||
ShellExecutionService.background(
|
||||
handle.pid!,
|
||||
'default',
|
||||
'long-running-child',
|
||||
);
|
||||
|
||||
const result = await handle.result;
|
||||
expect(result.backgrounded).toBe(true);
|
||||
@@ -861,7 +873,11 @@ describe('ShellExecutionService', () => {
|
||||
});
|
||||
|
||||
// Background the process
|
||||
ShellExecutionService.background(handle.pid!);
|
||||
ShellExecutionService.background(
|
||||
handle.pid!,
|
||||
'default',
|
||||
'failing-log-setup',
|
||||
);
|
||||
|
||||
const result = await handle.result;
|
||||
expect(result.backgrounded).toBe(true);
|
||||
@@ -872,6 +888,89 @@ describe('ShellExecutionService', () => {
|
||||
|
||||
await ShellExecutionService.kill(handle.pid!);
|
||||
});
|
||||
|
||||
it('should track background process history', async () => {
|
||||
await simulateExecution(
|
||||
'history-test-cmd',
|
||||
async (pty) => {
|
||||
ShellExecutionService.background(
|
||||
pty.pid,
|
||||
'default',
|
||||
'history-test-cmd',
|
||||
);
|
||||
|
||||
const history =
|
||||
ShellExecutionService.listBackgroundProcesses('default');
|
||||
expect(history).toHaveLength(1);
|
||||
expect(history[0]).toEqual(
|
||||
expect.objectContaining({
|
||||
pid: pty.pid,
|
||||
command: 'history-test-cmd',
|
||||
status: 'running',
|
||||
}),
|
||||
);
|
||||
|
||||
// Simulate exit
|
||||
pty.onExit.mock.calls[0][0]({ exitCode: 0, signal: null });
|
||||
},
|
||||
{ ...shellExecutionConfig, originalCommand: 'history-test-cmd' },
|
||||
);
|
||||
|
||||
const history = ShellExecutionService.listBackgroundProcesses('default');
|
||||
expect(history[0]).toEqual(
|
||||
expect.objectContaining({
|
||||
pid: mockPtyProcess.pid,
|
||||
command: 'history-test-cmd',
|
||||
status: 'exited',
|
||||
exitCode: 0,
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('should evict oldest process history when exceeding max size', () => {
|
||||
const MAX = 100;
|
||||
const history = new Map();
|
||||
for (let i = 1; i <= MAX; i++) {
|
||||
history.set(i, {
|
||||
command: `cmd-${i}`,
|
||||
status: 'running',
|
||||
startTime: Date.now(),
|
||||
});
|
||||
}
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
(ShellExecutionService as any).backgroundProcessHistory.set(
|
||||
'default',
|
||||
history,
|
||||
);
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
(ShellExecutionService as any).activeChildProcesses.set(101, {
|
||||
process: {},
|
||||
state: { output: '' },
|
||||
command: 'cmd-101',
|
||||
sessionId: 'default',
|
||||
});
|
||||
|
||||
ShellExecutionService.background(101, 'default', 'cmd-101');
|
||||
|
||||
const processes =
|
||||
ShellExecutionService.listBackgroundProcesses('default');
|
||||
expect(processes).toHaveLength(MAX);
|
||||
expect(processes.some((p) => p.pid === 1)).toBe(false);
|
||||
});
|
||||
|
||||
it('should throw error if sessionId is missing for background operations', () => {
|
||||
expect(() => ShellExecutionService.background(102)).toThrow(
|
||||
'Session ID is required for background operations',
|
||||
);
|
||||
});
|
||||
|
||||
it('should throw error if sessionId is missing for listBackgroundProcesses', () => {
|
||||
expect(() =>
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
ShellExecutionService.listBackgroundProcesses(undefined as any),
|
||||
).toThrow('Session ID is required');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Binary Output', () => {
|
||||
|
||||
@@ -103,6 +103,8 @@ export interface ShellExecutionConfig {
|
||||
maxSerializedLines?: number;
|
||||
sandboxConfig?: SandboxConfig;
|
||||
backgroundCompletionBehavior?: 'inject' | 'notify' | 'silent';
|
||||
originalCommand?: string;
|
||||
sessionId?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -114,6 +116,8 @@ interface ActivePty {
|
||||
ptyProcess: IPty;
|
||||
headlessTerminal: pkg.Terminal;
|
||||
maxSerializedLines?: number;
|
||||
command: string;
|
||||
sessionId?: string;
|
||||
}
|
||||
|
||||
interface ActiveChildProcess {
|
||||
@@ -124,6 +128,8 @@ interface ActiveChildProcess {
|
||||
sniffChunks: Buffer[];
|
||||
binaryBytesReceived: number;
|
||||
};
|
||||
command: string;
|
||||
sessionId?: string;
|
||||
}
|
||||
|
||||
const findLastContentLine = (
|
||||
@@ -230,11 +236,28 @@ const writeBufferToLogStream = (
|
||||
*
|
||||
*/
|
||||
|
||||
export type BackgroundProcess = {
|
||||
pid: number;
|
||||
command: string;
|
||||
status: 'running' | 'exited';
|
||||
exitCode?: number | null;
|
||||
signal?: number | null;
|
||||
};
|
||||
|
||||
export type BackgroundProcessRecord = Omit<BackgroundProcess, 'pid'> & {
|
||||
startTime: number;
|
||||
endTime?: number;
|
||||
};
|
||||
|
||||
export class ShellExecutionService {
|
||||
private static activePtys = new Map<number, ActivePty>();
|
||||
private static activeChildProcesses = new Map<number, ActiveChildProcess>();
|
||||
private static backgroundLogPids = new Set<number>();
|
||||
private static backgroundLogStreams = new Map<number, fs.WriteStream>();
|
||||
private static backgroundProcessHistory = new Map<
|
||||
string, // sessionId
|
||||
Map<number, BackgroundProcessRecord>
|
||||
>();
|
||||
|
||||
static getLogDir(): string {
|
||||
return path.join(Storage.getGlobalTempDir(), 'background-processes');
|
||||
@@ -519,10 +542,12 @@ export class ShellExecutionService {
|
||||
binaryBytesReceived: 0,
|
||||
};
|
||||
|
||||
if (child.pid) {
|
||||
if (child.pid !== undefined) {
|
||||
this.activeChildProcesses.set(child.pid, {
|
||||
process: child,
|
||||
state,
|
||||
command: shellExecutionConfig.originalCommand ?? commandToExecute,
|
||||
sessionId: shellExecutionConfig.sessionId,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -696,6 +721,17 @@ export class ShellExecutionService {
|
||||
exitCode,
|
||||
signal: exitSignal,
|
||||
};
|
||||
|
||||
const sessionId = shellExecutionConfig.sessionId ?? 'default';
|
||||
const history =
|
||||
ShellExecutionService.backgroundProcessHistory.get(sessionId);
|
||||
const historyItem = history?.get(pid);
|
||||
if (historyItem) {
|
||||
historyItem.status = 'exited';
|
||||
historyItem.exitCode = exitCode ?? undefined;
|
||||
historyItem.signal = exitSignal ?? undefined;
|
||||
historyItem.endTime = Date.now();
|
||||
}
|
||||
onOutputEvent(event);
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
||||
@@ -849,6 +885,8 @@ export class ShellExecutionService {
|
||||
ptyProcess,
|
||||
headlessTerminal,
|
||||
maxSerializedLines: shellExecutionConfig.maxSerializedLines,
|
||||
command: shellExecutionConfig.originalCommand ?? commandToExecute,
|
||||
sessionId: shellExecutionConfig.sessionId,
|
||||
});
|
||||
|
||||
const result = ExecutionLifecycleService.attachExecution(ptyPid, {
|
||||
@@ -1116,6 +1154,17 @@ export class ShellExecutionService {
|
||||
exitCode,
|
||||
signal: signal ?? null,
|
||||
};
|
||||
|
||||
const sessionId = shellExecutionConfig.sessionId ?? 'default';
|
||||
const history =
|
||||
ShellExecutionService.backgroundProcessHistory.get(sessionId);
|
||||
const historyItem = history?.get(ptyPid);
|
||||
if (historyItem) {
|
||||
historyItem.status = 'exited';
|
||||
historyItem.exitCode = exitCode;
|
||||
historyItem.signal = signal ?? null;
|
||||
historyItem.endTime = Date.now();
|
||||
}
|
||||
onOutputEvent(event);
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
||||
@@ -1269,16 +1318,57 @@ export class ShellExecutionService {
|
||||
*
|
||||
* @param pid The process ID of the target PTY.
|
||||
*/
|
||||
static background(pid: number): void {
|
||||
static background(pid: number, sessionId?: string, command?: string): void {
|
||||
const activePty = this.activePtys.get(pid);
|
||||
const activeChild = this.activeChildProcesses.get(pid);
|
||||
|
||||
const resolvedSessionId =
|
||||
sessionId ?? activePty?.sessionId ?? activeChild?.sessionId;
|
||||
const resolvedCommand =
|
||||
command ??
|
||||
activePty?.command ??
|
||||
activeChild?.command ??
|
||||
'unknown command';
|
||||
|
||||
if (!resolvedSessionId) {
|
||||
throw new Error('Session ID is required for background operations');
|
||||
}
|
||||
|
||||
const MAX_BACKGROUND_PROCESS_HISTORY_SIZE = 100;
|
||||
const history =
|
||||
this.backgroundProcessHistory.get(resolvedSessionId) ??
|
||||
new Map<
|
||||
number,
|
||||
{
|
||||
command: string;
|
||||
status: 'running' | 'exited';
|
||||
exitCode?: number | null;
|
||||
signal?: number | null;
|
||||
startTime: number;
|
||||
endTime?: number;
|
||||
}
|
||||
>();
|
||||
|
||||
if (history.size >= MAX_BACKGROUND_PROCESS_HISTORY_SIZE) {
|
||||
const oldestPid = history.keys().next().value;
|
||||
if (oldestPid !== undefined) {
|
||||
history.delete(oldestPid);
|
||||
}
|
||||
}
|
||||
|
||||
history.set(pid, {
|
||||
command: resolvedCommand,
|
||||
status: 'running',
|
||||
startTime: Date.now(),
|
||||
});
|
||||
this.backgroundProcessHistory.set(resolvedSessionId, history);
|
||||
|
||||
// Set up background logging
|
||||
const logPath = this.getLogFilePath(pid);
|
||||
const logDir = this.getLogDir();
|
||||
try {
|
||||
mkdirSync(logDir, { recursive: true });
|
||||
const stream = fs.createWriteStream(logPath, { flags: 'w' });
|
||||
mkdirSync(logDir, { recursive: true, mode: 0o700 });
|
||||
const stream = fs.createWriteStream(logPath, { flags: 'wx' });
|
||||
stream.on('error', (err) => {
|
||||
debugLogger.warn('Background log stream error:', err);
|
||||
});
|
||||
@@ -1391,4 +1481,20 @@ export class ShellExecutionService {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static listBackgroundProcesses(sessionId: string): BackgroundProcess[] {
|
||||
if (!sessionId) {
|
||||
throw new Error('Session ID is required');
|
||||
}
|
||||
const history = this.backgroundProcessHistory.get(sessionId);
|
||||
if (!history) return [];
|
||||
|
||||
return Array.from(history.entries()).map(([pid, info]) => ({
|
||||
pid,
|
||||
command: info.command,
|
||||
status: info.status,
|
||||
exitCode: info.exitCode,
|
||||
signal: info.signal,
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user