(messages: Message[])
| 1279 | } |
| 1280 | |
| 1281 | function getAutoModeAttachmentTurnCount(messages: Message[]): { |
| 1282 | turnCount: number |
| 1283 | foundAutoModeAttachment: boolean |
| 1284 | } { |
| 1285 | let turnsSinceLastAttachment = 0 |
| 1286 | let foundAutoModeAttachment = false |
| 1287 | |
| 1288 | // Iterate backwards to find most recent auto_mode attachment. |
| 1289 | // Count HUMAN turns (non-meta, non-tool-result user messages), not assistant |
| 1290 | // messages — the tool loop in query.ts calls getAttachmentMessages on every |
| 1291 | // tool round, so a single human turn with 100 tool calls would fire ~20 |
| 1292 | // reminders if we counted assistant messages. Auto mode's target use case is |
| 1293 | // long agentic sessions, where this accumulated 60-105× per session. |
| 1294 | for (let i = messages.length - 1; i >= 0; i--) { |
| 1295 | const message = messages[i] |
| 1296 | |
| 1297 | if ( |
| 1298 | message?.type === 'user' && |
| 1299 | !message.isMeta && |
| 1300 | !hasToolResultContent(message.message.content) |
| 1301 | ) { |
| 1302 | turnsSinceLastAttachment++ |
| 1303 | } else if ( |
| 1304 | message?.type === 'attachment' && |
| 1305 | message.attachment.type === 'auto_mode' |
| 1306 | ) { |
| 1307 | foundAutoModeAttachment = true |
| 1308 | break |
| 1309 | } else if ( |
| 1310 | message?.type === 'attachment' && |
| 1311 | message.attachment.type === 'auto_mode_exit' |
| 1312 | ) { |
| 1313 | // Exit resets the throttle — treat as if no prior attachment exists |
| 1314 | break |
| 1315 | } |
| 1316 | } |
| 1317 | |
| 1318 | return { turnCount: turnsSinceLastAttachment, foundAutoModeAttachment } |
| 1319 | } |
| 1320 | |
| 1321 | /** |
| 1322 | * Count auto_mode attachments since the last auto_mode_exit (or from start if no exit). |
no test coverage detected