Skip to content

Commit 32b9f0b

Browse files
DEXclaude
authored andcommitted
feat: v3.1 — Context Window Guardian (smart auto-save at 50%/75%)
New continuous monitoring behavior: - Track context window usage via observable signals (turns, tool calls, compaction markers, re-reads) - 50% threshold: lightweight checkpoint save (skip maintenance) - 75% threshold: full session-wrap + suggest new session - 80%+ emergency: minimize responses, prioritize saving - Session behavior adapts: free exploration → targeted reads → minimal responses as context grows - Platform-specific compaction detection (Claude [compacted], Cursor truncation, etc.) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent f06d41a commit 32b9f0b

3 files changed

Lines changed: 155 additions & 8 deletions

File tree

README.md

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# session-wrap v3.0
1+
# session-wrap v3.1
22

3-
**通用 AI Agent 記憶持久化** — 對話結束自動保存上下文,下次無縫接續。支援 19+ AI 編碼助手與 AI 平台。
3+
**通用 AI Agent 記憶持久化** — 對話結束自動保存上下文,下次無縫接續。v3.1 新增 Context Window 智慧管理。支援 19+ AI 平台。
44

55
Universal memory persistence for every AI agent. Say "wrap up" — context survives to the next session.
66

@@ -171,7 +171,49 @@ npx session-wrap-skill uninstall
171171

172172
---
173173

174-
## v3.0 新功能:記憶維護引擎
174+
## v3.1 新功能:Context Window 智慧管理
175+
176+
不用等到「收工」才存。AI 會**持續監控**上下文使用量,自動在關鍵節點保存記憶。
177+
178+
### 三段式閾值機制
179+
180+
```
181+
上下文使用量
182+
─────────────────────────────────────────────────
183+
0% 50% 75% 100%
184+
│ 🟢 正常工作 │ 🟠 檢查點存檔 │ 🔴 完整存檔 │
185+
│ │ (輕量快速) │ (含維護壓縮) │
186+
│ │ │ → 建議開新對話│
187+
```
188+
189+
| 閾值 | 行為 |
190+
|------|------|
191+
| **~50%** | 🟠 **自動檢查點** — 快速保存當前進度(跳過維護),然後繼續工作 |
192+
| **~75%** | 🔴 **完整存檔 + 提醒** — 執行全套 session-wrap,建議開新對話 |
193+
| **> 80%** | 🔴 **緊急模式** — 最小化回應,優先保存所有未存的上下文 |
194+
195+
### 怎麼判斷上下文用了多少?
196+
197+
AI 沒有精確 token 計數器,但可以從訊號估算:
198+
199+
| 訊號 | 說明 |
200+
|------|------|
201+
| 對話輪次 | 30+ 輪通常已過半 |
202+
| 工具呼叫數 | 60+ 次通常已過半 |
203+
| 系統壓縮訊息 | 看到 `[compacted]` = 已經被壓縮,立刻存檔 |
204+
| 重複讀檔 | 重新讀之前讀過的檔案 = 早期 context 被裁剪 |
205+
206+
### 各階段行為調整
207+
208+
| 階段 | 行為 |
209+
|------|------|
210+
| **早期 (< 30%)** | 自由探索、完整讀檔、詳細解釋 |
211+
| **中期 (30-60%)** | 精確定位讀取、合併工具呼叫、精簡回覆 |
212+
| **後期 (60%+)** | 只讀必要內容、引用記憶不重讀、主動存檔 |
213+
214+
---
215+
216+
## v3.0 功能:記憶維護引擎
175217

176218
### 自動壓縮
177219

@@ -204,15 +246,22 @@ npx session-wrap-skill uninstall
204246
## 運作原理
205247

