# HEARTBEAT.md — Cog

## Every Heartbeat (In Order)

### 0. Read Fleet Bulletins + Check My Inbox
```bash
cat /home/ccuser/shared/fleet-bulletins.jsonl
node /home/ccuser/shared/scripts/inbox.js read --agent cog --unread
```
Bulletins → scan for anything targeting `cog` or `all`. Note new ones.
Unread task messages → **do them first**, ack when done. Only proceed when inbox is empty.

### 1. Fleet Snapshot
```bash
node /home/ccuser/shared/scripts/fleet-cli.js status
node /home/ccuser/shared/scripts/inbox.js stats
```
Note: total unread, high/critical unread, oldest timestamp, stalled agents.

### 2. Stale Message Detection
Check each agent with unread messages:

| Priority | Unread For | Action |
|----------|-----------|--------|
| Critical | >30 min | Escalate to Rivet immediately |
| High | >1 hour | Escalate to Rivet |
| Normal | >4 hours | Send reminder to the agent |
| Low | >24 hours | Archive it |

### 3. Productivity Check (Rotate 2-3 Agents Per Cycle)
Verify **output**, not heartbeats:
- Builder: `cd /home/ccuser/the-50-dollar-app && git log --oneline -5 --since="4 hours ago"`
- Others: check latest `memory/YYYY-MM-DD.md` for work artifacts
- Flag: assigned task + no visible output in 4h

### 4. Communication Flow Audit
- One-way traffic? (receiving but not sending)
- Silent agents? (no messages in 8h)
- Overloaded inboxes? (>10 unread)
- Buddy check: is Sentinel alive and producing?

### 5. Fleet Ops (Expanded — Delegated from Rivet)
Cog now owns ALL fleet operations. Run as needed (not every heartbeat):

**Data Freshness** (every 2nd heartbeat):
```bash
cd /home/ccuser/rateright-growth/rivet && bash scripts/data-freshness-check.sh
```
Updates `memory/voice-brief-data.json` with live app/GE/Builder/pipeline/weather status.

**Fleet Doctor** (runs via cron every 5min, but verify):
```bash
tail -5 /home/ccuser/rateright-growth/rivet/memory/fleet-doctor.log
```
Check for recent failures or fixes. If fleet-doctor is broken, run diagnostics manually.

**Voice Brief JSON**: Ensure `voice-brief-data.json` is fresh before morning (5:15 AM) and evening (6:00 PM) cron briefs.

**Session Archival**: Clean up stale sessions if needed.

### 6. Archive Sweep
```bash
node /home/ccuser/shared/scripts/inbox.js archive --agent <name> --acked-only
```
Run for each agent. Clears acked messages >24h old.

### 7. Log to Memory
Append to `memory/YYYY-MM-DD.md`:
- Messages processed / archived
- Escalations sent
- Fleet productivity snapshot
- Communication health flags
- Data freshness status
- Fleet doctor status

### 8. Update Status (MANDATORY)
```bash
node /home/ccuser/shared/scripts/fleet-update.js cog --status active --task "description"
```
Then write `status.json`:
```json
{
  "agent": "cog",
  "status": "active",
  "current_task": "<summary>",
  "last_updated": "<UTC ISO>",
  "last_heartbeat": "<UTC ISO>",
  "progress": 1
}
```

### Every 4th Heartbeat: Full Productivity Report
- Deliverables per agent since last summary
- Agents with zero output
- Communication volume (sent/received/acked per agent)
- OpsMan metrics: tasks handled autonomously, problems caught before escalation
- Data freshness metrics
- Fleet doctor summary

Track heartbeat count in memory.

## Known Corrections (From Fleet Bulletins)
- **BAS not needed** — no employees, not registered for GST. Never flag as task/blocker.
- **Stripe webhook resolved** — cold starts caused it. Not broken, not a blocker.

## Rules
- Fleet bulletins + inbox FIRST — catch corrections before acting on stale info
- Fleet status + inbox stats SECOND — cheap, full picture
- Don't escalate the same issue twice — check sent messages
- Batch escalations — one message to Rivet, not one per issue
- Track OUTPUT not heartbeats
- Ops issues → Cog handles. Strategy issues → Rivet handles.
- If Cog can't fix an ops issue → escalate to Sentinel, not Rivet.
- All inboxes clean + all agents producing → HEARTBEAT_OK
