Skip to content

About

WeChat iLink rate-limit heartbeat & automated task recovery for Hermes Agent | Hermes Agent 微信限流心跳推送与自动任务恢复,解决iLink主动推送配额(10条/24h)限制

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

11 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

hermes-wexin-heartbeat

English | 简体中文

WeChat iLink rate-limit heartbeat and automated task recovery for Hermes Agent. Solves the WeChat proactive message quota (限流) problem - iLink allows only 10 messages per 24h per user conversation - by pushing periodic heartbeat messages to keep the channel alive, with dual-cron architecture for zero-token heartbeat pushes and LLM-powered task continuation.

License: MIT Python 3 Platform

Table of Contents

The Problem

WeChat iLink enforces a proactive message quota: 10 messages per 24-hour window per user conversation. When an AI agent sends messages consecutively, it quickly exhausts this quota and the WeChat channel gets rate-limited (限流). Users must manually send "继续" (continue) to reset the quota and resume the conversation.

hermes-wexin-heartbeat solves this with a dual-cron architecture running every 5 minutes:

  1. loop_heartbeat - Pushes a heartbeat message only when there's something to report (pending tasks, retry queue, recent events, or gateway down). Runs in no_agent mode (zero token consumption). Stays silent when idle to conserve the 10-message/24h quota.
  2. loop_continuation — Automatically detects the last unfinished task and continues execution without user intervention. Runs in agent mode (LLM reasoning, consumes few tokens).

Users never need to send "continue" - the system recovers on its own.

iLink Known Limitations

  • Proactive quota: iLink allows max 10 proactive messages per 24h per user conversation; user sending any message resets the quota to 10
  • Burst limit: Short-window burst cap of ~5 messages/second per account; exceeding this triggers rate-limiting (限流, ret=-2)
  • DM-focused: iLink primarily supports direct messages (私聊); group chat (群聊) support is limited and depends on account type
  • Exclusive lock: Only one process can poll iLink getupdates at a time
  • Session expiry: 24h without user interaction expires the session; Hermes auto-reconnects. Note: ret=-2 is ambiguous - it can mean either rate limit OR expired context_token

Features

  • Zero-token heartbeat: no_agent mode checks every 5 minutes, pushes only when there's something to report (silent when idle)
  • Automatic task recovery: Detects unfinished tasks and resumes them without user intervention
  • Retry queue: Failed messages are automatically retried with exponential backoff and cooldown
  • Pending task tracking: CLI tool (pending.py) for enqueue/list/complete/fail task management
  • Configurable timezone: Display time in any timezone via TZ_OFFSET_HOURS / TZ_LABEL env vars
  • Docker & Host support: All paths derive from HERMES_HOME env var, works in both deployment modes
  • Gateway health check: Gateway status (✓/✗) included in push when there's an alert
  • Pure Python stdlib: No external dependencies - runs on any Python 3 installation

Architecture

┌──────────────────────────────────────────────────────────────┐
│                    cron scheduler (every 5 min)               │
├──────────────────────┬───────────────────────────────────────┤
│  loop_heartbeat      │  loop_continuation                    │
│  (no_agent, 0 token) │  (agent, consumes few tokens)         │
│                      │                                       │
│  1. Write heartbeat   │  1. Check pending.py list for tasks   │
│  2. Process retry q   │  2. Has tasks -> auto-continue        │
│  3. Read pending state│  3. Done -> pending.py complete       │
│  4. Push if active    │  4. Failed -> pending.py fail         │
│                      │  5. Push to WeChat                     │
└──────────────────────┴───────────────────────────────────────┘

Why two crons?

loop_heartbeat loop_continuation
Mode no_agent (zero tokens) agent (LLM reasoning)
Function Push status when active Auto-continue tasks
Always pushes? No - silent when idle, pushes only on events/tasks/alerts Only when there are pending tasks
When rate-limited Auto-retries push after 5 min Auto-continues after 5 min

File Structure

hermes-wexin-heartbeat/
├── README.md                       # English docs (this file)
├── README.zh-CN.md                 # Chinese docs (简体中文)
├── screenshot/                     # Push format screenshot
├── scripts/
│   ├── loop_heartbeat.py           # Main heartbeat script (no_agent)
│   ├── loop_continuation.sh         # Continuation checker (agent cron)
│   └── pending.py                  # Pending task management CLI

