-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgroundhog
More file actions
executable file
Β·423 lines (361 loc) Β· 13.9 KB
/
groundhog
File metadata and controls
executable file
Β·423 lines (361 loc) Β· 13.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#!/bin/bash
# πΏοΈ Groundhog Day β same thing, every time.
# Auto-syncs ~/.copilot/skills/ β DUBSOpenHub/copilot-skills
#
# Modes:
# groundhog β one-shot sync
# groundhog watch β real-time file watcher (runs forever)
# groundhog status β health check
#
# Runs as macOS LaunchAgent: com.dubsopenhub.groundhog
set -euo pipefail
REPO="$HOME/dev/copilot-skills"
SKILLS="$HOME/.copilot/skills"
LOG="$HOME/.groundhog.log"
LOCKFILE="/tmp/groundhog.lock"
DEBOUNCE_SECS=5
MAX_LOG_BYTES=1048576 # 1MB
# ββ Logging ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
log() { echo "$(date): $*" >> "$LOG"; }
rotate_log() {
if [[ -f "$LOG" ]]; then
local size
size=$(stat -f%z "$LOG" 2>/dev/null || echo 0)
if [[ "$size" -gt "$MAX_LOG_BYTES" ]]; then
mv "$LOG" "${LOG}.old"
log "log rotated (was ${size} bytes)"
fi
fi
}
# ββ Repo health ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
validate_repo() {
cd "$REPO" || { log "FATAL: repo dir $REPO missing"; return 1; }
git rev-parse --is-inside-work-tree >/dev/null 2>&1 || { log "FATAL: not a git repo"; return 1; }
if [[ -d .git/rebase-merge ]] || [[ -d .git/rebase-apply ]]; then
log "FATAL: repo in rebase state β manual fix needed"; return 1
fi
if ! git diff --quiet 2>/dev/null; then
log "WARN: dirty working tree, resetting"
git checkout -- . 2>>"$LOG"
fi
return 0
}
validate_skills() {
[[ -d "$SKILLS" ]] || { log "ERROR: skills dir $SKILLS missing"; return 1; }
local count
count=$(find "$SKILLS" -name 'SKILL.md' 2>/dev/null | wc -l | tr -d ' ')
[[ "$count" -gt 0 ]] || { log "WARN: no skills found, skipping sync"; return 1; }
return 0
}
# ββ Push with retry ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
push_with_retry() {
local attempts=3
for i in $(seq 1 $attempts); do
git push >>"$LOG" 2>&1 && return 0
log "push attempt $i/$attempts failed"
sleep $((2 ** i))
done
osascript -e 'display notification "Git push failed after 3 attempts" with title "πΏοΈ Groundhog Day"' 2>/dev/null || true
return 1
}
# ββ Sync lock ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
acquire_lock() { mkdir "$LOCKFILE" 2>/dev/null; }
release_lock() { rmdir "$LOCKFILE" 2>/dev/null || true; }
# ββ Core sync ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
do_sync() {
rotate_log
validate_skills || return 0
validate_repo || return 1
cd "$REPO" || return 1
rsync -a --delete \
--exclude='.git' --exclude='README.md' \
--exclude='.DS_Store' --exclude='__pycache__' \
--exclude='node_modules' --exclude='.env' \
"$SKILLS"/ "$REPO"/ 2>>"$LOG"
generate_readme
git add -A
if ! git diff --cached --quiet; then
local msg
msg="sync skills $(date +%Y-%m-%d-%H%M)"
local new_skills
new_skills=$(git diff --cached --name-only --diff-filter=A | grep 'SKILL.md' | sed 's|/SKILL.md||' | sort -u || true)
if [ -n "$new_skills" ]; then
msg="add skill: $(echo "$new_skills" | tr '\n' ', ' | sed 's/,$//')"
fi
git commit -m "$msg" >>"$LOG" 2>&1
git pull --rebase --quiet >>"$LOG" 2>&1 || {
log "ERROR: pull --rebase failed, manual fix needed"
return 1
}
push_with_retry
log "πΏοΈ synced β $msg"
echo "πΏοΈ Groundhog Day synced: $msg"
fi
}
do_sync_locked() {
if ! acquire_lock; then
# Clear stale locks older than 5 minutes
local lock_age
lock_age=$(( $(date +%s) - $(stat -f%m "$LOCKFILE" 2>/dev/null || echo "0") ))
if [[ "$lock_age" -gt 300 ]]; then
log "WARN: clearing stale lock (${lock_age}s old)"
release_lock
acquire_lock || return 0
else
return 0
fi
fi
trap 'release_lock' RETURN
do_sync
}
# ββ README generation ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
generate_readme() {
cd "$REPO" || return
cat > README.md << 'HEADER'
# Copilot CLI Skills
A collection of skills for [GitHub Copilot CLI](https://docs.github.com/copilot/concepts/agents/about-copilot-cli) that extend the agent with specialized workflows.
## Skills
| Skill | Description |
|-------|-------------|
HEADER
python3 - "$REPO" >> README.md 2>/dev/null << 'PYSCRIPT'
import re, sys, os
repo = sys.argv[1]
for name in sorted(os.listdir(repo)):
sf = os.path.join(repo, name, "SKILL.md")
if not os.path.isfile(sf):
continue
text = open(sf).read()
m = re.search(r'description:\s*>\s*\n((?:\s+.*\n)*)', text)
if m:
desc = ' '.join(m.group(1).strip().split())
else:
m = re.search(r'description:\s*(.*)', text)
desc = m.group(1).strip().strip('"') if m else '\u2014'
if len(desc) > 120:
desc = desc[:117] + '...'
print(f'| [{name}]({name}/) | {desc} |')
PYSCRIPT
cat >> README.md << 'FOOTER'
## Install
### Install all skills
```bash
git clone https://github.com/DUBSOpenHub/copilot-skills.git /tmp/copilot-skills
cp -R /tmp/copilot-skills/*/ ~/.copilot/skills/
```
### Install a single skill
```bash
git clone --depth 1 https://github.com/DUBSOpenHub/copilot-skills.git /tmp/copilot-skills
cp -R /tmp/copilot-skills/pitch-master ~/.copilot/skills/
```
After installing, restart your Copilot CLI session (`/exit` then `copilot`) to pick up the new skills. Use `/skills` to verify they loaded.
## How skills work
Each skill is a folder containing a `SKILL.md` file with YAML frontmatter (name, description) and markdown prompt instructions. Copilot CLI loads skills from `~/.copilot/skills/` on startup.
## License
MIT
FOOTER
}
# ββ Watch mode βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
watch_mode() {
trap 'log "πΏοΈ shutting down"; kill 0; exit 0' SIGTERM SIGINT
echo "πΏοΈ Groundhog Day is watching... same thing, every time."
log "πΏοΈ watcher started (PID $$)"
fswatch -r -l "$DEBOUNCE_SECS" \
--event Created --event Updated --event Removed --event Renamed \
"$SKILLS" | while read -r _event; do
# Brief pause to let batched events settle
sleep 1
log "πΏοΈ change detected"
do_sync_locked
done
}
# ββ Status βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
cmd_status() {
if launchctl list 2>&1 | grep -q "com.dubsopenhub.groundhog"; then
echo "πΏοΈ Groundhog Day: β
Running"
elif pgrep -f "groundhog watch" >/dev/null 2>&1; then
echo "πΏοΈ Groundhog Day: β
Running (manual)"
else
echo "πΏοΈ Groundhog Day: β Not running"
fi
echo ""
if [[ -f "$LOG" ]]; then
echo "Last 5 log entries:"
tail -5 "$LOG"
else
echo "No log file found."
fi
}
# ββ Nightly checkup βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
cmd_checkup() {
local issues=0
local report=""
local CHECKUP_LOG="$HOME/.groundhog-checkup.log"
report+="πΏοΈ Groundhog Day β Nightly Checkup\n"
report+="$(date)\n"
report+="ββββββββββββββββββββββββββββββββββββ\n\n"
# 1. Process health
if pgrep -f "groundhog watch" >/dev/null 2>&1; then
report+="β
Watcher process: running\n"
else
report+="β Watcher process: NOT RUNNING\n"
issues=$((issues + 1))
fi
# 2. LaunchAgent registered
if launchctl list 2>/dev/null | grep -q "com.dubsopenhub.groundhog" 2>/dev/null; then
report+="β
LaunchAgent: loaded\n"
elif pgrep -f "groundhog watch" >/dev/null 2>&1; then
report+="β
LaunchAgent: running (manual)\n"
else
report+="β LaunchAgent: not loaded\n"
issues=$((issues + 1))
fi
# 3. Skills directory exists and has skills
if [[ -d "$SKILLS" ]]; then
local skill_count
skill_count=$(find "$SKILLS" -name 'SKILL.md' 2>/dev/null | wc -l | tr -d ' ')
report+="β
Skills directory: $skill_count skills found\n"
else
report+="β Skills directory: MISSING ($SKILLS)\n"
issues=$((issues + 1))
fi
# 4. Repo state
if [[ -d "$REPO/.git" ]]; then
cd "$REPO"
if git diff --quiet 2>/dev/null && git diff --cached --quiet 2>/dev/null; then
report+="β
Repo state: clean\n"
else
report+="β οΈ Repo state: dirty (uncommitted changes)\n"
issues=$((issues + 1))
fi
# Check if local is ahead of remote
git fetch --quiet 2>/dev/null || true
local ahead
ahead=$(git rev-list --count origin/main..HEAD 2>/dev/null || echo "0")
if [[ "$ahead" -gt 0 ]]; then
report+="β οΈ Unpushed commits: $ahead\n"
issues=$((issues + 1))
else
report+="β
Remote: in sync\n"
fi
# Check for rebase state
if [[ -d .git/rebase-merge ]] || [[ -d .git/rebase-apply ]]; then
report+="β Repo: stuck in rebase\n"
issues=$((issues + 1))
fi
else
report+="β Repo: MISSING ($REPO)\n"
issues=$((issues + 1))
fi
# 5. Recent push failures (last 7 days only)
if [[ -f "$LOG" ]]; then
local recent_failures=0
local cutoff_epoch
cutoff_epoch=$(date -v-7d +%s 2>/dev/null || date -d '7 days ago' +%s 2>/dev/null || echo "0")
local line
while IFS= read -r line; do
# Log format: "Wed Mar 11 19:13:33 PDT 2026: ..."
local line_date
line_date=$(echo "$line" | sed 's/: .*//' | head -1)
local line_epoch
line_epoch=$(date -j -f "%a %b %d %T %Z %Y" "$line_date" +%s 2>/dev/null || echo "0")
if [[ "$line_epoch" -ge "$cutoff_epoch" ]]; then
recent_failures=$((recent_failures + 1))
fi
done < <(grep "push.*failed\|ERROR\|FATAL" "$LOG" 2>/dev/null || true)
if [[ "$recent_failures" -gt 0 ]]; then
report+="β οΈ Log errors (7d): $recent_failures found\n"
local last_error
last_error=$(grep "push.*failed\|ERROR\|FATAL" "$LOG" | tail -1)
report+=" Last: $last_error\n"
issues=$((issues + 1))
else
report+="β
Log: no recent errors (7d)\n"
fi
# Log size
local log_size
log_size=$(stat -f%z "$LOG" 2>/dev/null || echo "0")
local log_kb=$((log_size / 1024))
report+="β
Log size: ${log_kb}KB\n"
# Last sync time
local last_sync
last_sync=$(grep "synced" "$LOG" | tail -1 | cut -d: -f1-3)
if [[ -n "$last_sync" ]]; then
report+="β
Last sync: $last_sync\n"
else
report+="β οΈ Last sync: never\n"
issues=$((issues + 1))
fi
else
report+="β οΈ Log file: not found\n"
issues=$((issues + 1))
fi
# 6. fswatch dependency
if command -v fswatch >/dev/null 2>&1; then
report+="β
fswatch: installed\n"
else
report+="β fswatch: MISSING\n"
issues=$((issues + 1))
fi
# 7. Copilot CLI check
if command -v copilot >/dev/null 2>&1; then
local cli_version
cli_version=$(copilot --version 2>/dev/null | head -1 || echo "unknown")
report+="β
Copilot CLI: installed ($cli_version)\n"
else
report+="β οΈ Copilot CLI: not installed\n"
issues=$((issues + 1))
fi
# 8. Stale lock file
if [[ -d "$LOCKFILE" ]]; then
local lock_age
lock_age=$(( $(date +%s) - $(stat -f%m "$LOCKFILE" 2>/dev/null || echo "0") ))
if [[ "$lock_age" -gt 300 ]]; then
report+="β οΈ Stale lock: $LOCKFILE (${lock_age}s old, removing)\n"
rmdir "$LOCKFILE" 2>/dev/null || true
issues=$((issues + 1))
else
report+="β
Sync lock: active (${lock_age}s old)\n"
fi
else
report+="β
Sync lock: clear\n"
fi
# 9. Shell config integrity (.zshrc)
if [[ -f "$HOME/.zshrc" ]]; then
if zsh -n "$HOME/.zshrc" 2>/dev/null; then
report+="β
Shell config: .zshrc syntax valid\n"
else
report+="β οΈ Shell config: .zshrc has syntax errors!\n"
report+=" β Run: zsh -n ~/.zshrc to diagnose\n"
issues=$((issues + 1))
fi
fi
# 10. Daily sync β ensure skills are backed up even if fswatch missed changes
log "πΏοΈ daily sync triggered by checkup"
do_sync_locked
report+="β
Daily sync: completed\n"
report+="\nββββββββββββββββββββββββββββββββββββ\n"
if [[ "$issues" -eq 0 ]]; then
report+="πΏοΈ All clear. Groundhog Day is healthy.\n"
else
report+="π¨ $issues issue(s) found. Run grid-medic:\n"
report+=" β Open Copilot CLI and say:\n"
report+=" \"grid-medic diagnose ~/bin/groundhog\"\n"
fi
# Output
echo -e "$report"
# Save to checkup log
echo -e "$report" >> "$CHECKUP_LOG"
# macOS notification if issues found
if [[ "$issues" -gt 0 ]] && [[ "$(uname)" == "Darwin" ]]; then
osascript -e "display notification \"$issues issue(s) found. Run: groundhog checkup\" with title \"πΏοΈ Groundhog Day Checkup\"" 2>/dev/null || true
fi
exit 0
}
# ββ Main βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
case "${1:-}" in
watch) watch_mode ;;
status) cmd_status ;;
checkup) cmd_checkup ;;
*) do_sync_locked ;;
esac