* Detects whether context pruning occurred by checking for: * 1. tag (context-pruner's output) * 2. Previous message(s) omitted due to length (trimMessagesToFitTokenLimit fallback) * 3. Significant message count reduction (>50% fewer messages than original)
( finalMessages: Message[], originalMessageCount: number, )
| 228 | * 3. Significant message count reduction (>50% fewer messages than original) |
| 229 | */ |
| 230 | function detectPruning( |
| 231 | finalMessages: Message[], |
| 232 | originalMessageCount: number, |
| 233 | ): { |
| 234 | wasPruned: boolean |
| 235 | hasSummary: boolean |
| 236 | hasTrimFallback: boolean |
| 237 | messageReduction: number |
| 238 | } { |
| 239 | const hasSummary = finalMessages.some((msg) => { |
| 240 | if (msg.role !== 'user' || !Array.isArray(msg.content)) return false |
| 241 | return msg.content.some( |
| 242 | (part) => |
| 243 | typeof part === 'object' && |
| 244 | 'type' in part && |
| 245 | part.type === 'text' && |
| 246 | typeof (part as any).text === 'string' && |
| 247 | (part as any).text.includes('<conversation_summary>'), |
| 248 | ) |
| 249 | }) |
| 250 | |
| 251 | const hasTrimFallback = finalMessages.some((msg) => { |
| 252 | if (!Array.isArray(msg.content)) return false |
| 253 | return msg.content.some( |
| 254 | (part) => |
| 255 | typeof part === 'object' && |
| 256 | 'type' in part && |
| 257 | part.type === 'text' && |
| 258 | typeof (part as any).text === 'string' && |
| 259 | (part as any).text.includes('Previous message(s) omitted'), |
| 260 | ) |
| 261 | }) |
| 262 | |
| 263 | // Message reduction: if fewer than 50% of original messages remain |
| 264 | const messageReduction = |
| 265 | originalMessageCount > 0 |
| 266 | ? 1 - finalMessages.length / originalMessageCount |
| 267 | : 0 |
| 268 | |
| 269 | const wasPruned = |
| 270 | hasSummary || hasTrimFallback || messageReduction > 0.5 |
| 271 | |
| 272 | return { wasPruned, hasSummary, hasTrimFallback, messageReduction } |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Verifies tool-call/tool-result pair integrity. |
no outgoing calls
no test coverage detected