xonecas.io
64875
Mc

mini-coder / commit fd39041

feat: compaction based on Jetbrains research on it

xonecas2026-04-29 fd390418975bcc99c29bbe6434fefba8f702e163 parent 92f0876

3 files changed

TODO.md+4−0
@@ -6,6 +6,10 @@
66
77 Raw notes, references:
88
9+### JetBrains Junie: Observation Masking
10+
11+Published research found that **simply hiding old tool outputs** matched the quality of full LLM summarization with **zero extra compute**:
12+
913 > good lessons from claude and general advice, seems good and grounded on real experience. Matches with learnings.
1014 > https://www.indiehackers.com/post/the-complete-guide-to-writing-agent-system-prompts-lessons-from-reverse-engineering-claude-code-6e18d54294
1115
src/agent.ts+44−0
@@ -1,9 +1,11 @@
11 import {
22 type AssistantMessage,
33 type Context,
4+ type Message,
45 streamSimple,
56 type TextContent,
67 type ToolCall,
8+ type ToolResultMessage,
79 } from "@mariozechner/pi-ai";
810 import { getApiKey } from "./oauth";
911 import type {
@@ -13,6 +15,48 @@ import type {
1315 ToolAndRunner,
1416 } from "./types";
1517
18+// ### JetBrains Junie: Observation Masking
19+// Published research found that **simply hiding old tool outputs** matched the quality of full LLM summarization with **zero extra compute**:
20+// https://blog.jetbrains.com/research/2025/12/efficient-context-management/
21+export function compactContext(messages: Message[]) {
22+ const KEEP_OBSERVATIONS = 10;
23+
24+ const toolResultIndices: number[] = [];
25+ for (let i = 0; i < messages.length; i++) {
26+ if (messages[i].role !== "toolResult") continue;
27+ const content = (messages[i] as ToolResultMessage).content;
28+ if (
29+ content.length === 1 &&
30+ content[0].type === "text" &&
31+ content[0].text.startsWith("Old environment output:")
32+ ) {
33+ continue;
34+ }
35+ toolResultIndices.push(i);
36+ }
37+
38+ if (toolResultIndices.length <= KEEP_OBSERVATIONS) return;
39+
40+ for (let i = 0; i < toolResultIndices.length - KEEP_OBSERVATIONS; i++) {
41+ const idx = toolResultIndices[i];
42+ const msg = messages[idx] as ToolResultMessage;
43+ let lines = 0;
44+ let images = 0;
45+ for (const c of msg.content) {
46+ if (c.type === "text") {
47+ lines += c.text.split(/\r?\n/).length;
48+ } else if (c.type === "image") {
49+ images++;
50+ }
51+ }
52+ let text = `Old environment output: (${lines} lines omitted)`;
53+ if (images > 0) {
54+ text += ` (${images} image${images > 1 ? "s" : ""} omitted)`;
55+ }
56+ msg.content = [{ type: "text", text }];
57+ }
58+}
59+
1660 export async function* streamAgent(
1761 agentCtx: AgentContex,
1862 ): AsyncGenerator<AgentEvent> {
src/tui.ts+9−1
@@ -1,6 +1,6 @@
11 import { cel, HStack, ProcessTerminal, VStack } from "@cel-tui/core";
22 import simpleGit from "simple-git";
3-import { streamAgent } from "./agent";
3+import { compactContext, streamAgent } from "./agent";
44 import {
55 buildSystemPrompt,
66 injectEnvReminder,
@@ -217,7 +217,15 @@ async function streamAgentTUI(state: TUIState) {
217217 const id = secureRandomString(10);
218218 state.sessionId = id;
219219 }
220+ // TODO: Should we make this delta only so compaction doesn;t affect saves?
221+ // I'm not sure since it we do, there is no trace in logs about compaction
222+ // and that would mean the logs don't repesent the truth. Confusing decision.
220223 await updateSession(state.sessionId, state.messages);
224+
225+ // Compact after saving, if the next turn fails because of compaction, the session is recoverable.
226+ // Compact at 80k tokens, the dumb zone threshold.
227+ if (estimateTokens(JSON.stringify(state.messages)) > 80000)
228+ compactContext(state.messages);
221229 }
222230
223231 try {