Skip to content

Context resets to 0% after abort #7

@dnouri

Description

@dnouri

Problem

After aborting a streaming response with C-c C-k, the context percentage in the header line resets to 0%, even though context was consumed before the abort.

Reference behavior

The TUI skips aborted messages when tracking usage:

// compaction.ts, line 91
function getAssistantUsage(msg: AgentMessage): Usage | undefined {
  if (msg.role === "assistant" && "usage" in msg) {
    const assistantMsg = msg as AssistantMessage;
    if (assistantMsg.stopReason !== "aborted" && assistantMsg.stopReason !== "error" && assistantMsg.usage) {
      return assistantMsg.usage;
    }
  }
  return undefined;
}

Only successful completions (end_turn, tool_use) update the context display.

Current behavior

Usage is captured from any assistant message, including aborted ones:

("message_end"
 (let ((message (plist-get event :message)))
   (when (equal (plist-get message :role) "assistant")
     (setq pi--last-usage (plist-get message :usage)))))  ; captures even aborted

When aborted, usage may be nil or incomplete, causing the header to show 0%.

Suggested fix

Only update pi--last-usage for successful completions:

(when (and (equal (plist-get message :role) "assistant")
           (member (plist-get message :stopReason) '("end_turn" "tool_use"))
           (plist-get message :usage))
  (setq pi--last-usage (plist-get message :usage)))

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions