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