Files
karuo-ai/01_卡资(金)/金仓_存储备份/脚本/cursor_maintenance.sh
2026-07-19 00:21:46 +08:00

100 lines
4.3 KiB
Bash
Executable File
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env bash
# Cursor 进程/磁盘维护 — 卡若AI 金仓
# 用法:
# bash cursor_maintenance.sh # 仅诊断报告
# bash cursor_maintenance.sh --cache # 安全清缓存+快照Cursor 可开着)
# bash cursor_maintenance.sh --online # 安全在线瘦身(不删 bubbleId
# bash cursor_maintenance.sh --stabilize # code5 稳定化保留最近120条气泡+清缓存
# bash cursor_maintenance.sh --deep # 深度清理(须 Cmd+Q 退出 Cursor
set -euo pipefail
MODE="${1:---report}"
CURSOR_SUPPORT="$HOME/Library/Application Support/Cursor"
STATE_DB="$CURSOR_SUPPORT/User/globalStorage/state.vscdb"
SNAPSHOTS_DIR="$CURSOR_SUPPORT/snapshots"
SLIM_DB="$HOME/Documents/个人/卡若AI/运营中枢/参考资料/scripts/cursor_slim_db.sh"
ONLINE_SLIM="$HOME/Documents/个人/卡若AI/01_卡资/金仓_存储备份/脚本/cursor_online_slim.py"
STABILIZE="$HOME/Documents/个人/卡若AI/01_卡资/金仓_存储备份/脚本/cursor_code5_stabilize.py"
report() {
echo "═══════════════════════════════════════════════"
echo "📊 Cursor 诊断 $(date '+%Y-%m-%d %H:%M')"
echo "═══════════════════════════════════════════════"
local wcount hcount tmb
wcount="$(osascript -e 'tell application "System Events" to tell process "Cursor" to count windows' 2>/dev/null || echo 0)"
hcount="$(pgrep -lf 'extension-host' 2>/dev/null | wc -l | tr -d ' ')"
tmb="$(du -sm "$CURSOR_SUPPORT" 2>/dev/null | awk '{print $1}')"
echo " 窗口数 : ${wcount} (每窗约 4 个 extension-host)"
echo " extension-host : ${hcount}"
echo " Cursor 总占用 : ${tmb} MB (~$((tmb/1024)) GB)"
if [ -f "$STATE_DB" ]; then
echo " state.vscdb : $(du -sh "$STATE_DB" | awk '{print $1}') [主因]"
fi
if [ -d "$SNAPSHOTS_DIR" ]; then
echo " snapshots/ : $(du -sh "$SNAPSHOTS_DIR" | awk '{print $1}')"
fi
echo " workspaceStorage : $(du -sh "$CURSOR_SUPPORT/User/workspaceStorage" | awk '{print $1}') ($(ls "$CURSOR_SUPPORT/User/workspaceStorage" 2>/dev/null | wc -l | tr -d ' ') 个工作区)"
echo ""
echo " 进程内存 TOP5"
ps aux | rg -i "Cursor Helper" | rg -v rg | awk '{printf " %6sMB %s\n", int($6/1024), substr($0, index($0,$11))}' | sort -k1 -nr | head -5
echo ""
echo " 进程多 = 正常架构; 4 窗 x 4 host 约 16+ 进程"
echo " retrieval 占 RAM 最大; 深度清理: Cmd+Q 后 bash $0 --deep"
}
cache_clean() {
echo "🧹 清 GPU/浏览器缓存..."
rm -rf "$CURSOR_SUPPORT/GPUCache" "$CURSOR_SUPPORT/Cache" "$CURSOR_SUPPORT/CachedData" "$CURSOR_SUPPORT/Code Cache" 2>/dev/null || true
if [ -d "$SNAPSHOTS_DIR" ]; then
echo "🧹 清索引快照 snapshots/Cursor 可开着,会按需重建)..."
for d in codebases roots stores; do
[ -d "$SNAPSHOTS_DIR/$d" ] && rm -rf "$SNAPSHOTS_DIR/$d" && mkdir -p "$SNAPSHOTS_DIR/$d"
done
fi
echo " 不自动删 state.vscdb.bak.*(防误删可恢复备份;须手动确认后再删)"
echo "✅ 缓存+快照已清"
}
online_slim() {
if [ ! -f "$ONLINE_SLIM" ]; then
echo "❌ 未找到 $ONLINE_SLIM"; exit 1
fi
echo "🧹 安全在线瘦身(保留 bubbleId对话不消失..."
python3 "$ONLINE_SLIM"
}
safe_online_slim() {
online_slim
}
stabilize() {
if [ ! -f "$STABILIZE" ]; then
echo "❌ 未找到 $STABILIZE"; exit 1
fi
echo "🛡️ code5 稳定化最近120条保留气泡其余仅列表..."
python3 "$STABILIZE" --keep 120
}
deep_clean() {
if pgrep -f "Cursor" >/dev/null 2>&1; then
echo "❌ 请先 Cmd+Q 完全退出 Cursor再执行 --deep"
exit 1
fi
if [ ! -f "$SLIM_DB" ]; then
echo "❌ 未找到 $SLIM_DB"
exit 1
fi
echo "🧹 深度瘦身 state.vscdb保留 agent-transcripts jsonl..."
bash "$SLIM_DB"
}
case "$MODE" in
--report|report) report ;;
--cache|cache) cache_clean; report ;;
--online|online) online_slim; report ;;
--safe-online|safe) safe_online_slim; report ;;
--stabilize|stabilize) stabilize; cache_clean; report ;;
--deep|deep) deep_clean; report ;;
*) echo "用法: $0 [--report|--cache|--online|--stabilize|--safe-online|--deep]"; exit 1 ;;
esac