Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:777genius Agent teams ai processSessionContextWithPhases

From Leeroopedia
Knowledge Sources
Domains Session_Analysis, Token_Tracking
Last Updated 2026-10-22 00:00 GMT

Overview

Concrete tool for tracking token consumption across 6 context categories with compaction phase awareness, provided by the Agent Teams application.

Description

The processSessionContextWithPhases() function processes all chat items in a session and computes per-turn `ContextStats` with phase information. It tracks 6 categories of context injection (claude-md, mentioned-file, tool-output, thinking-text, team-coordination, user-message) and resets accumulated injections on compaction events.

Key behaviors:

  • 6-category tracking: Each turn's context is attributed to claude-md, mentioned-file, tool-output, thinking-text, team-coordination, or user-message categories.
  • Accumulated injections: Context injections accumulate across turns within a phase; each AI group's stats reflect all accumulated injections up to that point.
  • Phase tracking: `ContextPhaseInfo` records phase number, boundaries (first/last AI group per phase), compact group, and token deltas per phase.
  • Compaction reset: When a compact chunk is encountered, accumulated injections are reset and a new phase starts with an incremented phase number.
  • User→AI pairing: User groups are tracked (`previousUserGroup`) for pairing with subsequent AI groups.
  • CLAUDE.md detection: Uses `claudeMdTracker` and directory token data to identify CLAUDE.md file injections.
  • AI group enhancement: Uses `aiGroupEnhancer` to enrich AI groups with context injection details.

Usage

  • Context badge: The per-turn `ContextStats` feeds the `ContextBadge` popover showing what consumed tokens each turn.
  • Phase display: `ContextPhaseInfo` enables phase boundary visualization in the session timeline.
  • Session panel: The full `statsMap` feeds the `SessionContextPanel` for a complete context overview.

Code Reference

Source Location

  • Repository: agent-teams
  • File: src/renderer/utils/contextTracker.ts

Signature

export function processSessionContextWithPhases(
  items: ChatItem[],
  projectRoot: string,
  claudeMdTokenData?: Record<string, ClaudeMdFileInfo>,
  mentionedFileTokenData?: Map<string, MentionedFileInfo>,
  directoryTokenData?: Record<string, ClaudeMdFileInfo>
): {
  statsMap: Map<string, ContextStats>;
  phaseInfo: ContextPhaseInfo;
};

Import

import { processSessionContextWithPhases } from '@renderer/utils/contextTracker';

I/O Contract

Inputs

Name Type Required Description
items ChatItem[] Yes Chat timeline items (user, AI, compact)
projectRoot string Yes Project path for CLAUDE.md detection
claudeMdTokenData Record<string, ClaudeMdFileInfo> No CLAUDE.md file token info
mentionedFileTokenData Map<string, MentionedFileInfo> No @-mentioned file token info
directoryTokenData Record<string, ClaudeMdFileInfo> No Directory-level CLAUDE.md info

Outputs

Name Type Description
return.statsMap Map<string, ContextStats> Per-AI-group context stats
return.phaseInfo ContextPhaseInfo Phase boundaries and token deltas
ContextStats.tokensByCategory TokensByCategory Per-category token counts for 6 categories

Usage Examples

Computing Context Stats

import { processSessionContextWithPhases } from '@renderer/utils/contextTracker';

const { statsMap, phaseInfo } = processSessionContextWithPhases(
  chatItems,
  '/path/to/project',
  claudeMdTokenData,
  mentionedFileTokenData
);

// Access per-turn stats
for (const [groupId, stats] of statsMap) {
  console.log(`Group ${groupId}:`);
  console.log(`  claude-md: ${stats.tokensByCategory['claude-md']} tokens`);
  console.log(`  tool-output: ${stats.tokensByCategory['tool-output']} tokens`);
  console.log(`  thinking-text: ${stats.tokensByCategory['thinking-text']} tokens`);
}

// Phase info
console.log(`${phaseInfo.phases.length} phases (compaction events)`);

Feeding UI Components

// Pass statsMap to ContextBadge for per-turn popover
function TimelineItem({ item, groupId }) {
  const stats = statsMap.get(groupId);
  return (
    <div>
      {item.content}
      {stats && <ContextBadge stats={stats} phaseInfo={phaseInfo} />}
    </div>
  );
}

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment