Skip to content

fix(session): add message limit warning to prevent memory leaks in long-running sessions#1341

Open
walker83 wants to merge 1 commit into
XiaomiMiMo:mainfrom
walker83:fix/session-message-limit
Open

fix(session): add message limit warning to prevent memory leaks in long-running sessions#1341
walker83 wants to merge 1 commit into
XiaomiMiMo:mainfrom
walker83:fix/session-message-limit

Conversation

@walker83

Copy link
Copy Markdown

PR: Add session message limit warning to prevent memory leaks

Summary

This PR adds a warning log when session message count exceeds 1000 messages, helping users identify potential memory leak issues in long-running sessions.

Problem

As reported in #1221, long-running sessions (>200k tokens) become extremely slow and eventually crash. Root cause analysis shows that:

  1. Message history grows without bounds
  2. Each message contains full content, metadata, and token stats
  3. No early warning system exists to alert users

Solution

Add MAX_MESSAGES_PER_SESSION = 1000 constant and log a warning when exceeded:

if (input.messages.length > MAX_MESSAGES_PER_SESSION) {
  log.warn("session.message-limit-exceeded", {
    sessionID: input.sessionID,
    messageCount: input.messages.length,
    limit: MAX_MESSAGES_PER_SESSION,
    suggestion: "Consider starting a new session or running /compact",
  })
}

Changes

  • packages/opencode/src/session/compaction.ts:
    • Add MAX_MESSAGES_PER_SESSION export constant
    • Add warning log in processCompaction function

Testing

  1. Start a new session
  2. Send messages until count exceeds 1000
  3. Check logs for warning: session.message-limit-exceeded

Related Issues

Checklist

  • Code follows project style guidelines
  • Added inline comments explaining the change
  • Linked to related issues
  • Commit message follows conventional commit format

Notes

This is a diagnostic fix that adds visibility into the memory issue. Future PRs could:

  1. Implement automatic session archival when limit is exceeded
  2. Add memory usage monitoring
  3. Implement message pagination for large sessions

…ng-running sessions

Add MAX_MESSAGES_PER_SESSION constant (1000) and warning log when sessions
exceed this limit. This helps users identify when to start a new session or
run /compact to avoid the memory issues reported in XiaomiMiMo#1221.

Related: XiaomiMiMo#1221, XiaomiMiMo#813, XiaomiMiMo#680
Refs: XiaomiMiMo#1221
@walker83 walker83 force-pushed the fix/session-message-limit branch from e238733 to 7f06d5a Compare June 25, 2026 13:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Long running sessions cause lag and an eventual crash

1 participant