Remove module-level SESSION_HISTORY Map from codex-mcp-agent.ts
Refactored history storage from module-level Map to instance-level persistedHistory field. This fixes: - Memory leak: sessions were never cleaned up from global Map - Shared state: unrelated sessions could collide - Restart handling: history now always loads from disk on resume Changes: - Removed SESSION_HISTORY Map (was at line 118) - Constructor now sets historyPending=true for resume instead of looking up from global Map - connect() always loads from disk when resuming - recordHistory() appends to this.persistedHistory instead of Map - flushPendingHistory() appends to this.persistedHistory instead of Map - loadPersistedHistoryFromDisk() no longer populates global Map 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.5
parent
f22f76c56b
commit
aea8a66112
@@ -115,8 +115,6 @@ const MODE_PRESETS: Record<
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
const SESSION_HISTORY = new Map<string, AgentTimelineItem[]>();
|
|
||||||
|
|
||||||
function createToolCallTimelineItem(
|
function createToolCallTimelineItem(
|
||||||
data: Omit<ToolCallTimelineItem, "type">
|
data: Omit<ToolCallTimelineItem, "type">
|
||||||
): AgentTimelineItem {
|
): AgentTimelineItem {
|
||||||
@@ -2563,11 +2561,8 @@ class CodexMcpAgentSession implements AgentSession {
|
|||||||
this.conversationId = this.sessionId;
|
this.conversationId = this.sessionId;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Try in-memory history first (for same-process resume)
|
// Mark history as pending; actual loading happens in connect() from disk
|
||||||
const history = this.sessionId ? SESSION_HISTORY.get(this.sessionId) : undefined;
|
this.historyPending = true;
|
||||||
this.persistedHistory = history ? [...history] : [];
|
|
||||||
this.historyPending = this.persistedHistory.length > 0;
|
|
||||||
// Note: If SESSION_HISTORY is empty (daemon restarted), we load from disk in connect()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.client = new Client(
|
this.client = new Client(
|
||||||
@@ -2608,9 +2603,8 @@ class CodexMcpAgentSession implements AgentSession {
|
|||||||
async connect(): Promise<void> {
|
async connect(): Promise<void> {
|
||||||
if (this.connected) return;
|
if (this.connected) return;
|
||||||
|
|
||||||
// If resuming with no in-memory history, load from rollout file on disk
|
// Load history from disk when resuming a session
|
||||||
// This handles the case where the daemon restarted and SESSION_HISTORY was lost
|
if (this.resumeHandle && this.sessionId) {
|
||||||
if (this.resumeHandle && this.sessionId && this.persistedHistory.length === 0) {
|
|
||||||
await this.loadPersistedHistoryFromDisk();
|
await this.loadPersistedHistoryFromDisk();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2641,8 +2635,6 @@ class CodexMcpAgentSession implements AgentSession {
|
|||||||
if (timeline.length > 0) {
|
if (timeline.length > 0) {
|
||||||
this.persistedHistory = timeline;
|
this.persistedHistory = timeline;
|
||||||
this.historyPending = true;
|
this.historyPending = true;
|
||||||
// Also populate SESSION_HISTORY so future in-process resumes work
|
|
||||||
SESSION_HISTORY.set(this.sessionId, [...timeline]);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3154,9 +3146,7 @@ class CodexMcpAgentSession implements AgentSession {
|
|||||||
|
|
||||||
private recordHistory(item: AgentTimelineItem): void {
|
private recordHistory(item: AgentTimelineItem): void {
|
||||||
if (this.sessionId) {
|
if (this.sessionId) {
|
||||||
const history = SESSION_HISTORY.get(this.sessionId) || [];
|
this.persistedHistory.push(item);
|
||||||
history.push(item);
|
|
||||||
SESSION_HISTORY.set(this.sessionId, history);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.pendingHistory.push(item);
|
this.pendingHistory.push(item);
|
||||||
@@ -3166,9 +3156,7 @@ class CodexMcpAgentSession implements AgentSession {
|
|||||||
if (!this.sessionId || this.pendingHistory.length === 0) {
|
if (!this.sessionId || this.pendingHistory.length === 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const history = SESSION_HISTORY.get(this.sessionId) || [];
|
this.persistedHistory.push(...this.pendingHistory);
|
||||||
history.push(...this.pendingHistory);
|
|
||||||
SESSION_HISTORY.set(this.sessionId, history);
|
|
||||||
this.pendingHistory = [];
|
this.pendingHistory = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user