Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

trimHistory is quadratic and blocks the main process on long OpenAI-compatible chats

Closed Beginner friendly
#171 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
78/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
typescript
Domain
performance

Research direction

Start with src/main/llm/openai.ts, especially trimHistory at lines 225–242 and its call from runTurn at line 162. Check the existing token-estimation helpers and tests, then add a time-bounded test with 5,000 messages. Done when trimming avoids repeatedly processing the remaining history and the test confirms it completes within a reasonable bound.

Written by the indexing model from the issue text.

Description

bug performance priority: medium severity: medium

Found in the 2026-10-07 code review and confirmed: the real trimHistory took 2,388 ms for one call on 3,001 messages (about 4.5 MB). Doubling the history roughly quadruples the time.

Problem

trimHistory (src/main/llm/openai.ts:225-242, called from runTurn at :162) moves start forward one message at a time. Each step calls estimateTokens([first, ...rest.slice(start)]), which JSON-stringifies everything that remains. That is O(n²), run synchronously on the main process before every request once the history passes 100k tokens.

Impact

Long sessions on Chat Completions endpoints freeze the UI, IPC and streaming for seconds on every turn.

Fix

Measure each message once:

const sizes = rest.map((m) => estimateChars(m));
let remaining = estimateChars(first) + sizes.reduce((a, b) => a + b, 0);
let start = 0;
while (start < lastGroup && Math.ceil(remaining / 4) > MAX_HISTORY_TOKENS) {
  remaining -= sizes[start++]!;
  while (start < lastGroup && rest[start]?.role === 'tool') remaining -= sizes[start++]!;
}

Add a time-bounded test with 5,000 messages.

Dominant language
TypeScript
Stars
2
Forks
2
Avg merge
5h 28m
Merged PRs (30d)
24

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from PierrunoYT/patch

All issues in PierrunoYT/patch

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.