Files
chenhg5-cc-connect/agent/pi
Han 8eab274958 feat(pi): add ContextUsageReporter for reply footer token stats (#1235)
* feat(pi): add ContextUsageReporter for reply footer token stats

Parse ~/.pi/agent/models.json at session start for per-model context
window data. Handle pi's agent_end event to extract assistant message
usage (input, output, cacheRead, cacheWrite) and populate ContextUsage,
enabling the engine's buildClaudeStatusLineFooter to render the CCD-style
token-usage footer for pi agent replies.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* fix(pi): address PR #1235 review — log levels, UsedTokens semantics, tests

- Log file-not-found for models.json at Info level (not Warn)
- Fix UsedTokens = input + cacheWrite + cacheRead (was incorrectly
  including output); TotalTokens = UsedTokens + output. Mirrors
  claudecode's per-sub-call pattern.
- Add comments for unpopulated BaselineTokens/ReasoningOutputTokens
  and session-lifetime modelsCW caching
- Add 11 unit tests covering loadModelsContextWindows, handleAgentEnd,
  GetContextUsage edge cases (nil, empty, fallback, copy semantics)

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* fix(pi): silence errcheck lint in new tests

Add _ = prefix to os.Setenv/os.Unsetenv in Cleanup closures,
and check os.WriteFile errors to satisfy errcheck linter.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-09 23:21:30 +08:00
..