Prerequisites

  • Hermes Agent installed with gateway running
  • WeChat (微信) channel configured via iLink (hermes gateway setup → select Weixin)
  • hermes send command available (used to push messages to WeChat)
  • Cron scheduler enabled (hermes cron list returns normally)
  • WEIXIN_HOME_CHANNEL set in ~/.hermes/.env (required for proactive message delivery)

Docker vs Host: Path Differences

Hermes Agent uses different paths depending on deployment mode. This project's scripts default to Docker paths but support both via the HERMES_HOME environment variable.

Setting Docker Host (bare metal)
HERMES_HOME /opt/data ~/.hermes
Hermes binary /opt/hermes/.venv/bin/hermes ~/.local/bin/hermes
Config directory /opt/data (mounted from host ~/.hermes) ~/.hermes
Audit logs $HERMES_HOME/audit ~/.hermes/audit
Cron jobs $HERMES_HOME/cron/ ~/.hermes/cron/
Pending tasks $HERMES_HOME/state/pending.jsonl ~/.hermes/state/pending.jsonl
Gateway process Supervised by s6-overlay in container systemd service or manual

Docker deployment (host ~/.hermes → container /opt/data):

docker run -d --name hermes \
  -v ~/.hermes:/opt/data \
  -p 8642:8642 \
  nousresearch/hermes-agent gateway run

Host deployment — set HERMES_HOME before running scripts:

export HERMES_HOME="$HOME/.hermes"
export HERMES_SEND_CMD="$HOME/.local/bin/hermes send"

Installation

1. Copy scripts

# Place scripts into Hermes' scripts directory
cp scripts/loop_heartbeat.py  $HERMES_HOME/scripts/cron/
cp scripts/loop_continuation.sh $HERMES_HOME/scripts/cron/
cp scripts/pending.py          $HERMES_HOME/scripts/

# Ensure they are executable
chmod +x $HERMES_HOME/scripts/cron/loop_heartbeat.py
chmod +x $HERMES_HOME/scripts/cron/loop_continuation.sh
chmod +x $HERMES_HOME/scripts/pending.py

2. Review configuration

All configurable variables are at the top of loop_heartbeat.py and support environment variable overrides:

# Hermes root directory
# Docker: /opt/data | Host: ~/.hermes
HERMES_HOME = os.environ.get("HERMES_HOME", "/opt/data")

# Audit log directory
AUDIT_DIR = os.environ.get("HERMES_AUDIT_DIR") or os.path.join(HERMES_HOME, "audit")

# Retry queue & dead letter (derived from HERMES_HOME)
RETRY_QUEUE_DIR = os.path.join(HERMES_HOME, "cron", "retry_queue")
DEAD_DIR = os.path.join(HERMES_HOME, "cron", "retry_dead_letter")

# Timezone (relative to UTC)
TZ_OFFSET_HOURS = int(os.environ.get("TZ_OFFSET_HOURS", "8"))  # Beijing=8, Tokyo=9, UTC=0
TZ_LABEL = os.environ.get("TZ_LABEL", "北京时间")

# hermes send command path
HERMES_SEND_CMD = os.environ.get("HERMES_SEND_CMD", "/opt/hermes/.venv/bin/hermes send")

Environment variables reference:

Variable Description Docker default Host typical
HERMES_HOME Hermes config root /opt/data ~/.hermes
HERMES_AUDIT_DIR Audit log directory $HERMES_HOME/audit ~/.hermes/audit
TZ_OFFSET_HOURS Timezone offset from UTC 8 (Beijing) 8 (Beijing)
TZ_LABEL Timezone display label 北京时间 北京时间
HERMES_SEND_CMD hermes send command path /opt/hermes/.venv/bin/hermes send ~/.local/bin/hermes send
HERMES_SEND_PATH Subprocess PATH override Includes /opt/hermes/.venv/bin Includes ~/.local/bin

3. Create cron jobs

3a. Create loop_heartbeat (no_agent)

hermes cron create \
  --name "loop_heartbeat (5min)" \
  --schedule "*/5 * * * *" \
  --script "cron/loop_heartbeat.py" \
  --no-agent \
  --deliver origin \
  --workdir "$HERMES_HOME"

3b. Create loop_continuation (agent)

First, ensure cron/loop_continuation.sh is in place (copied in step 1).

