@pentoshi/clai 3.8.39 → 3.8.41

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.
@@ -15,7 +15,7 @@ import { availableToolNames, normalizeToolCall, runToolCall, BATCH_SAFE_TOOLS, }
15
15
  import { getToolDefinitions, getCompactToolDefinitions, PLAN_TOOL_NAMES, } from "../tools/definitions.js";
16
16
  import { appendAssistantWithTools, ensureUniqueToolCallIds, appendToolResult, assertValidToolProtocol, fillMissingToolResults, repairToolProtocol, } from "./tool-history.js";
17
17
  import { formatViewportHint, registerViewport } from "../ui/output-pane.js";
18
- import { compactMessagesWithSummary, estimateMessagesTokens, shouldApplyAutoCompact, COMPACTION_MEMORY_PREFIX, PLAN_IMPLEMENT_MEMORY_PREFIX, isCompactionMemoryMessage, } from "./context-manager.js";
18
+ import { compactMessagesWithSummary, shouldApplyAutoCompact, COMPACTION_MEMORY_PREFIX, PLAN_IMPLEMENT_MEMORY_PREFIX, isCompactionMemoryMessage, } from "./context-manager.js";
19
19
  import { buildContextBreakdown, contextBreakdownAuditPayload, } from "./context-breakdown.js";
20
20
  import { autoCompactTriggerTokens, dedupeToolContextOutput, freeTierGuardNotices, getReliabilityPolicy, resolveStepMaxTokens, } from "./reliability-policy.js";
21
21
  import { auditLog } from "../store/logs.js";
@@ -2010,8 +2010,19 @@ export async function runAgentTurn(prompt, options = {}) {
2010
2010
  });
2011
2011
  return response.text;
2012
2012
  };
2013
+ /**
2014
+ * Estimate the complete next model request, including attached native-tool
2015
+ * schemas. This must match the request-context accounting used by the UI
2016
+ * and audit trail: large schemas can otherwise push an actual request past
2017
+ * the compaction threshold while message-only accounting says it is safe.
2018
+ */
2019
+ const estimateNextRequestTokens = (contextMessages) => {
2020
+ const { native } = resolveNativeTools(provider, model);
2021
+ const nextTools = selectToolDefs(native, useCompactSystemPrompt);
2022
+ return buildContextBreakdown(contextMessages, nextTools).estimatedTotalTokens;
2023
+ };
2013
2024
  async function maybeAutoCompact(reason, force = false) {
2014
- const beforeTokens = estimateMessagesTokens(messages);
2025
+ const beforeTokens = estimateNextRequestTokens(messages);
2015
2026
  // E1: soft early compact (default 70k) while hard ceiling remains 100k.
2016
2027
  const compactTrigger = autoCompactTriggerTokens();
2017
2028
  if (!force && beforeTokens < compactTrigger)
@@ -2037,8 +2048,8 @@ export async function runAgentTurn(prompt, options = {}) {
2037
2048
  }
2038
2049
  messages.splice(0, messages.length, ...result.messages);
2039
2050
  loopGuard.resetReadOnly();
2040
- // Token stats BEFORE plan re-injection so the reduction is accurate.
2041
- const compactedTokens = estimateMessagesTokens(messages);
2051
+ // Token stats use the same complete request estimate as the trigger.
2052
+ const compactedTokens = estimateNextRequestTokens(messages);
2042
2053
  // Re-inject the live plan so the model keeps full plan awareness even
2043
2054
  // after older turns (which carried the plan context) were summarized.
2044
2055
  const livePlan = await loadPlan(session.sessionId).catch(() => undefined);
@@ -2051,8 +2062,8 @@ export async function runAgentTurn(prompt, options = {}) {
2051
2062
  // Re-inject live SESSION STATE after compaction (older flags survive).
2052
2063
  refreshSessionState(livePlan);
2053
2064
  lastCompactionMsgCount = messages.length;
2054
- // Final count the model actually receives (may include re-injected plan).
2055
- const afterTokens = estimateMessagesTokens(messages);
2065
+ // Final request count the model receives (may include re-injected plan).
2066
+ const afterTokens = estimateNextRequestTokens(messages);
2056
2067
  await auditLog("agent.compact", {
2057
2068
  newLength: messages.length,
2058
2069
  estimatedTokens: afterTokens,