---
number: 9964
title: >-
  fix(MemoryCore): Resolve local MLX n_ctx exhaustion by truncating tail-end
  session documents (#9921)
author: tobiu
state: MERGED
createdAt: '2026-04-13T11:43:19Z'
updatedAt: '2026-04-13T11:54:44Z'
closedAt: '2026-04-13T11:54:39Z'
mergedAt: '2026-04-13T11:54:39Z'
head: agent/9921-n_ctx-exhaustion
base: dev
url: 'https://github.com/neomjs/neo/pull/9964'
---
Resolves #9921. Implements strict text length truncation on Session Service session history aggregation to prevent raw context payload sizes from crashing local MLX OpenAiCompatible endpoints via n_ctx exhaustion limits. Includes Unit Testing coverage as specified by the testing skill guidelines.

## Comments

### `@tobiu` commented on 2026-04-13T11:45:26Z

**Input from gemini-exp-1206 (Antigravity):**

> ✦ # PR Review Summary
> 
> **Status:** Approved
> 
> Great job knocking this out! Implementing a hard constraint on the prompt envelope is the most robust, defensive way to avoid hitting local model context exhaustion limits while staying lightweight. I've reviewed the diff and it aligns cleanly with the Neo Agent OS architecture.
> 
> ---
> 
> ### 📊 Evaluation Metrics
> *   **`[ARCH_ALIGNMENT]`**: 100 - Perfect alignment with the Neo Memory Core architecture. It intercepts the prompt aggregation exactly where it needs to without mutating the underlying vector abstractions.
> *   **`[CONTENT_COMPLETENESS]`**: 90 - The inline comments clearly explain the purpose of the negative length slice as a preventative measure for `n_ctx`.
> *   **`[EXECUTION_QUALITY]`**: 100 - Clean execution. The unit test explicitly intercepts the mock's prompt generation to mathematically prove the length falls within safe boundaries, ensuring no slow live-model API calls block the test suite.
> *   **`[PRODUCTIVITY]`**: 100 - Successfully prevents MLX context crashes, which directly unblocks the RLAIF backend.
> *   **`[IMPACT]`**: 85 - High impact for system stability. Resolves an immediate infrastructure blocker.
> *   **`[COMPLEXITY]`**: 20 - Very targeted, focused implementation.
> *   **`[EFFORT_PROFILE]`**: Quick Win - High ROI/Low Complexity.
> 
> ---
> 
> ### 🕸️ Context & Graph Linking
> *   **Target Epic / Issue ID:** Resolves #9921
> *   **Related Graph Nodes:** `SessionService`
> 
> ---
> 
> ### 🧠 Graph Ingestion Notes
> 
> *   **`[KB_GAP]`**: N/A
> *   **`[TOOLING_GAP]`**: N/A
> *   **`[RETROSPECTIVE]`**: The decision to truncate strings by prioritizing the tail end `slice(-MAX_CONTENT_LENGTH)` is a significant strategic takeaway for conversational RLAIF extraction. Because agents resolve their tasks at the very end of a turn sequence, the chronological tail holds the highest summarization value. Always preserve the trailing edges when forced to truncate sequences.
> 
> ---
> 
> ### 📋 Required Actions
> 
> To proceed with merging, please address the following:
> 
> - [x] All PR components approved and ready for Squash Merge by the Commander.
> 
> Excellent work resolving the `n_ctx` constraints! Once this is merged, we can proceed to #9913.

---

