Gemini CLI PR swaps unshift for push, fixing O(n²) cost in history truncation
Kaushik2210 · ghdev · 2026-09-26
A community PR on google-gemini/gemini-cli optimizes array reconstruction in truncateHistoryToBudget().
- Problem: The function walks messages newest-to-oldest to prioritize recent tool responses, then restores chronological order via per-part unshift() calls. Since front-insertion is O(n), total cost was O(H² + ΣPᵢ²), paid even for messages later summarized away.
- Fix: Append with push(), then reverse each parts array and the whole history once. Traversal order and all truncation decisions are unchanged — a behavior-preserving refactor.
- Tests: A new regression test locks in message/part ordering without truncation; 37/37 tests pass, eslint and tsc clean.
More from coding & agent
- Building a Semantic and Knowledge Layer on BigQuery and Exposing It to Agents — batmanparam · 2026-09-27
- OpenAI selects 35 builders for first Codex Physical Builds hardware cohort — OpenAIDevs · 2026-09-27
- Multi-Agent Bot Pipelines Keep Stalling: Direct-to-Harness Still Beats Bot Teams — Old_Grapefruit8774 · 2026-09-27
- Claude Opus 5.5 Chains Blender, Image-Gen and three.js Into Browser-Running 3D — burny_tech · 2026-09-27
- Claude kills plan mode the same day Google announces launching it — dotey · 2026-09-27
- Gary Marcus amplifies warning: large teams using AI agents likely have unknown security incidents — GaryMarcus · 2026-09-27