Fix agents.json race condition on concurrent starts

Re-read agents.json immediately before writing in startAgent to avoid
one start overwriting another's entry. Also clean stale sockets before
starting new agents.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-07 13:54:54 +00:00
parent 36af68da90
commit 7ead9c86c5
3 changed files with 10 additions and 3 deletions

View File

@@ -251,6 +251,9 @@ export async function startAgent(
mkdirSync(CONFIG.socketDir, { recursive: true });
mkdirSync(CONFIG.runsDir, { recursive: true });
// Clean stale socket from previous run
try { unlinkSync(socketPath); } catch {}
// Prepare rootfs
copyFileSync(AGENT_ROOTFS, rootfsPath);
injectAgentConfig(
@@ -321,8 +324,11 @@ export async function startAgent(
startedAt: new Date().toISOString(),
};
agents[name] = info;
saveAgents(agents);
// Re-read agents.json before writing to avoid race conditions
// (another startAgent may have written since we last read)
const currentAgents = loadAgents();
currentAgents[name] = info;
saveAgents(currentAgents);
log(`Agent "${name}" started: nick=${nick} ip=${ip}`);
return info;

View File

@@ -197,7 +197,7 @@ export async function runOverseer(config: OverseerConfig) {
for (const name of knownAgents) {
if (!currentNames.has(name)) {
log(`Agent "${name}" died, cleaned up.`);
bot.say("#agents", `Agent "${name}" has died and been cleaned up.`);
bot.say(config.channel, `Agent "${name}" has died and been cleaned up.`);
}
}
knownAgents = currentNames;