diff --git a/src/agent/agent.js b/src/agent/agent.js index fe44a25..4581166 100644 --- a/src/agent/agent.js +++ b/src/agent/agent.js @@ -170,7 +170,7 @@ export class Agent { }); this.bot.on('end', (reason) => { console.warn('Bot disconnected! Killing agent process.', reason) - process.exit(1); + this.cleanKill('Bot disconnected! Killing agent process.'); }); this.bot.on('death', () => { this.coder.cancelResume(); @@ -178,7 +178,7 @@ export class Agent { }); this.bot.on('kicked', (reason) => { console.warn('Bot kicked!', reason); - process.exit(1); + this.cleanKill('Bot kicked! Killing agent process.'); }); this.bot.on('messagestr', async (message, _, jsonMsg) => { if (jsonMsg.translate && jsonMsg.translate.startsWith('death') && message.startsWith(this.name)) { @@ -215,4 +215,11 @@ export class Agent { isIdle() { return !this.coder.executing && !this.coder.generating; } + + cleanKill(msg='Killing agent process...') { + this.history.add('system', msg); + this.bot.chat('Goodbye world.') + this.history.save(); + process.exit(1); + } } diff --git a/src/agent/coder.js b/src/agent/coder.js index 7ba66e3..c26d4d0 100644 --- a/src/agent/coder.js +++ b/src/agent/coder.js @@ -240,7 +240,7 @@ export class Coder { console.log('waiting for code to finish executing...'); await new Promise(resolve => setTimeout(resolve, 1000)); if (Date.now() - start > 10 * 1000) { - process.exit(1); // force exit program after 10 seconds of failing to stop + this.agent.cleanKill('Code execution refused stop after 10 seconds. Killing process.'); } } } @@ -255,19 +255,8 @@ export class Coder { return setTimeout(async () => { console.warn(`Code execution timed out after ${TIMEOUT_MINS} minutes. Attempting force stop.`); this.timedout = true; - this.agent.bot.output += `\nAction performed for ${TIMEOUT_MINS} minutes and then timed out and stopped. You may want to continue or do something else.`; - this.stop(); // last attempt to stop - await new Promise(resolve => setTimeout(resolve, 5 * 1000)); // wait 5 seconds - if (this.executing) { - console.error(`Failed to stop. Killing process. Goodbye.`); - this.agent.bot.output += `\nForce stop failed! Process was killed and will be restarted. Goodbye world.`; - this.agent.bot.chat('Goodbye world.'); - let output = this.formatOutput(this.agent.bot); - this.agent.history.add('system', output); - this.agent.history.save(); - process.exit(1); // force exit program - } - console.log('Code execution stopped successfully.'); + this.agent.history.add('system', `Code execution timed out after ${TIMEOUT_MINS} minutes. Attempting force stop.`); + await this.stop(); // last attempt to stop }, TIMEOUT_MINS*60*1000); } } \ No newline at end of file diff --git a/src/agent/commands/actions.js b/src/agent/commands/actions.js index 12ccacb..53cfd6b 100644 --- a/src/agent/commands/actions.js +++ b/src/agent/commands/actions.js @@ -45,7 +45,7 @@ export const actionsList = [ description: 'Restart the agent process.', perform: async function (agent) { await agent.history.save(); - process.exit(1); + agent.cleanKill(); } }, { diff --git a/src/process/agent-process.js b/src/process/agent-process.js index 8d8383d..21b6c2c 100644 --- a/src/process/agent-process.js +++ b/src/process/agent-process.js @@ -25,7 +25,7 @@ export class AgentProcess { process.exit(1); } console.log('Restarting agent...'); - this.start(profile, true, 'Agent process restarted. Notify the user and decide what to do.'); + this.start(profile, true, 'Agent process restarted.'); last_restart = Date.now(); } });