MCPcopy Create free account
hub / github.com/Noumena-Network/code / truncateHeadForPTLRetry

Function truncateHeadForPTLRetry

src/services/compact/compact.ts:255–303  ·  view source on GitHub ↗
(
  messages: Message[],
  ptlResponse: AssistantMessage,
)

Source from the content-addressed store, hash-verified

253 * that wasn't migrated in bfdb472f's unification.
254 */
255export function truncateHeadForPTLRetry(
256 messages: Message[],
257 ptlResponse: AssistantMessage,
258): Message[] | null {
259 // Strip our own synthetic marker from a previous retry before grouping.
260 // Otherwise it becomes its own group 0 and the 20% fallback stalls
261 // (drops only the marker, re-adds it, zero progress on retry 2+).
262 const input =
263 messages[0]?.type === 'user' &&
264 messages[0].isMeta &&
265 messages[0].message.content === PTL_RETRY_MARKER
266 ? messages.slice(1)
267 : messages
268
269 const groups = groupMessagesByApiRound(input)
270 if (groups.length < 2) return null
271
272 const tokenGap = getPromptTooLongTokenGap(ptlResponse)
273 let dropCount: number
274 if (tokenGap !== undefined) {
275 let acc = 0
276 dropCount = 0
277 for (const g of groups) {
278 acc += roughTokenCountEstimationForMessages(g)
279 dropCount++
280 if (acc >= tokenGap) break
281 }
282 } else {
283 dropCount = Math.max(1, Math.floor(groups.length * 0.2))
284 }
285
286 // Keep at least one group so there's something to summarize.
287 dropCount = Math.min(dropCount, groups.length - 1)
288 if (dropCount < 1) return null
289
290 const sliced = groups.slice(dropCount).flat()
291 // groupMessagesByApiRound puts the preamble in group 0 and starts every
292 // subsequent group with an assistant message. Dropping group 0 leaves an
293 // assistant-first sequence which the API rejects (first message must be
294 // role=user). Prepend a synthetic user marker — ensureToolResultPairing
295 // already handles any orphaned tool_results this creates.
296 if (sliced[0]?.type === 'assistant') {
297 return [
298 createUserMessage({ content: PTL_RETRY_MARKER, isMeta: true }),
299 ...sliced,
300 ]
301 }
302 return sliced
303}
304
305export const ERROR_MESSAGE_PROMPT_TOO_LONG =
306 'Conversation too long. Press esc twice to go up a few messages and try again.'

Callers 2

compactConversationFunction · 0.85

Calls 5

groupMessagesByApiRoundFunction · 0.85
getPromptTooLongTokenGapFunction · 0.85
maxMethod · 0.80
createUserMessageFunction · 0.50

Tested by

no test coverage detected