estimateSessionBranchTokens function

int estimateSessionBranchTokens(
  1. List<SessionRecord> branch
)

Estimated context tokens for a walked session branch (root-first records), counting ONLY what Session.buildContextMessages would project into the model context: messages, custom messages, compaction and branch summaries. Ledger-only records (custom payloads like model_request_summary, model/label markers, …) never reach the context and count zero — a marathon tail full of giant model_request_summary snapshots does not inflate the estimate.

Backs the token-budgeted resume walk (issue #503): the CLI pages a windowed open back from the tail only until the resident records cover the model's context window, instead of always walking to the newest compaction boundary (or the whole file). Older history stays on disk and pages in lazily via the scrollback's loadOlder path.

Implementation

int estimateSessionBranchTokens(List<SessionRecord> branch) {
  var tokens = 0;
  for (final record in branch) {
    tokens += switch (record) {
      MessageRecord(:final message) => estimateTokens(message),
      CustomMessageRecord(:final content, :final timestamp) => estimateTokens(
        UserMessage(content: content, timestamp: timestamp),
      ),
      CompactionRecord(:final summary) =>
        (summary.length / _charsPerToken).ceil(),
      BranchSummaryRecord(:final summary) =>
        (summary.length / _charsPerToken).ceil(),
      _ => 0,
    };
  }
  return tokens;
}