# Heartbeat

This file runs every 30 minutes. Most tasks are silent. Do not be noisy.

## ⚠️ ABSOLUTE RULE: SKIP = SILENCE

**If a task's gate condition is not met (wrong hour, cooldown not expired, no new data), you SKIP it. Skipping means:**
- Do NOT send a Discord message saying you skipped
- Do NOT explain why you skipped
- Do NOT mention the task at all
- Just move to the next task or reply HEARTBEAT_OK

**Violating this rule wastes the team's attention. Sam has asked 12+ times. This is final.**

## 1. Memory Sync (every run)

Check for uncommitted changes in memory files (`MEMORY.md`, `memory/`, `USER.md`). If changes exist:

1. `git fetch origin main` and check if local main is behind remote
2. If behind: `git stash`, `git pull --rebase origin main`, `git stash pop`. If conflicts arise, create a PR instead of pushing directly.
3. Stage only memory files: `MEMORY.md`, `memory/`, `USER.md`
4. Commit with message: `chore: sync memory [YYYY-MM-DD]`
5. Push directly to main

Only auto-push memory files. Never auto-push `SOUL.md`, `AGENTS.md`, `TOOLS.md`, `IDENTITY.md` -- those always go through PRs.

Silent. No Discord message.

## 2. Scheduled Reminders (every run)

Check `memory/` files for any entries matching `## Reminder:` with status `PENDING`. For each:

1. Parse the **When** field for the target UTC hour.
2. Compare against the current UTC hour (`date -u +%H`). If the current hour matches, send the reminder message to the appropriate Discord channel and update the status from `PENDING` to `DONE`.
3. If the hour hasn't arrived yet, skip silently.

Silent if no reminders are due.

## 3. Infrastructure Monitoring (every run)

Run `df -h /` and `mcporter call x402 get_wallet_info` to check disk and wallet.

For RAM: read `/tmp/craig-ram-peak` which tracks the highest RAM % since last reset (sampled every 15s by `craig-ram-monitor.service`). After reading, reset it: `echo "0" > /tmp/craig-ram-peak`.

Only alert if:
- Disk usage > 70%
- Peak RAM usage > 75% (since last heartbeat)
- x402 wallet balance < $2.00

If any threshold is breached: send one short message to Discord with the number(s) and timestamp. If all fine: say nothing.

## 4. Email Check (once daily)

**Gate:** Check the current UTC hour (`date -u +%H`). Only run during hour **14** (10am ET). If other hour, skip.

Check both inboxes for new messages:
1. Subdomain inbox: `mcporter call x402.fetch url="https://stableemail.dev/api/subdomain/inbox/messages" method=POST body='{"subdomain":"craig","localPart":"craig"}'`
2. Shared inbox: `mcporter call x402.fetch url="https://stableemail.dev/api/inbox/messages" method=POST body='{"username":"craig"}'`

For each message, check if its `messageId` is already in `~/.craig-notified-emails`. If NOT already notified:
1. Read the full message via the appropriate `/messages/read` endpoint
2. Post a short summary to Discord #craig channel: who it's from, subject, 1-line preview
3. Append the `messageId` to `~/.craig-notified-emails` (one per line)

Never notify about the same email twice. Silent if no new emails.

## 5. Proactive Work (twice daily)

**Gate:** Check the current UTC hour (`date -u +%H`). Only run this section during hours **10** and **22** (10am and 10pm UTC). If it's any other hour, skip entirely. Also check `~/.craig-last-proactive` -- if the file's timestamp is less than 10 hours old, skip. After running, `touch ~/.craig-last-proactive`.

**Budget:** Up to $0.50 in x402 calls per run.

**Goal:** Find the single highest-leverage thing you could do for the Merit Systems team right now, then go do it.

### Research phase (do all of these, then synthesize)

1. **Recent PRs:** Run `gh pr list --repo Merit-Systems --state merged --limit 10` across the org's active repos. Understand what the team is actively building and shipping.
2. **Social signals:** Use x402 to search Twitter/X for recent posts by team members (sragss and other Merit-Systems contributors). Look at what they're talking about, excited about, or frustrated by.
3. **Memories and context:** Read your `MEMORY.md` and recent `memory/` logs. What have people asked you about recently? What patterns do you see?
4. **Open issues:** Scan for open issues across Merit-Systems repos that are unassigned or stale.

### Decision phase

From your research, identify ONE concrete action that would be genuinely useful. Prioritize:
- Fixing a bug someone mentioned but nobody addressed
- Writing code for an open issue that aligns with current work
- Researching something the team needs to make a decision on
- Preparing a summary or analysis that saves someone time

Do NOT prioritize:
- Cosmetic changes, README tweaks, or trivial refactors
- Anything that would surprise the team in a bad way
- Work on repos you don't understand well enough

### Execution phase

Go do the thing. Clone the repo, write the code, create the PR -- or do the research and compile your findings.

### Self-review (be brutally honest)

Before presenting to the team, ask yourself:
- Would a senior engineer find this genuinely useful, or is this busywork?
- Does this save someone real time or unblock real work?
- Is the quality high enough that a human wouldn't need to redo it?

If the answer to any of these is no, discard the work. Do not present it. Write a note in your `memory/` log about what you tried and why it wasn't good enough. Reply HEARTBEAT_OK.

### Present (only if it passed self-review)

Send a short message to Discord:
- What you did and why
- Link to the PR or a brief summary of findings
- 2-3 sentences max. No fluff.