Track and clean up all artifacts produced during a Claw-family agent session:
~/.workbuddy/skills/ or .workbuddy/skills/
Trigger words (start tracking):
开启清理追踪 | 启动会话清理 | session cleanup start | track session files |
开始记录临时文件 | enable cleanup tracking
On activation:
/.workbuddy/session-track.json with the following structure. If the file already exists from a previous interrupted session, load it and announce: "✅ 发现上一次会话的追踪记录,已恢复。"
```json
{
"started_at": "ISO-8601 timestamp",
"workspace": "path/to/workspace",
"items": {
"temp_files": [],
"pip_packages": [],
"npm_packages": [],
"system_software": [],
"skills": [],
"other": []
},
"skip_list": []
}
```
session-track.json using replace_in_file or write_to_file. This ensures the tracking log survives token overflow, session resets, or unexpected interruptions. Do NOT announce each individual append.
What to track — detection rules:
| Category | Detection signal |
|---|---|
| Temp files | write_to_file / execute_command that creates a file in a temp path (%TEMP%, /tmp, session scratch dirs, generated-images/), OR any file where the explanation contains keywords: "temporary", "demo", "test", "example", "scratch", "临时", "演示", "测试" |
| Pip packages | pip install, pip3 install, uv pip install |
| npm packages | npm install, npx ... --save |
| System software | winget install, choco install, apt install, brew install, scoop install |
| Skills | init_skill.py, use_skill, manually writing a new SKILL.md to a skills folder |
| Other | Any item the user explicitly asks to track |
Auto-tracking enforcement (critical — prevents missed items):
After EVERY tool call while tracking is active, perform a quick post-check on the tool call result. This is mandatory and must not be skipped:
write_to_file — Check if the filePath is in a temp/scratch/generated path OR if the explanation contains keywords: "temporary", "demo", "test", "example", "scratch", "临时", "演示", "测试". If yes → append to temp_files in session-track.json.
execute_command — Scan the command string for:
pip install / pip3 install / uv pip install → extract package name, append to pip_packages
npm install / npm i → extract package name, append to npm_packages
winget install / choco install / apt install / brew install / scoop install → extract package name, append to system_software
%TEMP%, /tmp, etc.) → append to temp_files
use_skill or init_skill.py** — If a new skill is being installed or initialized, append the skill path to skills.
other.
This post-check should be a silent, automatic step — do NOT announce it to the user. Simply update session-track.json in the background.
Trigger words (run cleanup):
清理垃圾文件 | 结束清理 | 清理对话文件 | cleanup now | clean session |
清理临时文件 | 会话清理 | 帮我清理 | 删除临时文件 | clean up
Cleanup workflow — follow these steps in order:
Read session-track.json from . Print the full tracked list as a Markdown checklist grouped by category. Format each category array as a checklist. Example output:
## 本次会话产生的资源清单
### 🗂 临时文件
- [ ] C:\Users\<user>\AppData\Local\Temp\demo_script.py
- [x] C:\Users\<user>\AppData\Local\Temp\keep_this.py (保留)
- [ ] /path/to/workspace/generated-images/test.png
### 📦 安装的 Python 包
- [ ] requests==2.31.0
### 🧩 安装的 Skills
- [ ] ~/.workbuddy/skills/my-test-skill/
If the tracked list is empty (excluding skip_list items), say: "📭 本次会话未检测到可清理的资源。" and stop.
When classifying, check skip_list in session-track.json — any item in skip_list should be excluded from cleanup and shown as "保留" in the summary.
For each tracked item, classify it as either:
delete_file (workspace files) or execute_command (non-workspace files). This gives step-by-step visibility and is token-efficient for small lists.
cleanup.py in two phases:
python "/scripts/cleanup.py" --track-file "/.workbuddy/session-track.json" --dry-run
python "/scripts/cleanup.py" --track-file "/.workbuddy/session-track.json"
This leverages the script's built-in safety checks, trash-first logic, and batch efficiency.
cleanup.py regardless of item count.
delete_file tool directly.
%TEMP%, /tmp, home directory) → use execute_command with a trash-first strategy:
```powershell
# Try Recycle Bin first (safe, reversible)
$shell = New-Object -ComObject Shell.Application
$item = $shell.NameSpace(0).ParseName('
$item.InvokeVerb('delete')
```
If the above fails or the item doesn't appear in Recycle Bin, fall back to:
Remove-Item "
osascript -e 'tell app "Finder" to delete POSIX file ""'
Fallback: rm "
gio trash "" or trash-put ""
Fallback: rm "
%TEMP%, /tmp), hard-delete is acceptable since these are OS-managed scratch spaces. For all other paths, always prefer trash.
For each Class B item, present a grouped confirmation prompt:
以下资源涉及依赖关系,需要您确认是否删除/卸载:
📦 Python 包:
• requests==2.31.0 → pip uninstall requests
🧩 Skills:
• ~/.workbuddy/skills/my-test-skill/ → 将永久删除
请回复:
全部删除 / 全部保留 / 逐一确认
For package uninstalls:
pip uninstall -y via execute_command
npm uninstall -g via execute_command
winget uninstall --id via execute_command
choco uninstall -y via execute_command
brew uninstall via execute_command
sudo apt remove -y via execute_command
scoop uninstall via execute_command
For Skills uninstall:
execute_command:
Remove-Item -Path "" -Recurse -Force
rm -rf ""
delete_file for skill directories as they are typically outside the workspace.
For system_software uninstall:
winget, try winget list first to find the exact ID before uninstalling.
After cleanup is complete, print a summary:
✅ 会话清理完成
已删除临时文件: N 项
已卸载 Python 包: N 项
已删除 Skills: N 项
跳过/保留: N 项
Reset the in-memory session log and delete .
| Command | Action |
|---|---|
| 列出临时文件 / show session log | Print current tracked list from session-track.json without deleting |
| 清除记录 / clear log | Delete via delete_file and reset tracking state. Does NOT delete any actual files. Note: The safety rule "never delete .workbuddy/ folder" protects the directory itself, not transient session files within it — session-track.json is explicitly designed to be deleted by this command. |
| 手动添加 / add to cleanup | Append a path to the relevant array in session-track.json |
| 跳过 / skip | Add an item to skip_list in session-track.json, excluding it from future cleanup. Accepts partial matches (e.g. 跳过 demo_script.py matches the full path). Confirm to user: "✅ 已标记保留:
跳过 command detail:
When the user says 跳过 :
session-track.json > items for entries containing as a substring.
skip_list, confirm to user.
列出临时文件 to review.
Items in skip_list are displayed with (保留) tag in the cleanup list and excluded from actual deletion/uninstall.
.workbuddy/ directory itself. Exception: session-track.json inside .workbuddy/ is a transient session file and may be deleted by the 清除记录 command or Step E cleanup.
rm -rf or del /S /Q on home, desktop, downloads, or project roots.
scripts/cleanup.py — Command-line cleanup utility; can be run directly to remove a list of paths.
references/cleanup-guide.md — Full reference of uninstall commands by package manager and OS.
track_init() shallow copy bug — nested items dict now uses copy.deepcopy() to prevent cross-session reference sharing (P2-6)
uninstall_pip() / uninstall_npm() now pre-check whether the package is actually installed before attempting uninstall (P2-9)
Path("/") resolving to \ on Windows, incorrectly flagging all paths as unsafe
skip_list partial matching — added _is_skipped() helper for substring-based matching
send_to_trash() silent hard-delete fallback — now requires allow_hard_delete=True (only set for temp paths)
--workspace CLI argument for --manifest mode
ALLOWED_SUBDIRS list for workspace-aware safety checks (generated-images, node_modules)
session-track.json
(保留) skip marker in Step A example output
--init, --add, --skip-add, --show, --dry-run
共 1 个版本