206248
```
207-
觸發「收工」
249+
持續監控 context window
250+
┌──────────────────┐
251+
│ < 50% → 正常 │
252+
│ ~ 50% → 檢查點 │ ← v3.1 新增
253+
│ ~ 75% → 完整存檔│
254+
└──────────────────┘
255+
256+
觸發「收工」或達到閾值
208257
209258
210259
┌───────────────────────────────────┐
211260
│ 1. 偵測平台(19+ agent) │
212261
│ 2. 決定記憶路徑 │
213262
│ 3. 掃描 git + 現有記憶 │
214263
│ 4. 分析本次 session 關鍵資訊 │
215-
│ 5. 記憶維護(壓縮/過期/去重) │ ← v3 新增
264+
│ 5. 記憶維護(壓縮/過期/去重) │ ← v3.0
216265
│ 6. 寫入/更新記憶檔案 │
217266
│ 7. 更新 MEMORY.md 索引 │
218267
│ 8. Bootstrap 注入(如需要) │
@@ -319,7 +368,8 @@ auto memory 被動。session-wrap 主動掃描 + 結構化寫入 + 維護壓縮
319368

320369
| 版本 | 日期 | 變更 |
321370
|------|------|------|
322-
| **v3.0** | 2026-03-25 | 記憶維護引擎(壓縮/過期/衝突解決/去重)、一鍵安裝 CLI、install.sh、19+ agent、韓語觸發 |
371+
| **v3.1** | 2026-03-25 | Context Window 智慧管理(50% 檢查點、75% 完整存檔、行為分階段調整) |
372+
| v3.0 | 2026-03-25 | 記憶維護引擎(壓縮/過期/衝突解決/去重)、一鍵安裝 CLI、install.sh、19+ agent、韓語觸發 |
323373
| v2.1.1 | 2026-03-25 | 新增 OpenClaw 支援 |
324374
| v2.1 | 2026-03-25 | 14+ agent、Bootstrap 機制、中文文件 |
325375
| v2.0 | 2026-03-25 | 通用化(Claude Code + Cursor + Windsurf) |

SKILL.md

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
name: session-wrap
3-
version: 3.0.0
3+
version: 3.1.0
44
description: |
55
Universal session wrap-up: automatically persist project context to memory files for seamless resume.
66
Works with ANY AI agent — Claude Code, Codex, Gemini CLI, Cursor, Windsurf, Cline, Roo Code,
@@ -38,6 +38,100 @@ Also trigger proactively when: major milestone reached, user signals finality, o
3838

3939
---
4040

41+
## Context Window Guardian
42+
43+
**This is a continuous monitoring behavior, not a one-time step.** Throughout the entire session, actively track context window usage and take action at defined thresholds.
44+
45+
### How to Estimate Context Usage
46+
47+
You don't have a precise token counter, but you can estimate based on observable signals:
48+
49+
| Signal | Estimation method |
50+
|--------|------------------|
51+
| **Conversation turns** | Count user messages + your responses. Average turn ≈ 500-2000 tokens. |
52+
| **Tool call accumulation** | Each file read ≈ 1000-5000 tokens. Each large grep/glob result ≈ 500-3000 tokens. |
53+
| **Code blocks generated** | Each code block ≈ 200-1000 tokens. |
54+
| **Session duration** | Long sessions (30+ turns, 60+ tool calls) are likely past 50%. |
55+
| **System compaction hints** | If you see `[compacted]` markers or system messages about compression, you're near the limit. |
56+
| **Repeated context** | If you're re-reading files you already read earlier, context may have been pruned — you're deep. |
57+
58+
**Rough formula**: `estimated_usage = (user_turns × 800) + (tool_results × 1500) + (your_responses × 600)`
59+
60+
For reference, common context windows:
61+
- Claude Code (Opus): ~200K tokens (1M with extended)
62+
- Cursor: ~128K tokens
63+
- Codex: ~128K tokens
64+
- Gemini CLI: ~1M tokens
65+
- Most others: ~64K-200K tokens
66+
67+
### Threshold Actions
68+
69+
| Context % | Action | What to do |
70+
|-----------|--------|------------|
71+
| **< 40%** | 🟢 Normal | Continue working. No action needed. |
72+
| **40-50%** | 🟡 Awareness | Start mentally noting what's important this session. No action yet. |
73+
| **~50%** | 🟠 **Checkpoint Save** | **Proactively execute a lightweight memory save.** Don't wait for user to say "收工". Announce: "上下文已過半,先存個記憶檢查點。" Then run Steps 1-8 (lightweight mode — skip maintenance, just save current state). |
74+
| **50-70%** | 🟠 Active management | After checkpoint, continue working but be concise. Avoid re-reading files already in context. Prefer targeted reads over full-file reads. |
75+
| **~75%** | 🔴 **Full Save + Alert** | Execute full session-wrap (including maintenance). Alert user: "上下文快滿了,建議開新對話繼續。記憶已保存。" |
76+
| **> 80%** | 🔴 Critical | If still in session, every response should be minimal. Prioritize saving any unsaved work to memory before context is auto-compacted. |
77+
78+
### Checkpoint Save (Lightweight Mode)
79+
80+
When triggered at ~50%, do a fast save without full maintenance:
81+
82+
1. **Skip** memory compression, deduplication, staleness check (Step 5)
83+
2. **Do** gather state (Step 2) → analyze (Step 3) → write (Step 4) → update index (Step 6)
84+
3. **Mark** the checkpoint in memory with timestamp: `> Checkpoint: 2026-03-25 14:30 (mid-session save)`
85+
4. **Continue working** — this is NOT a session end, just a safety save
86+
87+
### Full Save at ~75%
88+
89+
When triggered at ~75%, do the complete session-wrap:
90+
91+
1. Execute **all steps** (1-8) including maintenance
92+
2. Summarize what's left to do (if any incomplete tasks)
93+
3. Suggest user start a new session: "記憶已完整保存,建議開新對話繼續未完成的工作。"
94+
4. If user continues, keep responses minimal and focused
95+
96+
### Context-Aware Behavior Throughout Session
97+
98+
Beyond threshold saves, adopt these habits as context grows:
99+
100+
**Early session (< 30%)**:
101+
- Read files freely, explore broadly
102+
- Detailed explanations are fine
103+
104+
**Mid session (30-60%)**:
105+
- Prefer targeted reads (specific line ranges) over full files
106+
- Consolidate tool calls — do multiple checks in one call
107+
- Keep responses concise
108+
109+
**Late session (60%+)**:
110+
- Only read what's absolutely necessary
111+
- Reference previous reads by memory, don't re-read
112+
- Short, action-oriented responses
113+
- Proactively save any important findings to memory
114+
115+
### Platform-Specific Context Signals
116+
117+
| Platform | How to detect you're running out |
118+
|----------|----------------------------------|
119+
| **Claude Code** | System sends `<compacted>` message with conversation summary. If you see this, you've already been compressed — save immediately. |
120+
| **Cursor** | Chat may slow down or truncate earlier messages. No explicit signal. |
121+
| **Codex** | May start dropping early tool results from context. |
122+
| **Gemini CLI** | Has 1M context — less urgent but still track for very long sessions. |
123+
| **Cline/Roo** | VS Code may show token count in status bar. |
124+
125+
### What to Save at Each Threshold
126+
127+
| Threshold | Save scope |
128+
|-----------|-----------|
129+
| **50% checkpoint** | Current task progress, key decisions made, files modified, any gotchas discovered so far |
130+
| **75% full save** | Everything from checkpoint + lessons learned + what's left to do + handoff notes for next session |
131+
| **80%+ emergency** | Bare minimum: what was the goal, what's done, what's not done, any critical context that would be lost |
132+
133+
---
134+
41135
## Step 1: Detect Platform & Memory Path
42136

43137
### Platform Detection Table
@@ -268,3 +362,6 @@ Report with summary table:
268362
| Memory file > 50 lines | Compress: summarize, keep last 3 history entries |
269363
| Expired memory | Mark [STALE] in index, don't auto-delete |
270364
| Duplicate memories | Merge into more specific file, delete generic one |
365+
| Context auto-compacted | System compressed history — immediately save all important context to memory before more is lost |
366+
| User ignores 75% alert | Continue working but save on every significant milestone. Don't nag repeatedly. |
367+
| Very long session (100+ turns) | Likely past 50% on most platforms. Trigger checkpoint if not already done. |

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "session-wrap-skill",
3-
"version": "3.0.0",
3+
"version": "3.1.0",
44
"description": "Universal AI agent memory persistence — auto-save project context at session end. Works with Claude Code, Codex, Gemini CLI, Cursor, Windsurf, Cline, Roo Code, Copilot, Amp, OpenClaw, OpenHands, Amazon Q, and more.",
55
"keywords": [
66
"ai-agent",

0 commit comments

Comments
 (0)