@caupulican/pi-adaptative 0.81.17 → 0.81.19

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.
@@ -1613,6 +1613,7 @@ export class AgentSession {
1613
1613
  // accumulate until the interactive Node process hits the V8 heap limit.
1614
1614
  if (event.type === "message_end") {
1615
1615
  compactToolResultDetailsForRetention(event.message);
1616
+ let messagePersisted = false;
1616
1617
  // While a cheap routed turn is buffering, its messages are captured for later flush/discard
1617
1618
  // instead of persisted here (see ModelRouterController.captureSessionMessage).
1618
1619
  if (this._modelRouter.captureSessionMessage(event.message)) {
@@ -1622,18 +1623,23 @@ export class AgentSession {
1622
1623
  else if (event.message.role === "custom") {
1623
1624
  // Persist as CustomMessageEntry
1624
1625
  this.sessionManager.appendCustomMessageEntry(event.message.customType, event.message.content, event.message.display, event.message.details);
1626
+ messagePersisted = true;
1625
1627
  }
1626
1628
  else if (event.message.role === "user" ||
1627
1629
  event.message.role === "assistant" ||
1628
1630
  event.message.role === "toolResult") {
1629
1631
  // Regular LLM message - persist as SessionMessageEntry
1630
1632
  this.sessionManager.appendMessage(event.message);
1633
+ messagePersisted = true;
1631
1634
  }
1632
1635
  // Other message types (bashExecution, compactionSummary, branchSummary) are persisted elsewhere
1633
1636
  // Track assistant message for auto-compaction (checked on agent_end)
1634
1637
  if (event.message.role === "assistant") {
1635
1638
  this._lastAssistantMessage = event.message;
1636
1639
  const assistantMsg = event.message;
1640
+ if (messagePersisted) {
1641
+ this._pipeline.observeProviderUsage(this.agent.state.messages, assistantMsg);
1642
+ }
1637
1643
  if (assistantMsg.stopReason !== "error") {
1638
1644
  this._overflowRecoveryAttempted = false;
1639
1645
  }
@@ -3033,6 +3039,18 @@ export class AgentSession {
3033
3039
  }
3034
3040
  return false;
3035
3041
  }
3042
+ _measureLiveContextTokensForCompaction() {
3043
+ const estimatedTokens = this._pipeline.estimateCurrentContextTokens(this.agent.state.messages);
3044
+ const assistantMessage = findLastAssistantMessage(this.agent.state.messages);
3045
+ if (!assistantMessage || assistantMessage.stopReason === "error" || assistantMessage.stopReason === "aborted") {
3046
+ return estimatedTokens;
3047
+ }
3048
+ const compactionEntry = getLatestCompactionEntry(this.sessionManager.getBranch());
3049
+ if (compactionEntry && assistantMessage.timestamp <= new Date(compactionEntry.timestamp).getTime()) {
3050
+ return estimatedTokens;
3051
+ }
3052
+ return Math.max(calculateContextTokens(assistantMessage.usage), estimatedTokens);
3053
+ }
3036
3054
  /**
3037
3055
  * Internal: Run auto-compaction with events.
3038
3056
  */
@@ -3062,7 +3080,7 @@ export class AgentSession {
3062
3080
  const margin = Math.max(0, Math.floor(0.01 * contextWindow));
3063
3081
  const outcome = await runCompactionLoop({
3064
3082
  getBranch: () => this.sessionManager.getBranch(),
3065
- measureLiveTokens: () => this._pipeline.estimateCurrentContextTokens(this.agent.state.messages),
3083
+ measureLiveTokens: () => this._measureLiveContextTokensForCompaction(),
3066
3084
  shouldCompact: reason === "overflow"
3067
3085
  ? () => true
3068
3086
  : (tokens) => shouldCompact(tokens, contextWindow, settings, model.autoCompactionTriggerTokens),
@@ -3771,4 +3789,13 @@ export class AgentSession {
3771
3789
  return this._extensionRunner;
3772
3790
  }
3773
3791
  }
3792
+ function findLastAssistantMessage(messages) {
3793
+ for (let i = messages.length - 1; i >= 0; i--) {
3794
+ const message = messages[i];
3795
+ if (message.role === "assistant") {
3796
+ return message;
3797
+ }
3798
+ }
3799
+ return undefined;
3800
+ }
3774
3801
  //# sourceMappingURL=agent-session.js.map