xonecas.io
64875
Mc

mini-coder / commit d781b45

feat: move compaction down to the agent core

- This makes headless also benefit from it.

xonecas2026-05-14 d781b45f20c88e2911d91b49c6790b4397d7393e parent a6806db

2 files changed

src/agent.ts+11−1
@@ -9,6 +9,7 @@ import {
99 type ToolResultMessage,
1010 } from "@earendil-works/pi-ai";
1111 import { getApiKey } from "./oauth";
12+import { estimateTokens } from "./shared";
1213 import type {
1314 AgentContex,
1415 AgentEvent,
@@ -77,6 +78,10 @@ export async function* streamAgent(
7778
7879 // Main agent loop, continues until llm sends a response other than toolCall or has no tool calls.
7980 while (true) {
81+ let estimate = estimateTokens(JSON.stringify(llmCtx));
82+ // 80k is the agreed uppon threshold to the DUMB ZONE
83+ if (estimate > 80000) compactContext(llmCtx.messages);
84+
8085 const s = streamSimple(agentCtx.options.model, llmCtx, {
8186 reasoning: agentCtx.options.effort,
8287 signal: agentCtx.signal,
@@ -103,13 +108,18 @@ export async function* streamAgent(
103108 case "thinking_end":
104109 case "toolcall_start":
105110 case "toolcall_delta":
106- case "toolcall_end":
111+ case "toolcall_end": {
107112 if (partial) {
108113 partial = e.partial;
109114 llmCtx.messages[llmCtx.messages.length - 1] = partial;
110115 yield { type: "message_update", partial };
111116 }
117+
118+ estimate = estimateTokens(JSON.stringify(llmCtx));
119+ // 80k is the agreed uppon threshold to the DUMB ZONE
120+ if (estimate > 80000) compactContext(llmCtx.messages);
112121 break;
122+ }
113123
114124 case "error": {
115125 const finalMessage = await s.result();
src/tui.ts+11−13
@@ -1,5 +1,5 @@
11 import { cel, HStack, ProcessTerminal, VStack } from "@cel-tui/core";
2-import { compactContext, streamAgent } from "./agent";
2+import { streamAgent } from "./agent";
33 import { getBranchLabel } from "./git";
44 import {
55 buildSystemPrompt,
@@ -158,10 +158,6 @@ async function streamAgentTUI(state: TUIState) {
158158 });
159159 state.prompt = "";
160160
161- // Compact at 80k tokens, the dumb zone threshold.
162- if (estimateTokens(JSON.stringify(state.messages)) > 80000)
163- compactContext(state.messages);
164-
165161 const systemPrompt = await buildSystemPrompt(MAIN_PROMPT);
166162 const ctx: AgentContex = {
167163 systemPrompt,
@@ -179,11 +175,13 @@ async function streamAgentTUI(state: TUIState) {
179175 switch (ev.type) {
180176 case "message_start":
181177 case "message_update":
178+ case "message_end": {
179+ const { systemPrompt, tools, messages } = ctx;
180+ state.contextSize = estimateTokens(
181+ JSON.stringify({ systemPrompt, tools, messages }),
182+ );
182183 break;
183-
184- case "message_end":
185- state.contextSize = estimateTokens(JSON.stringify(ctx));
186- break;
184+ }
187185
188186 case "tool_message_start":
189187 case "tool_message_update":
@@ -204,7 +202,10 @@ async function streamAgentTUI(state: TUIState) {
204202 state.messages[idx] = withReminder;
205203 }
206204
207- state.contextSize = estimateTokens(JSON.stringify(ctx));
205+ const { systemPrompt, tools, messages } = ctx;
206+ state.contextSize = estimateTokens(
207+ JSON.stringify({ systemPrompt, tools, messages }),
208+ );
208209 }
209210 }
210211 }
@@ -214,9 +215,6 @@ async function streamAgentTUI(state: TUIState) {
214215 const id = secureRandomString(10);
215216 state.sessionId = id;
216217 }
217- // TODO: Should we make this delta only so compaction doesn;t affect saves?
218- // I'm not sure since it we do, there is no trace in logs about compaction
219- // and that would mean the logs don't repesent the truth. Confusing decision.
220218 await updateSession(state.sessionId, state.messages);
221219 }