iachat/enterprise/app
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
..
builders Merge branch main into chore/merge-upstream 2025-12-20 12:44:31 -03:00
controllers fix(captain-memory): always authorize index even when list is empty 2026-04-19 01:43:57 -03:00
dispatchers/enterprise feat(ee): Add Captain features (#10665) 2025-01-14 16:15:47 -08:00
drops feat: Add push/email notification support for SLA (#9140) 2024-03-29 20:27:21 +11:00
errors/captain/errors fix: Resolve erros de linting e conclui as correções de prompt customizado 2026-02-27 13:02:44 -03:00
fields fix: Render all account limit fields (#13435) 2026-02-04 20:21:07 +05:30
finders/enterprise feat: Conversation API to return applied_sla and sla_events (#9174) 2024-04-01 23:30:07 +05:30
helpers feat: langfuse logging improvements (#13534) 2026-02-17 13:30:04 +05:30
jobs feat(captain-memory): add HardDeleteExpiredJob with daily cron (LGPD) 2026-04-19 01:09:28 -03:00
listeners feat(captain-memory): enqueue extraction on conversation.resolved 2026-04-19 01:13:26 -03:00
mailers/enterprise/agent_notifications chore: fix sla email notifications (#9192) 2024-04-04 21:16:49 +05:30
models feat(captain): hierarchical model routing + conversation-level memory cache 2026-04-19 09:47:15 -03:00
policies feat(captain-memory): add ContactMemoryPolicy (Pundit) 2026-04-19 01:37:13 -03:00
presenters/enterprise/conversations feat: UI to show the SLA threshold in chat screen (#9146) 2024-04-04 15:46:46 +05:30
services feat(captain): rate limiting with runaway loop detection + bot_handoff 2026-04-19 11:16:54 -03:00
views feat(lifecycle): REST endpoint for lifecycle deliveries audit log 2026-04-15 10:29:24 -03:00