#15726: fix(sessions): use model contextWindow instead of agent contextTokens in session listing
commands
stale
size: XS
experienced-contributor
Cluster:
Context Management Fixes
## Summary
Fixes #15705. Also addresses #15697.
The `sessionsCommand` fallback chain for `configContextTokens` incorrectly prioritized `cfg.agents.defaults.contextTokens` (the agent max buffer setting) over the model's actual context window. When users set a large buffer value (e.g. 1M), session listings showed inflated context windows and misleading token percentages.\n\n## Root Cause\n\nIn `src/commands/sessions.ts`, the fallback chain was:\n\n```typescript\nconst configContextTokens =\n cfg.agents?.defaults?.contextTokens ?? // agent buffer (e.g. 1M)\n lookupContextTokens(resolved.model) ?? // model's actual contextWindow\n DEFAULT_CONTEXT_TOKENS; // 200k\n```\n\n`cfg.agents.defaults.contextTokens` is the agent's max buffer setting, not the model's context window. Because `??` only falls through on `null`/`undefined`, a configured value (e.g. `1_000_000`) always wins, even when the model's actual context window is known.\n\n## Fix\n\nRemove `cfg.agents.defaults.contextTokens` from the fallback chain:\n\n```typescript\nconst configContextTokens =\n lookupContextTokens(resolved.model) ?? DEFAULT_CONTEXT_TOKENS;\n```\n\n## Before / After\n\n**Before** (agent buffer = 1M, model = MiniMax-M2.5-Omni with 204k context):\n```\ndirect +15555550123 30m ago MiniMax-M2.5-Omni 60k/1000k (6%)\n```\n\n**After:**\n```\ndirect +15555550123 30m ago MiniMax-M2.5-Omni 60k/200k (30%)\n```\n\n## Test Plan\n\n- Updated existing e2e test assertions to reflect correct fallback behavior\n- Added regression test verifying `agents.defaults.contextTokens` does not leak into display\n- All 4 tests pass: `pnpm vitest run src/commands/sessions.e2e.test.ts --config vitest.e2e.config.ts`
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR updates `sessionsCommand` to derive the displayed context window from model metadata (`lookupContextTokens(resolved.model)`) and fall back to `DEFAULT_CONTEXT_TOKENS`, rather than incorrectly preferring `cfg.agents.defaults.contextTokens` (agent buffer size). The sessions e2e tests are updated accordingly and add a regression assertion to ensure `agents.defaults.contextTokens` does not affect the JSON/table display when the model context window is unknown.
<h3>Confidence Score: 5/5</h3>
- This PR is safe to merge with minimal risk.
- The change is narrowly scoped to a single fallback chain in `sessionsCommand` and aligns the displayed context window with model metadata, preventing misleading token percentages. Test updates and an explicit regression test cover the behavior change for both table and JSON outputs.
- No files require special attention
<sub>Last reviewed commit: 058ef3f</sub>
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#19412: fix(status): prefer configured contextTokens over session entry
by rafaelipuente · 2026-02-17
84.3%
#11109: fix(tui): prefer config contextTokens over persisted session value
by marezgui · 2026-02-07
84.2%
#17414: fix(sessions): refresh contextTokens when model override changes
by michaelbship · 2026-02-15
83.8%
#16478: fix(gateway): fall back to lookupContextTokens on model switch
by colddonkey · 2026-02-14
83.3%
#18886: fix(status): prefer configured contextTokens over model metadata
by BinHPdev · 2026-02-17
82.8%
#23299: fix(status): show runtime model context limit instead of stale sess...
by SidQin-cyber · 2026-02-22
81.7%
#18721: fix: prefer configured contextTokens over model catalog in status d...
by MisterGuy420 · 2026-02-17
81.5%
#16609: fix: resolve session store race condition and contextTokens updates
by battman21 · 2026-02-14
81.3%
#14879: fix: persist session metadata to sessions.json after context pruning
by skylarkoo7 · 2026-02-12
80.3%
#6053: fix: use 400K context window instead of 200K if the model allows (g...
by icedac · 2026-02-01
80.0%