Skip to content

feat(context): per-session context token tree across CLI, TUI, and dashboard#592

Merged
iamtoruk merged 5 commits into
mainfrom
feat/context-tree
Jul 2, 2026
Merged

feat(context): per-session context token tree across CLI, TUI, and dashboard#592
iamtoruk merged 5 commits into
mainfrom
feat/context-tree

Conversation

@iamtoruk

@iamtoruk iamtoruk commented Jul 2, 2026

Copy link
Copy Markdown
Member

Replaces #591, which was merged while still based on its stacked branch instead of main, so the feature never reached main. Identical content, rebased onto main (with the dash changes reconciled against the new mobile-responsive header and sidebar).

What

New codeburn context command plus a Context page in the web dashboard: for any Claude Code or Codex session, see what is actually filling the context window, broken down by role, block type, and tool, reconstructed from the session files on disk.

  • codeburn context opens an interactive TUI: arrows to move, enter expands a session's tree inline, tab switches Claude Code / Codex, f toggles live window vs full history.
  • codeburn context <id> [--provider codex] [--full] [--json] prints the same tree statically for scripts, and --list [--json] enumerates recent sessions with their titles.
  • The dashboard gets a Context page in the navbar: provider picker, recent sessions, and click-to-expand details with metric chips, a context-vs-window bar, and the token tree.

How it works

  • Claude Code transcripts are re-read per message block. Thinking text is stripped from transcripts, so reasoning is derived per message as output_tokens minus the estimated visible output. Compaction boundaries (including the preserved segment carried across a compaction) split the live window from full history.
  • Codex rollouts feed the tree from response items; token_count events supply exact reasoning totals and the real model_context_window, and compacted entries seed the new window from their replacement_history.
  • Block numbers are chars/4 estimates and images are priced from sniffed PNG/JPEG dimensions, but the headline "context (exact)" figure comes from API usage, so estimates always have a hard anchor. The gap between the two is shown as derived system prompt and tool overhead.
  • Context windows are detected per model (opus-4-8 and [1m] ids map to 1M). When a Codex rollout does not state its window, the percentage is omitted rather than guessed.

Notes

  • Everything stays local. The tree endpoint strips file paths and serves precomputed rows, so the CLI, TUI, and dashboard render identical trees.
  • Session discovery honors CLAUDE_CONFIG_DIRS, CLAUDE_CONFIG_DIR, and CODEX_HOME, stats files in parallel, and resolves ids without scanning every session file. The server caches built trees per file version with LRU eviction.

iamtoruk added 5 commits July 2, 2026 04:54
…n tree

Reconstructs a zaly-style context breakdown for a Claude Code session
from its transcript: messages and tokens by role, block type, and tool,
split into full-session history vs the live window since the last
compaction (following preservedSegment.headUuid).

Block tokens are chars/4 estimates, images estimated from sniffed PNG or
JPEG dimensions, reasoning derived per message from output_tokens minus
visible output since transcripts strip thinking text. The exact context
size comes from the last assistant message's API usage, and the gap to
the estimate is shown as derived system prompt and tool overhead.
Reads the latest ai-title entry (summary entry as fallback for older
sessions) from one tail and one head chunk of each transcript, so the
list stays fast on 100MB files.
…trees

Adds a Context page to the web dashboard: a navbar toggle, a provider
picker (Claude Code / Codex), the 15 most recent sessions with titles,
and per-session expandable details showing the context token tree, the
exact live context vs window, and a live/full-history scope toggle.

Server side adds /api/context/sessions and /api/context/tree with an
mtime-keyed tree cache, plus a Codex rollout builder: response items
feed the tree, compacted entries (with replacement_history) split live
window from full history, reasoning comes exact from cumulative
token_count totals, and model_context_window gives the real window.
codeburn context with no arguments now opens an ink TUI: arrow keys to
move, enter to expand a session's token tree inline (with a spinner
while the transcript reads), tab to switch between Claude Code and
Codex, f to toggle live window vs full history, q to quit. Flag and id
forms keep the plain output for scripts, and non-TTY runs fall back to
the static list. Tree rows are shared with the static renderer via
snapshotRows.
…d surfaces

Window sizes are no longer guessed from token counts alone: opus-4-8 and
[1m] model ids map to the 1M window (a 190K opus-4-8 session used to
render as 95% of a 200K window), and Codex omits the percentage instead
of borrowing Anthropic constants when the rollout lacks
model_context_window.

Codex compaction accounting now reads the encrypted compaction item and
developer messages from replacement_history, so post-compaction windows
stop undercounting. Both builders stream with largeLineAsBuffer so one
oversized line cannot silently truncate the walk.

The tree API stops sending session.filePath, serves the flattened rows
so the dash renders the same tree as the CLI and TUI, resolves ids
directly instead of re-scanning every session file per request, and the
tree cache evicts LRU. Session discovery honors CLAUDE_CONFIG_DIRS,
CLAUDE_CONFIG_DIR, and CODEX_HOME, stats files in parallel, and id
lookups stat only matching files.

CLI gains --provider codex and machine-readable --list --json; --full
now opens the TUI in full scope instead of bypassing it. The TUI shows
build errors instead of a frozen spinner and drops the ref-plus-counter
repaint for plain state. The dashboard scopes the usage error banner and
device sidebar to the Usage page.
@iamtoruk iamtoruk merged commit 9b20dfd into main Jul 2, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant