Skip to content

Commit e04ad2d

Browse files
committed
feat(opencode): add opencode-gc maintenance script
Reclaims unbounded growth in ~/.local/share/opencode: superseded event rows in opencode.db (keep-latest per message/part) plus VACUUM, interrupted-repack tmp_pack garbage and unreachable packs in snapshot repos, stale tool-output files, oversized logs. Refs anomalyco/opencode#31526 #33356 #36093
1 parent 3324638 commit e04ad2d

1 file changed

Lines changed: 138 additions & 0 deletions

File tree

‎common/.local/bin/opencode-gc‎

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
#!/usr/bin/env bash
2+
# opencode-gc — periodic maintenance for the opencode data dir.
3+
#
4+
# Reclaims disk from known unbounded-growth paths (upstream: anomalyco/opencode
5+
# #31526 auto_vacuum off, #33356 unbounded event journal, #36093 snapshot GC):
6+
# 1. snapshot repos: remove tmp_pack_* garbage, git gc --prune=now when large
7+
# 2. opencode.db: delete superseded event rows (keep latest per message/part),
8+
# then VACUUM when freelist is significant
9+
# 3. tool-output: delete files older than TOOL_OUTPUT_DAYS
10+
# 4. log: truncate when > LOG_MAX_MB
11+
#
12+
# Usage: opencode-gc [--dry-run]
13+
# Env: OPENCODE_DATA, TOOL_OUTPUT_DAYS (default 14), LOG_MAX_MB (default 100),
14+
# GC_MIN_MB (default 100), VACUUM_MIN_MB (default 1024)
15+
16+
set -euo pipefail
17+
18+
DRY_RUN=0
19+
for arg in "$@"; do
20+
case "$arg" in
21+
--dry-run) DRY_RUN=1 ;;
22+
*) echo "usage: opencode-gc [--dry-run]" >&2; exit 2 ;;
23+
esac
24+
done
25+
26+
DATA_DIR="${OPENCODE_DATA:-$HOME/.local/share/opencode}"
27+
DB="$DATA_DIR/opencode.db"
28+
TOOL_OUTPUT_DAYS="${TOOL_OUTPUT_DAYS:-14}"
29+
LOG_MAX_MB="${LOG_MAX_MB:-100}"
30+
GC_MIN_MB="${GC_MIN_MB:-100}"
31+
VACUUM_MIN_MB="${VACUUM_MIN_MB:-1024}"
32+
BATCH=25000
33+
34+
run() {
35+
if [ "$DRY_RUN" = 1 ]; then echo "[dry-run] $*"; else "$@"; fi
36+
}
37+
38+
echo "opencode-gc: $DATA_DIR ($(du -sh "$DATA_DIR" 2>/dev/null | cut -f1)) $(date '+%F %T')"
39+
if pgrep -x opencode >/dev/null 2>&1; then
40+
echo "note: opencode instances running — writes are WAL-safe, VACUUM may wait on locks"
41+
fi
42+
43+
# --- 1. snapshot repos ------------------------------------------------------
44+
shopt -s nullglob
45+
for snaprepo in "$DATA_DIR"/snapshot/*/*/; do
46+
[ -f "${snaprepo}HEAD" ] || continue
47+
# interrupted-repack garbage (git never consumes tmp_pack_* files)
48+
while IFS= read -r -d '' f; do
49+
echo "snapshot garbage: $f ($(du -h "$f" | cut -f1))"
50+
run rm -f "$f"
51+
done < <(find "$snaprepo/objects" -name 'tmp_pack_*' -type f -print0 2>/dev/null)
52+
# gc only when the object store is large; refs (active snapshot history) are kept
53+
mb=$(du -sm "${snaprepo}objects" 2>/dev/null | cut -f1)
54+
if [ "${mb:-0}" -ge "$GC_MIN_MB" ]; then
55+
echo "snapshot gc: $snaprepo (${mb}M)"
56+
run git --git-dir="$snaprepo" reflog expire --expire=now --expire-unreachable=now --all
57+
run git --git-dir="$snaprepo" gc --prune=now
58+
fi
59+
done
60+
shopt -u nullglob
61+
62+
# --- 2. db: prune superseded events, keep latest per message/part -----------
63+
if [ -f "$DB" ]; then
64+
to_delete=$(sqlite3 "$DB" "
65+
SELECT COUNT(*) FROM (
66+
SELECT ROW_NUMBER() OVER (
67+
PARTITION BY type, aggregate_id,
68+
CASE type WHEN 'message.updated.1' THEN json_extract(data,'\$.info.id')
69+
WHEN 'message.part.updated.1' THEN json_extract(data,'\$.part.id') END
70+
ORDER BY seq DESC) rn
71+
FROM event WHERE type IN ('message.updated.1','message.part.updated.1')
72+
) WHERE rn > 1;" 2>/dev/null || echo 0)
73+
echo "db: superseded event rows to delete: ${to_delete}"
74+
75+
if [ "${to_delete:-0}" -gt 0 ] && [ "$DRY_RUN" != 1 ]; then
76+
{
77+
echo "PRAGMA busy_timeout=60000;"
78+
echo "CREATE TEMP TABLE del_ids AS WITH base AS (
79+
SELECT id, type, aggregate_id,
80+
CASE type WHEN 'message.updated.1' THEN json_extract(data,'\$.info.id')
81+
WHEN 'message.part.updated.1' THEN json_extract(data,'\$.part.id') END AS obj_id,
82+
seq
83+
FROM event WHERE type IN ('message.updated.1','message.part.updated.1'))
84+
SELECT id FROM (SELECT id, ROW_NUMBER() OVER (
85+
PARTITION BY type, aggregate_id, obj_id ORDER BY seq DESC) rn FROM base) WHERE rn>1;"
86+
n=$(( (to_delete + BATCH - 1) / BATCH + 2 ))
87+
i=0
88+
while [ "$i" -lt "$n" ]; do
89+
echo "DELETE FROM event WHERE id IN (SELECT id FROM del_ids WHERE rowid IN (SELECT rowid FROM del_ids ORDER BY rowid LIMIT $BATCH));"
90+
echo "DELETE FROM del_ids WHERE rowid IN (SELECT rowid FROM del_ids ORDER BY rowid LIMIT $BATCH);"
91+
i=$((i + 1))
92+
done
93+
} | sqlite3 "$DB" >/dev/null
94+
echo "db: deleted ${to_delete} superseded event rows"
95+
fi
96+
97+
# VACUUM only if there is meaningful free space to reclaim and headroom for it
98+
if [ "$DRY_RUN" != 1 ]; then
99+
dead_mb=$(sqlite3 "$DB" "SELECT CAST(ROUND(page_size*freelist_count/1048576.0) AS INTEGER) FROM pragma_page_size(), pragma_freelist_count();" 2>/dev/null || echo 0)
100+
db_kb=$(du -k "$DB" | cut -f1)
101+
free_kb=$(df -k "$DATA_DIR" | awk 'NR==2 {print $4}')
102+
if [ "${dead_mb:-0}" -ge "$VACUUM_MIN_MB" ] && [ "$free_kb" -gt $((db_kb * 2)) ]; then
103+
echo "db: VACUUM (dead=${dead_mb}M, file=$(du -h "$DB" | cut -f1))"
104+
sqlite3 "$DB" "PRAGMA busy_timeout=300000; PRAGMA wal_checkpoint(TRUNCATE); VACUUM; PRAGMA integrity_check;" >/dev/null
105+
echo "db: vacuumed -> $(du -h "$DB" | cut -f1)"
106+
else
107+
echo "db: VACUUM skipped (dead=${dead_mb:-0}M < ${VACUUM_MIN_MB}M or low disk headroom)"
108+
fi
109+
else
110+
dead_mb=$(sqlite3 "file:$DB?mode=ro" "SELECT CAST(ROUND(page_size*freelist_count/1048576.0) AS INTEGER) FROM pragma_page_size(), pragma_freelist_count();" 2>/dev/null || echo 0)
111+
echo "[dry-run] would VACUUM if dead=${dead_mb:-0}M >= ${VACUUM_MIN_MB}M"
112+
fi
113+
else
114+
echo "db: $DB not found, skipping"
115+
fi
116+
117+
# --- 3. tool-output: age-based cleanup ---------------------------------------
118+
if [ -d "$DATA_DIR/tool-output" ]; then
119+
echo "tool-output: removing files older than ${TOOL_OUTPUT_DAYS}d"
120+
if [ "$DRY_RUN" = 1 ]; then
121+
find "$DATA_DIR/tool-output" -type f -mtime +"$TOOL_OUTPUT_DAYS" -print
122+
else
123+
find "$DATA_DIR/tool-output" -type f -mtime +"$TOOL_OUTPUT_DAYS" -print -delete
124+
fi
125+
fi
126+
127+
# --- 4. log truncation --------------------------------------------------------
128+
shopt -s nullglob
129+
for lg in "$DATA_DIR"/log/*.log; do
130+
mb=$(( $(wc -c <"$lg") / 1024 / 1024 ))
131+
if [ "$mb" -ge "$LOG_MAX_MB" ]; then
132+
echo "log: $lg (${mb}M) > ${LOG_MAX_MB}M"
133+
run : >"$lg"
134+
fi
135+
done
136+
shopt -u nullglob
137+
138+
echo "opencode-gc done: $DATA_DIR ($(du -sh "$DATA_DIR" 2>/dev/null | cut -f1))"

0 commit comments

Comments
 (0)