xonecas.io
64875
Mc

mini-coder / commit 4cabfb5

feat: try to delay compaction as much as possible

xonecas2026-07-09 4cabfb5ed52b5df594f6f3836c799277315d1dcc parent bfa983c

3 files changed

src/agent.ts+13−5
@@ -8,7 +8,12 @@ import type {
88 ToolResultMessage,
99 } from "@earendil-works/pi-ai";
1010 import { createAppModels } from "./models.ts";
11-import { estimateContextTokens, estimateTokens } from "./shared";
11+import {
12+ contextCompactionThreshold,
13+ estimateContextTokens,
14+ estimateNextTurnTokens,
15+ estimateTokens,
16+} from "./shared";
1217 import { truncateToolOutput } from "./tool-output.ts";
1318 import type {
1419 AgentContex,
@@ -96,10 +101,13 @@ export async function* streamAgent(
96101 while (true) {
97102 const fallbackContext = JSON.stringify(llmCtx);
98103 let estimate = estimateContextTokens(llmCtx.messages, fallbackContext);
99- // Stay below both the agreed-upon 80k DUMB ZONE and smaller model windows.
100- const compactionThreshold = Math.min(
101- 80_000,
102- Math.floor(agentCtx.options.model.contextWindow * 0.8),
104+ const estimatedNextTurn = estimateNextTurnTokens(
105+ llmCtx.messages,
106+ agentCtx.options.model.maxTokens,
107+ );
108+ const compactionThreshold = contextCompactionThreshold(
109+ agentCtx.options.model.contextWindow,
110+ estimatedNextTurn,
103111 );
104112 if (estimate > compactionThreshold) {
105113 for (
src/shared.ts+60−17
@@ -80,19 +80,44 @@ export function takeTail<T>(arr: T[], x: number): T[] {
8080 return x <= 0 ? [] : arr.slice(-x);
8181 }
8282
83-function reportedContextTokens(message: Message): number | undefined {
83+const MAX_CONTEXT_SAFETY_TOKENS = 4_096;
84+const CONTEXT_SAFETY_RATIO = 0.05;
85+
86+type ReportedUsage = {
87+ contextTokens: number;
88+ outputTokens: number;
89+};
90+
91+function reportedUsage(message: Message): ReportedUsage | undefined {
8492 if (message.role !== "assistant" || !message.usage) return;
93+ if (message.stopReason === "aborted" || message.stopReason === "error")
94+ return;
8595
8696 const { usage } = message;
87- const reported = usage.totalTokens;
88- if (Number.isFinite(reported) && reported > 0) {
89- return Math.ceil(reported);
90- }
91-
9297 const calculated =
9398 usage.input + usage.output + usage.cacheRead + usage.cacheWrite;
94- if (Number.isFinite(calculated) && calculated > 0) {
95- return Math.ceil(calculated);
99+ const contextTokens =
100+ Number.isFinite(usage.totalTokens) && usage.totalTokens > 0
101+ ? usage.totalTokens
102+ : calculated;
103+ if (!Number.isFinite(contextTokens) || contextTokens <= 0) return;
104+
105+ const outputTokens = Number.isFinite(usage.output)
106+ ? Math.max(0, Math.ceil(usage.output))
107+ : 0;
108+
109+ return {
110+ contextTokens: Math.ceil(contextTokens),
111+ outputTokens,
112+ };
113+}
114+
115+function latestReportedUsage(
116+ messages: readonly Message[],
117+): (ReportedUsage & { index: number }) | undefined {
118+ for (let index = messages.length - 1; index >= 0; index--) {
119+ const usage = reportedUsage(messages[index]);
120+ if (usage) return { ...usage, index };
96121 }
97122 }
98123
@@ -101,20 +126,38 @@ export function estimateTokens(text: string): number {
101126 }
102127
103128 export function estimateContextTokens(
104- messages: Message[],
129+ messages: readonly Message[],
105130 fallbackSerializedContext = JSON.stringify(messages),
106131 ): number {
107- for (let index = messages.length - 1; index >= 0; index--) {
108- const providerTokens = reportedContextTokens(messages[index]);
109- if (providerTokens === undefined) continue;
132+ const usage = latestReportedUsage(messages);
133+ if (!usage) return estimateTokens(fallbackSerializedContext);
110134
111- const trailingMessages = messages.slice(index + 1);
112- if (trailingMessages.length === 0) return providerTokens;
135+ const trailingMessages = messages.slice(usage.index + 1);
136+ if (trailingMessages.length === 0) return usage.contextTokens;
113137
114- return providerTokens + estimateTokens(JSON.stringify(trailingMessages));
115- }
138+ return usage.contextTokens + estimateTokens(JSON.stringify(trailingMessages));
139+}
140+
141+export function estimateNextTurnTokens(
142+ messages: readonly Message[],
143+ maxTokens: number,
144+): number {
145+ const usage = latestReportedUsage(messages);
146+ if (!usage) return 0;
147+
148+ return Math.min(usage.outputTokens, Math.max(0, maxTokens));
149+}
150+
151+export function contextCompactionThreshold(
152+ contextWindow: number,
153+ estimatedNextTurnTokens: number,
154+): number {
155+ const safetyTokens = Math.min(
156+ MAX_CONTEXT_SAFETY_TOKENS,
157+ Math.floor(Math.max(0, contextWindow) * CONTEXT_SAFETY_RATIO),
158+ );
116159
117- return estimateTokens(fallbackSerializedContext);
160+ return Math.max(0, contextWindow - estimatedNextTurnTokens - safetyTokens);
118161 }
119162
120163 export function parseSkillFrontmatter(content: string) {
src/tui-components.ts+4−9
@@ -82,20 +82,15 @@ export function ContextPill(state: TUIState) {
8282 text = "0%";
8383 bg = theme.bwhite;
8484 } else {
85- // Colors show the progress in the "smart window" or how much
86- // before the dumb zone. The user facing percentage is the model
87- // amount. An elegant way to show both :)
8885 const max = state.options.model.contextWindow;
89- const smartMax = 80000;
9086 const percent = Math.floor((state.contextSize / max) * 100);
91- const smartPercent = Math.floor((state.contextSize / smartMax) * 100);
92- if (smartPercent > 90) {
87+ if (percent > 90) {
9388 bg = theme.bred;
94- } else if (smartPercent > 85) {
89+ } else if (percent > 85) {
9590 bg = theme.red;
96- } else if (smartPercent > 80) {
91+ } else if (percent > 80) {
9792 bg = theme.yellow;
98- } else if (smartPercent > 60) {
93+ } else if (percent > 60) {
9994 bg = theme.byellow;
10095 }
10196 text = `~${percent}%`;