Fix concurrent agent initialization in sessions

This commit is contained in:
Mohamed Boudra
2025-11-30 02:26:19 +00:00
parent 51d31bb76d
commit f38287ff4c
+7 -7
View File
@@ -73,6 +73,7 @@ type AgentMcpClientConfig = {
const execAsync = promisify(exec); const execAsync = promisify(exec);
const ACTIVE_TITLE_GENERATIONS = new Set<string>(); const ACTIVE_TITLE_GENERATIONS = new Set<string>();
const pendingAgentInitializations = new Map<string, Promise<ManagedAgent>>();
let restartRequested = false; let restartRequested = false;
const KNOWN_AGENT_PROVIDERS: AgentProvider[] = ["claude", "codex"]; const KNOWN_AGENT_PROVIDERS: AgentProvider[] = ["claude", "codex"];
const RESTART_EXIT_DELAY_MS = 250; const RESTART_EXIT_DELAY_MS = 250;
@@ -232,10 +233,6 @@ export class Session {
private readonly agentMcpConfig: AgentMcpClientConfig; private readonly agentMcpConfig: AgentMcpClientConfig;
private agentTitleCache: Map<string, string | null> = new Map(); private agentTitleCache: Map<string, string | null> = new Map();
private unsubscribeAgentEvents: (() => void) | null = null; private unsubscribeAgentEvents: (() => void) | null = null;
private pendingAgentInitializations: Map<
string,
Promise<ManagedAgent>
> = new Map();
constructor( constructor(
clientId: string, clientId: string,
@@ -582,7 +579,7 @@ export class Session {
return existing; return existing;
} }
const inflight = this.pendingAgentInitializations.get(agentId); const inflight = pendingAgentInitializations.get(agentId);
if (inflight) { if (inflight) {
return inflight; return inflight;
} }
@@ -610,12 +607,15 @@ export class Session {
return this.agentManager.getAgent(agentId) ?? snapshot; return this.agentManager.getAgent(agentId) ?? snapshot;
})(); })();
this.pendingAgentInitializations.set(agentId, initPromise); pendingAgentInitializations.set(agentId, initPromise);
try { try {
return await initPromise; return await initPromise;
} finally { } finally {
this.pendingAgentInitializations.delete(agentId); const current = pendingAgentInitializations.get(agentId);
if (current === initPromise) {
pendingAgentInitializations.delete(agentId);
}
} }
} }