trimHistory is quadratic and blocks the main process on long OpenAI-compatible chats
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
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
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from PierrunoYT/patch
-
enhancement platform: windows priority: low severity: low
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
PierrunoYT/patch#198 ·
Maintainers usually reply within 1 day
-
priority: medium security severity: low
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
PierrunoYT/patch#66 ·
Maintainers usually reply within 1 day
-
bug platform: macos priority: low severity: low tests
Difficulty 3/5 1-2 days Newbie friendliness 56/100
PierrunoYT/patch#218 · 1 comment ·
Maintainers usually reply within 1 day
-
enhancement priority: low security severity: low
Difficulty 4/5 3-5 days Newbie friendliness 55/100
PierrunoYT/patch#211 · 2 comments ·
Maintainers usually reply within 1 day
-
enhancement platform: windows priority: low security severity: low
Difficulty 4/5 3-5 days Newbie friendliness 55/100
PierrunoYT/patch#207 ·
Maintainers usually reply within 1 day
All issues in PierrunoYT/patch
Similar issues
-
Flaky: mongodb-memory-server 'Port already in use' when another process starts a mongod concurrentlyOpenarea:testing bug effort:S priority:P2
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
Maintainers usually reply within 1 day
-
lens:agent lens:process process
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
thebristolsound/birdbrain#1772 ·
Maintainers usually reply within 1 day
-
bug priority:low ready-for-dev
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
Automattic/data-liberation-agent#685 ·
Maintainers usually reply within 1 day
-
Business
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
Maintainers usually reply within 1 day