@dyyz1993/pi-coding-agent 0.70.3 → 0.70.4

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.
@@ -2800,15 +2800,10 @@ export class AgentSession {
2800
2800
  const contextWindow = model.contextWindow ?? 0;
2801
2801
  if (contextWindow <= 0)
2802
2802
  return undefined;
2803
- // After compaction, the last assistant usage reflects pre-compaction context size.
2804
- // We can only trust usage from an assistant that responded after the latest compaction.
2805
- // If no such assistant exists, context token count is unknown until the next LLM response.
2806
2803
  const branchEntries = this.sessionManager.getBranch();
2807
2804
  const latestCompaction = getLatestCompactionEntry(branchEntries);
2808
2805
  if (latestCompaction) {
2809
- // Check if there's a valid assistant usage after the compaction boundary
2810
2806
  const compactionIndex = branchEntries.lastIndexOf(latestCompaction);
2811
- let hasPostCompactionUsage = false;
2812
2807
  for (let i = branchEntries.length - 1; i > compactionIndex; i--) {
2813
2808
  const entry = branchEntries[i];
2814
2809
  if (entry.type === "message" && entry.message.role === "assistant") {
@@ -2816,18 +2811,16 @@ export class AgentSession {
2816
2811
  if (assistant.stopReason !== "aborted" && assistant.stopReason !== "error") {
2817
2812
  const contextTokens = calculateContextTokens(assistant.usage);
2818
2813
  if (contextTokens > 0) {
2819
- hasPostCompactionUsage = true;
2814
+ const percent = (contextTokens / contextWindow) * 100;
2815
+ return { tokens: contextTokens, contextWindow, percent };
2820
2816
  }
2821
- break;
2822
2817
  }
2818
+ break;
2823
2819
  }
2824
2820
  }
2825
- if (!hasPostCompactionUsage) {
2826
- return { tokens: null, contextWindow, percent: null };
2827
- }
2828
2821
  }
2829
2822
  const estimate = estimateContextTokens(this.messages);
2830
- const percent = (estimate.tokens / contextWindow) * 100;
2823
+ const percent = contextWindow > 0 ? (estimate.tokens / contextWindow) * 100 : 0;
2831
2824
  return {
2832
2825
  tokens: estimate.tokens,
2833
2826
  contextWindow,