-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcompactor.py
More file actions
379 lines (306 loc) · 12.9 KB
/
compactor.py
File metadata and controls
379 lines (306 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
"""compactor: in-session message compaction.
Shrinks the in-context messages list when it grows past a turn count or token
threshold by summarizing older user/assistant turns into a single synthetic
user message. The full transcript on disk (sessions/<id>.messages.json) is
left untouched. Compaction records are written to:
sessions/<id>.compactions.jsonl append-only structured records
sessions/<id>.compactions.index.json fast lookup index
Does NOT touch .mimi/MEMORY.md or .mimi/RULES.md. Those systems remain as-is.
Triggers (auto):
- uncompacted user turns >= MIMICODE_COMPACT_TURN_INTERVAL (default 5)
- last input tokens >= MIMICODE_COMPACT_TOKEN_THRESHOLD (default 20000)
Toggle:
- MIMICODE_COMPACT_AUTO=0 disables auto-compaction (manual still works)
"""
from __future__ import annotations
import json
import os
import time
from pathlib import Path
import anthropic
from logger import log
DEFAULT_TURN_INTERVAL = 5
DEFAULT_TOKEN_THRESHOLD = 20_000
KEEP_RECENT_USER_TURNS = 2
SUMMARIZER_MODEL = "claude-haiku-4-5-20251001"
SUMMARIZER_MAX_TOKENS = 2048
COMPACTION_PROMPT = """\
You are summarizing a slice of a coding-agent transcript so it can replace the original messages in-context.
Return ONLY a single JSON object — no prose, no code fences. Schema:
{{
"one_line": "single sentence summary of this slice",
"user_intents": ["..."],
"decisions": ["..."],
"files_touched": [{{"path": "...", "what": "...", "why": "..."}}],
"tools_used": {{"bash": 0, "read": 0, "edit": 0, "write": 0}},
"key_findings": ["..."],
"open_issues": ["..."]
}}
If a previous "[COMPACTED ...]" marker appears in the slice, fold its contents
into your output rather than discarding it.
Slice transcript:
{transcript}
"""
def auto_enabled() -> bool:
return os.environ.get("MIMICODE_COMPACT_AUTO", "1") not in ("0", "false", "False", "off")
def set_auto(value: bool) -> None:
os.environ["MIMICODE_COMPACT_AUTO"] = "1" if value else "0"
def turn_interval() -> int:
try:
return max(1, int(os.environ.get("MIMICODE_COMPACT_TURN_INTERVAL", DEFAULT_TURN_INTERVAL)))
except ValueError:
return DEFAULT_TURN_INTERVAL
def token_threshold() -> int:
try:
return max(1000, int(os.environ.get("MIMICODE_COMPACT_TOKEN_THRESHOLD", DEFAULT_TOKEN_THRESHOLD)))
except ValueError:
return DEFAULT_TOKEN_THRESHOLD
# ---------------------------------------------------------------------------
# message inspection
# ---------------------------------------------------------------------------
def _is_real_user_turn(m: dict) -> bool:
"""A 'real' user turn: role=user with string content (not a tool_result list)."""
return m.get("role") == "user" and isinstance(m.get("content"), str)
def _is_marker(m: dict) -> bool:
return _is_real_user_turn(m) and m["content"].startswith("[COMPACTED")
def _user_turn_indices(messages: list[dict]) -> list[int]:
return [i for i, m in enumerate(messages) if _is_real_user_turn(m)]
def find_compaction_split(messages: list[dict], keep_recent: int = KEEP_RECENT_USER_TURNS) -> int:
"""Index at which to split — everything < idx gets compacted.
Returns 0 when there aren't enough user turns to compact safely.
Splits on a user-turn boundary so tool_use/tool_result pairs stay intact.
"""
user_indices = _user_turn_indices(messages)
if len(user_indices) <= keep_recent:
return 0
return user_indices[-keep_recent]
def uncompacted_user_turn_count(messages: list[dict]) -> int:
"""Count real user turns excluding any compaction marker.
Used to decide whether the turn-interval trigger should fire.
"""
return sum(1 for m in messages if _is_real_user_turn(m) and not _is_marker(m))
def should_auto_compact(messages: list[dict], last_tokens_in: int) -> tuple[bool, str]:
if not auto_enabled():
return False, ""
split = find_compaction_split(messages)
if split == 0:
return False, ""
uncompacted = uncompacted_user_turn_count(messages)
interval = turn_interval()
if uncompacted >= interval + KEEP_RECENT_USER_TURNS:
return True, f"turn_interval ({uncompacted} uncompacted turns)"
threshold = token_threshold()
if last_tokens_in and last_tokens_in >= threshold:
return True, f"token_threshold ({last_tokens_in} >= {threshold})"
return False, ""
# ---------------------------------------------------------------------------
# transcript flattening for the summarizer
# ---------------------------------------------------------------------------
def _flatten_for_summary(messages: list[dict], max_per_block: int = 600) -> str:
parts: list[str] = []
for m in messages:
role = m.get("role", "")
content = m.get("content", "")
if isinstance(content, str):
parts.append(f"[{role}] {content[:max_per_block]}")
elif isinstance(content, list):
for b in content:
t = b.get("type")
if t == "text":
parts.append(f"[{role}] {b.get('text', '')[:max_per_block]}")
elif t == "tool_use":
inp = b.get("input", {}) or {}
parts.append(f"[tool:{b.get('name','?')}] {json.dumps(inp)[:300]}")
elif t == "tool_result":
out = (b.get("content") or "")
if isinstance(out, list):
out = json.dumps(out)
err = " (error)" if b.get("is_error") else ""
parts.append(f"[result{err}] {str(out)[:max_per_block]}")
return "\n".join(parts)
# ---------------------------------------------------------------------------
# disk paths
# ---------------------------------------------------------------------------
def compactions_path(session_path: Path) -> Path:
return session_path.with_suffix(".compactions.jsonl")
def index_path(session_path: Path) -> Path:
return session_path.with_suffix(".compactions.index.json")
def _next_compaction_id(session_path: Path) -> str:
p = compactions_path(session_path)
if not p.exists():
return "c001"
n = sum(1 for line in p.read_text(encoding="utf-8").splitlines() if line.strip())
return f"c{n + 1:03d}"
def _append_record(session_path: Path, record: dict) -> None:
p = compactions_path(session_path)
p.parent.mkdir(parents=True, exist_ok=True)
with p.open("a", encoding="utf-8") as f:
f.write(json.dumps(record) + "\n")
def _update_index(session_path: Path, entry: dict) -> None:
p = index_path(session_path)
if p.exists():
try:
data = json.loads(p.read_text(encoding="utf-8"))
except json.JSONDecodeError:
data = {"compactions": []}
else:
data = {"compactions": []}
data.setdefault("compactions", []).append(entry)
p.write_text(json.dumps(data, indent=2), encoding="utf-8")
def list_compactions(session_path: Path) -> list[dict]:
p = index_path(session_path)
if not p.exists():
return []
try:
return json.loads(p.read_text(encoding="utf-8")).get("compactions", [])
except (json.JSONDecodeError, OSError):
return []
def load_compaction(session_path: Path, compaction_id: str) -> dict | None:
p = compactions_path(session_path)
if not p.exists():
return None
for line in p.read_text(encoding="utf-8").splitlines():
if not line.strip():
continue
try:
rec = json.loads(line)
except json.JSONDecodeError:
continue
if rec.get("id") == compaction_id:
return rec
return None
# ---------------------------------------------------------------------------
# summarization
# ---------------------------------------------------------------------------
def _summarize(transcript: str) -> dict:
api_key = os.environ.get("ANTHROPIC_API_KEY", "")
if not api_key:
return {"one_line": "compaction-skipped-no-api-key"}
client = anthropic.Anthropic(api_key=api_key)
prompt = COMPACTION_PROMPT.format(transcript=transcript)
try:
resp = client.messages.create(
model=SUMMARIZER_MODEL,
max_tokens=SUMMARIZER_MAX_TOKENS,
messages=[{"role": "user", "content": prompt}],
)
except Exception as e:
return {"one_line": f"compaction-summary-failed: {type(e).__name__}", "error": str(e)[:300]}
text = resp.content[0].text.strip()
if text.startswith("```"):
lines = text.splitlines()
text = "\n".join(l for l in lines if not l.startswith("```")).strip()
try:
return json.loads(text)
except json.JSONDecodeError:
return {"one_line": "compaction-summary-parse-failed", "raw": text[:1500]}
def _format_marker(compaction_id: str, summary: dict, turn_range: tuple[int, int]) -> str:
parts = [
f"[COMPACTED — turns {turn_range[0]}–{turn_range[1]}, id={compaction_id}]",
f"Summary: {summary.get('one_line', '(none)')}",
]
if summary.get("user_intents"):
parts.append("User intents: " + "; ".join(map(str, summary["user_intents"])))
if summary.get("decisions"):
parts.append("Decisions: " + "; ".join(map(str, summary["decisions"])))
if summary.get("files_touched"):
ft = ", ".join(
f.get("path", "?") if isinstance(f, dict) else str(f)
for f in summary["files_touched"]
)
parts.append(f"Files touched: {ft}")
if summary.get("key_findings"):
parts.append("Key findings: " + "; ".join(map(str, summary["key_findings"])))
if summary.get("open_issues"):
parts.append("Open issues: " + "; ".join(map(str, summary["open_issues"])))
parts.append(
f'(Full record available via recall_compaction(compaction_id="{compaction_id}").)'
)
return "\n".join(parts)
# ---------------------------------------------------------------------------
# main entry points
# ---------------------------------------------------------------------------
def compact(
messages: list[dict],
session_path: Path,
keep_recent: int = KEEP_RECENT_USER_TURNS,
reason: str = "manual",
) -> tuple[list[dict], dict | None]:
"""Compact older messages. Returns (new_messages, record_or_None).
No-op (returns original list) when there's nothing safely compactable."""
split = find_compaction_split(messages, keep_recent)
if split == 0:
return messages, None
slice_to_compact = messages[:split]
if not slice_to_compact:
return messages, None
user_indices_in_slice = [i for i, m in enumerate(slice_to_compact) if _is_real_user_turn(m)]
if not user_indices_in_slice:
return messages, None
prior_turns = sum(
(c.get("turn_range", [0, 0])[1] - c.get("turn_range", [0, 0])[0] + 1)
for c in list_compactions(session_path)
)
real_in_slice = sum(1 for m in slice_to_compact if _is_real_user_turn(m) and not _is_marker(m))
turn_lo = prior_turns + 1
turn_hi = prior_turns + real_in_slice if real_in_slice else turn_lo
transcript = _flatten_for_summary(slice_to_compact)
summary = _summarize(transcript)
cid = _next_compaction_id(session_path)
timestamp = time.time()
record = {
"id": cid,
"timestamp": timestamp,
"turn_range": [turn_lo, turn_hi],
"msg_count_compacted": len(slice_to_compact),
"reason": reason,
"summary": summary,
}
_append_record(session_path, record)
_update_index(session_path, {
"id": cid,
"timestamp": timestamp,
"turn_range": [turn_lo, turn_hi],
"one_line": summary.get("one_line", ""),
"reason": reason,
})
marker_text = _format_marker(cid, summary, (turn_lo, turn_hi))
new_messages = (
[
{"role": "user", "content": marker_text},
{"role": "assistant", "content": [
{"type": "text", "text": f"Acknowledged compaction {cid}."}
]},
]
+ messages[split:]
)
log("compaction_done", {
"id": cid,
"reason": reason,
"msgs_compacted": len(slice_to_compact),
"turn_range": [turn_lo, turn_hi],
"msgs_after": len(new_messages),
})
return new_messages, record
def maybe_compact(
messages: list[dict],
session_path: Path,
last_tokens_in: int = 0,
) -> tuple[list[dict], dict | None]:
"""Auto-compact if a trigger fires. No-op otherwise."""
fire, reason = should_auto_compact(messages, last_tokens_in)
if not fire:
return messages, None
return compact(messages, session_path, reason=f"auto:{reason}")
def status_text(session_path: Path, last_tokens_in: int = 0) -> str:
"""Human-readable status for the :compact / /compact status command."""
enabled = "on" if auto_enabled() else "off"
interval = turn_interval()
threshold = token_threshold()
n = len(list_compactions(session_path))
return (
f"compaction: {enabled} · "
f"every {interval} turns or {threshold} tokens · "
f"last_tokens_in={last_tokens_in} · "
f"prior compactions={n}"
)