hermes cron create \
  --name "loop_continuation (5min)" \
  --schedule "*/5 * * * *" \
  --script "cron/loop_continuation.sh" \
  --deliver origin \
  --workdir "$HERMES_HOME"

Then set the prompt for the continuation cron:

hermes cron update YOUR_JOB_ID \
  --prompt "You are the Loop Continuation Agent. Goal: automatically detect and resume interrupted conversations.

Steps:
1. Run \`python3 $HERMES_HOME/scripts/pending.py list\` to check for pending tasks
2. If there are pending tasks, pick the most urgent one and continue executing it
3. If no pending tasks, check the audit logs from the last 5 minutes for unfinished work
4. If unfinished work is found, continue executing it
5. When done, mark it complete with \`python3 $HERMES_HOME/scripts/pending.py complete <id>\`
6. On failure, mark it with \`python3 $HERMES_HOME/scripts/pending.py fail <id> <reason>\`
7. If there are no pending tasks and no unfinished work, return '[SILENT]' (this suppresses message delivery to avoid spam)

Note: This is automatic continuation. Do not ask the user questions, just do it. When there is nothing to do, stay silent - do not push unnecessary messages."

4. Optional: Pending task management

# Add a task
python3 $HERMES_HOME/scripts/pending.py enqueue "Configure multimodal model"

# List tasks
python3 $HERMES_HOME/scripts/pending.py list

# Complete a task
python3 $HERMES_HOME/scripts/pending.py complete t-123456

# Mark as failed
python3 $HERMES_HOME/scripts/pending.py fail t-123456 "Invalid API key"

# View status distribution
python3 $HERMES_HOME/scripts/pending.py status

If there is no pending.jsonl file, the loop heartbeat skips the task section and only pushes retries and events.

Push Format

When there's something to report, a push is sent in the following format:

⏳ 23:00 北京时间 | 待办 1 · 已完成 3 · 重试 2 · 事件 5
  [待办] 配置多模态模型 (1次)
  [已完成] 下载视频 (1次)
  [已完成] 转 GIF (1次)
  [已完成] 写文案 (1次)
  ... 还有 1 条
  Gateway: ✓

Status labels: [待办] · [进行中] · [已完成] · [失败]

Example of an actual push:

WeChat heartbeat push format screenshot - shows task status summary with pending, completed, retry counts and gateway health indicator

Testing

Run manually to test:

# Test heartbeat
python3 scripts/loop_heartbeat.py

# Test pending task management
python3 scripts/pending.py status

FAQ

Q: Pushes are too frequent and getting rate-limited?

iLink allows only 10 proactive messages per 24h per user conversation. With a 5-minute cron interval, the quota is exhausted in 50 minutes. To conserve quota, increase the cron interval (e.g., */30 * * * * = every 30 min = 48 pushes/24h, still over quota but allows retry on failure). Alternatively, the user can send any message to reset the quota to 10. You can also increase MIN_RETRY_INTERVAL for the retry queue.

Q: Don't need the retry queue feature?

Remove the _process_retry_queue() call, or set RETRY_QUEUE_DIR to an empty directory.

Q: Don't need pending task tracking?

Just delete the pending.jsonl file. The heartbeat will skip the task section.

Q: How to force a single push?

Manually run python3 scripts/cron/loop_heartbeat.py. Output goes to stdout.

Q: Continuation is not responding?

Check if there are pending tasks (pending.py list). If none, continuation has nothing to do. Also verify WEIXIN_HOME_CHANNEL is set in your Hermes .env file — without it, proactive pushes have nowhere to deliver.

Q: Will the two crons conflict?

No. Heartbeat is no_agent (zero tokens, pure script), continuation is agent (LLM). They run independently.

Q: How to change the timezone?

Set environment variables before running the script or in your Hermes .env:

export TZ_OFFSET_HOURS=9    # Tokyo time
export TZ_LABEL="JST"

Q: Docker or Host — which should I use?

Both work. Docker is recommended for production (isolated, easy to upgrade, s6 auto-restart). Host installation is simpler for development. See Docker vs Host section for path differences.

Related Projects

Star History

Star History

License

MIT

About

WeChat iLink rate-limit heartbeat & automated task recovery for Hermes Agent | Hermes Agent 微信限流心跳推送与自动任务恢复,解决iLink主动推送配额(10条/24h)限制

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages