iachat/enterprise/app/services
Rodribm10 f3f8a8d5c1 feat(captain): rate limiting with runaway loop detection + bot_handoff
Três camadas de proteção contra runaway token burn no AgentRunnerService:

1. MAX_TURNS_PER_MESSAGE = 15
   Cap dentro de uma única chamada run(). Já estava aplicado;
   agora extraído como constante nomeada.

2. MAX_TURNS_PER_CONVERSATION = 30
   Cap ao longo da vida da conversa. Contador em
   conversation.custom_attributes['captain_turn_count']. Ao atingir,
   dispara bot_handoff automático e responde com mensagem de
   transferência pra humano.

3. TOOL_LOOP_THRESHOLD = 3
   Detecta a mesma (tool_name, args) invocada 3+ vezes no resultado
   de um único run (sintoma do loop faq_lookup que queimou tokens
   em 2026-04-19). Ao detectar: dispara bot_handoff e aborta o turno.

trigger_bot_handoff! aciona conversation.bot_handoff! quando
disponível, removendo a conversa do pipeline automático.

Motivação: dois incidentes reais de queima de crédito OpenAI em
2026-04-19. Ver memory/feedback_never_touch_captain_without_safety_caps.md
pras invariantes completas.

Tests atualizados: mock_result agora stuba :messages (usado pelo
novo tool_loop_detected?) e max_turns esperado é 15.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-19 11:16:54 -03:00
..
captain feat(captain): rate limiting with runaway loop detection + bot_handoff 2026-04-19 11:16:54 -03:00
cloudflare Merge branch 'main' into chore/merge-upstream-4.5.0 2025-08-20 11:20:31 -03:00
companies Chore/merge upstream 4.8.0 (#150) 2025-11-19 16:25:58 -03:00
contacts Chore/merge upstream 4.8.0 (#150) 2025-11-19 16:25:58 -03:00
enterprise feat(ai): implement dynamic debounce with sidekiq and concurrency locking with redis 2026-03-01 02:04:55 -03:00
internal Merge branch main into chore/merge-upstream 2025-12-20 12:44:31 -03:00
llm feat: add global config for captain settings (#13141) 2026-01-12 19:54:19 +05:30
messages fix(messages): reduce audio transcription 400 retry noise (#13487) 2026-02-17 13:25:13 +05:30
sla chore: fix sla email notifications (#9192) 2024-04-04 21:16:49 +05:30
twilio chore: apply rubocop 2025-09-19 19:44:02 -03:00
voice chore: fix linting 2025-12-20 12:55:27 -03:00
page_crawler_service.rb feat: Response Bot using GPT and Webpage Sources (#7518) 2023-07-21 18:11:51 +03:00