fix fitPageContent: proportional overhead, never drop all content on small contexts

This commit is contained in:
russell@unturf.com 2026-03-03 16:38:32 -05:00
parent d0d65f8ae4
commit 940d9c849f

View file

@ -299,11 +299,10 @@ function collapseSidebarDebris(text) {
// No summarization: content is real, just structurally compressed.
// Stages run in order of aggressiveness, stopping when content fits.
export function fitPageContent(pageContent, maxModelTokens) {
// Reserve tokens for everything except page content:
// system prompt ~300, computed intel ~200, analysis ~300,
// wrapper text ~100, chat history ~8000, output ~4000, safety ~2000
const overhead = 15000;
const maxPageTokens = Math.max(0, maxModelTokens - overhead);
// Reserve 20% of context for overhead (system prompt, chat history, output)
// but never more than 15k and always leave at least 60% for page content
const overhead = Math.min(15000, Math.floor(maxModelTokens * 0.2));
const maxPageTokens = Math.max(Math.floor(maxModelTokens * 0.6), maxModelTokens - overhead);
const originalTokens = estimateTokens(pageContent);
if (originalTokens <= maxPageTokens) {