@@ -20,17 +20,31 @@ import type {
2020 // Published research found that **simply hiding old tool outputs** matched the quality of full LLM summarization with **zero extra compute**:
2121 // https://blog.jetbrains.com/research/2025/12/efficient-context-management/
2222 export function compactContext(messages: Message[]) {
23- // TODO: Preserve SKILL.md contents, and exclude them from compaction.
24- // Problem: How do we know? we need to check each message result againt it's arguments
25- // and then find if by chance it has a read skill command... This is a mess. We we.
26- // A better way would be to first add a read(path, lines, offset). And then we know
27- // which files are read using it, and can match by path if it's a SKILL.md ending.
2823 const KEEP_OBSERVATIONS = 10;
2924
25+ const skillReadToolCallIds = new Set<string>();
26+ for (const message of messages) {
27+ if (message.role !== "assistant") continue;
28+
29+ for (const content of message.content) {
30+ if (content.type !== "toolCall") continue;
31+ if (content.name !== "read") continue;
32+
33+ const path = content.arguments.path;
34+ if (typeof path === "string" && path.endsWith("SKILL.md")) {
35+ skillReadToolCallIds.add(content.id);
36+ }
37+ }
38+ }
39+
3040 const toolResultIndices: number[] = [];
3141 for (let i = 0; i < messages.length; i++) {
3242 if (messages[i].role !== "toolResult") continue;
33- const content = (messages[i] as ToolResultMessage).content;
43+
44+ const message = messages[i] as ToolResultMessage;
45+ if (skillReadToolCallIds.has(message.toolCallId)) continue;
46+
47+ const content = message.content;
3448 if (
3549 content.length === 1 &&
3650 content[0].type === "text" &&
@@ -130,12 +144,6 @@ export async function* streamAgent(
130144 return;
131145 }
132146 }
133-
134- // Experiment: use this inside the inner loop for "running" compaction
135- // after we are in the dumb zone.
136- const estimate = estimateTokens(JSON.stringify(llmCtx));
137- // 80k is the agreed uppon threshold to the DUMB ZONE
138- if (estimate > 80000) compactContext(llmCtx.messages);
139147 }
140148
141149 const message = await s.result();