From 033e476a6f26abbdc528420f5a72f597e81ebb0c Mon Sep 17 00:00:00 2001 From: hailin Date: Sat, 7 Feb 2026 08:29:36 -0800 Subject: [PATCH] fix(agents): wire up autoCompactIfNeeded to prevent token overflow The auto-compaction logic (threshold 80K tokens, summarize older messages via Haiku) existed but was never called in sendMessage flow. Now called after context injection, before agent loop. Co-Authored-By: Claude Opus 4.6 --- .../agents/coordinator/coordinator-agent.service.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/services/conversation-service/src/infrastructure/agents/coordinator/coordinator-agent.service.ts b/packages/services/conversation-service/src/infrastructure/agents/coordinator/coordinator-agent.service.ts index ea0de2f..c7b50ff 100644 --- a/packages/services/conversation-service/src/infrastructure/agents/coordinator/coordinator-agent.service.ts +++ b/packages/services/conversation-service/src/infrastructure/agents/coordinator/coordinator-agent.service.ts @@ -211,7 +211,7 @@ export class CoordinatorAgentService implements OnModuleInit { const systemPrompt = this.buildSystemPromptBlocks(); // 3. Inject dynamic context - const enrichedMessages = await this.contextInjector.inject( + let enrichedMessages = await this.contextInjector.inject( { conversationId: context.conversationId, userId: context.userId, @@ -227,6 +227,13 @@ export class CoordinatorAgentService implements OnModuleInit { messages, ); + // 3.5. Auto-compact if messages exceed token threshold + const { messages: compactedMessages, wasCompacted } = + await this.contextInjector.autoCompactIfNeeded(enrichedMessages, this.anthropicClient); + if (wasCompacted) { + enrichedMessages = compactedMessages; + } + // 4. Create abort controller const abortController = new AbortController();