clodds 1.6.2 → 1.6.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -16336,9 +16336,14 @@ async function createAgentManager(config, feeds, db, sessionManager, sendMessage
16336
16336
  'claude-3-opus-20240229': 200000,
16337
16337
  };
16338
16338
  const modelContextWindow = MODEL_CONTEXT_WINDOWS[modelId] || 200000;
16339
+ // Estimate fixed overhead: tool definitions + system prompt (these don't change during conversation)
16340
+ const toolsTokenEstimate = (0, context_1.estimateTokens)(JSON.stringify(tools), modelId);
16341
+ const systemTokenEstimate = (0, context_1.estimateTokens)(finalSystemPrompt, modelId);
16342
+ // Reserve enough for tools + system prompt + response buffer
16343
+ const reserveForFixed = toolsTokenEstimate + systemTokenEstimate + 4096;
16339
16344
  const contextConfig = {
16340
16345
  maxTokens: modelContextWindow,
16341
- reserveTokens: 4096,
16346
+ reserveTokens: reserveForFixed,
16342
16347
  compactThreshold: 0.85,
16343
16348
  minMessagesAfterCompact: 6,
16344
16349
  summarizer,
@@ -16349,9 +16354,7 @@ async function createAgentManager(config, feeds, db, sessionManager, sendMessage
16349
16354
  similarity: memory?.cosineSimilarity,
16350
16355
  };
16351
16356
  const contextManager = (0, context_1.createContextManager)(contextConfig, memory);
16352
- const effectiveMaxTokens = (contextConfig.maxTokens ?? 128000) - (contextConfig.reserveTokens ?? 4096);
16353
- // Estimate tool definitions once (they don't change during the conversation)
16354
- const toolsTokenEstimate = (0, context_1.estimateTokens)(JSON.stringify(tools), modelId);
16357
+ const effectiveMaxTokens = (contextConfig.maxTokens ?? 128000) - reserveForFixed;
16355
16358
  const estimateSubmitTokens = () => {
16356
16359
  const system = (0, context_1.estimateTokens)(finalSystemPrompt, modelId);
16357
16360
  const msgs = messages.reduce((sum, m) => {
@@ -16368,10 +16371,9 @@ async function createAgentManager(config, feeds, db, sessionManager, sendMessage
16368
16371
  content,
16369
16372
  });
16370
16373
  }
16371
- // Add system prompt tokens
16372
- const systemTokens = (0, context_1.estimateTokens)(finalSystemPrompt, modelId);
16373
16374
  // Check if we need to compact before first API call
16374
- const guard = contextManager.checkGuard(systemTokens);
16375
+ // (tools + system prompt are already accounted for in reserveTokens)
16376
+ const guard = contextManager.checkGuard();
16375
16377
  if (guard.shouldCompact) {
16376
16378
  logger_1.logger.info({ percentUsed: guard.percentUsed }, 'Context approaching limit, compacting');
16377
16379
  // Trigger compaction:before hook
@@ -16413,6 +16415,11 @@ async function createAgentManager(config, feeds, db, sessionManager, sendMessage
16413
16415
  }
16414
16416
  const initialEstimate = estimateSubmitTokens();
16415
16417
  logger_1.logger.info({ tokens: initialEstimate, max: effectiveMaxTokens }, 'Token estimate before submit');
16418
+ // Safety: if still over limit after compaction, return a friendly error
16419
+ if (initialEstimate > effectiveMaxTokens * 1.1) {
16420
+ logger_1.logger.warn({ tokens: initialEstimate, max: effectiveMaxTokens }, 'Context exceeds limit even after compaction');
16421
+ return 'This conversation has gotten too long for me to process. Please start a new conversation and I\'ll be happy to help!';
16422
+ }
16416
16423
  let response = await createMessage({
16417
16424
  model: modelId,
16418
16425
  max_tokens: 1024,
@@ -16517,7 +16524,7 @@ async function createAgentManager(config, feeds, db, sessionManager, sendMessage
16517
16524
  content,
16518
16525
  });
16519
16526
  }
16520
- const loopGuard = contextManager.checkGuard(0);
16527
+ const loopGuard = contextManager.checkGuard();
16521
16528
  if (loopGuard.shouldCompact) {
16522
16529
  logger_1.logger.info({ percentUsed: loopGuard.percentUsed }, 'Compacting context during tool loop');
16523
16530
  const loopCompactResult = await contextManager.compact();
@@ -16535,6 +16542,11 @@ async function createAgentManager(config, feeds, db, sessionManager, sendMessage
16535
16542
  }
16536
16543
  const loopEstimate = estimateSubmitTokens();
16537
16544
  logger_1.logger.info({ tokens: loopEstimate, max: effectiveMaxTokens }, 'Token estimate before submit (tool loop)');
16545
+ // Safety: bail if over limit during tool loop
16546
+ if (loopEstimate > effectiveMaxTokens * 1.1) {
16547
+ logger_1.logger.warn({ tokens: loopEstimate, max: effectiveMaxTokens }, 'Context exceeds limit during tool loop');
16548
+ break;
16549
+ }
16538
16550
  response = await createMessage({
16539
16551
  model: modelId,
16540
16552
  max_tokens: 1024,