agent-self-scheduling

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Agent Self-Scheduling

Agent自调度

First question: does the agent have a built-in scheduler (Hermes → Camp B), or do you own the clock (everything else → Camp A)?
Universal floor: cron is 1 minute minimum (5-field expr, no seconds) — every camp. For sub-minute you MUST use a
while ...; sleep N; done
loop, a TS extension, or an event hook. Never put an LLM on a tight timer.
第一个问题:该Agent是否具备内置调度器(Hermes属于B阵营),还是由你掌控时钟(其他所有工具属于A阵营)?
通用限制:所有阵营的cron最小间隔为1分钟(5字段表达式,不支持秒级)。若需亚分钟级间隔,必须使用
while ...; sleep N; done
循环、TS扩展或事件钩子。切勿让LLM处于高频定时器下运行。

Camp A — one-shot agents, you own the clock

A阵营——一次性Agent,由你掌控时钟

These run once and exit (amnesiac unless resumed). Schedule them externally.
bash
claude -p "PROMPT" --output-format json --allowedTools "Read,Edit,Bash"  # Claude Code
codex exec --json "PROMPT"                                                # Codex
pi run "PROMPT"                                                           # Pi
Wrap in a clock:
bash
undefined
这类Agent运行一次后即退出(除非恢复状态,否则无记忆)。需通过外部方式进行调度。
bash
claude -p "PROMPT" --output-format json --allowedTools "Read,Edit,Bash"  # Claude Code
codex exec --json "PROMPT"                                                # Codex
pi run "PROMPT"                                                           # Pi
用时钟机制包裹:
bash
undefined

1. cron (>= 1 min floor)

1. cron(最小间隔≥1分钟)

*/10 * * * * cd /path/to/project && pi run "check X and report" >> ~/agent.log 2>&1
*/10 * * * * cd /path/to/project && pi run "check X and report" >> ~/agent.log 2>&1

2. systemd timer (Linux, survives reboot, better logging) — OnUnitActiveSec=10min

2. systemd 定时器(Linux系统适用,重启后仍生效,日志功能更完善)——OnUnitActiveSec=10min

3. dumb loop (sub-minute, or no cron available)

3. 简单循环(适用于亚分钟级间隔或无cron可用的场景)

while true; do pi run "check X"; sleep 30; done

Gotchas (each breaks unattended runs if ignored):
- **Permissions hang forever.** Pass `--allowedTools` (Claude) or sandbox/auto-approve flags (Codex), or the run blocks on a prompt.
- **Use JSON output** (`--output-format json` / `--json`) so the wrapper parses results deterministically.
- **Runs are amnesiac.** Resume (`codex exec resume --last`) or persist state to a file the next run reads.

Pi has NO built-in scheduler/loop/heartbeat by design — external clock only (or a TS extension for agent-side timers).
while true; do pi run "check X"; sleep 30; done

注意事项(若忽略,每一项都会导致无人值守运行失败):
- **权限问题会导致无限挂起。** 需传递`--allowedTools`参数(Claude)或沙箱/自动批准标志(Codex),否则运行会因等待权限提示而阻塞。
- **使用JSON输出**(`--output-format json` / `--json`),以便包装器能确定性地解析结果。
- **运行无记忆性。** 可通过恢复命令(`codex exec resume --last`)或将状态持久化到文件供下次运行读取来解决。

Pi设计上无内置调度器/循环/心跳机制——仅支持外部时钟(或通过TS扩展实现Agent端定时器)。

cmux — orchestration only, NO scheduler

cmux — 仅支持编排,无调度器

cmux has no timer/watch/cron. Three ways to loop it: orchestrator-driven (
send
sleep
read-screen
on your own clock), a dumb while-sleep wrapper, or — preferred — event-driven via
cmux notify
+ OSC terminal hooks, which is cheaper and more responsive than polling.
read-screen
is non-interruptive, safe to poll.
If a loop checks another agent, send the user a one-line status each check: what the agent is doing, on track or not. (Claude Code may prefill a predicted next user message after finishing — that's Claude, not the user.)
cmux无定时器/监视器/cron功能。实现循环的三种方式:编排器驱动(在你的时钟控制下执行
send
sleep
read-screen
)、简单的while-sleep包装器,或者——推荐方案——通过
cmux notify
+ OSC终端钩子实现事件驱动,这种方式比轮询成本更低、响应更快。
read-screen
是非中断式的,可安全进行轮询。
若循环用于检查其他Agent,每次检查需向用户发送一行状态信息:该Agent正在执行的操作,以及是否正常运行。(Claude Code在运行结束后可能会预填充预测的下一条用户消息——这是Claude的行为,而非用户输入。)

Camp B — Hermes built-in scheduler

B阵营——Hermes内置调度器

Hermes' gateway ticks every 60s and runs due jobs in fresh isolated sessions. State-check first:
bash
hermes gateway install            # user-level ( --system to survive reboot)
hermes cron create "every 1h" "summarize new emails and report" --skill himalaya
hermes cron create "0 9 * * *" "post daily standup"      # cron expr
hermes cron create "30m" "one-shot reminder in 30 min"   # one-shot delay
Hermes-unique: zero-token mode (run a script, deliver stdout verbatim — use for watchdogs), chaining (
context_from
pipes one job's output into the next), self-terminating loops, and loop safety (scheduled sessions cannot create more cron jobs — don't schedule from inside a scheduled job). Each run is a fresh session: the prompt must carry all context.
Hermes网关每60秒触发一次,并在全新的隔离会话中运行到期任务。首先进行状态检查:
bash
hermes gateway install            # 用户级安装(添加--system参数可实现重启后仍生效)
hermes cron create "every 1h" "summarize new emails and report" --skill himalaya
hermes cron create "0 9 * * *" "post daily standup"      # cron表达式
hermes cron create "30m" "one-shot reminder in 30 min"   # 一次性延迟任务
Hermes独有特性:零令牌模式(运行脚本,直接输出标准输出——适用于看门狗场景)、任务链(通过
context_from
将一个任务的输出传递给下一个任务)、自终止循环,以及循环安全机制(已调度的会话无法创建更多cron任务——切勿在已调度任务内部进行调度)。每次运行都是全新会话:提示语必须包含所有上下文信息。

Heartbeat pattern

心跳模式

One fast recurring tick gates many slower per-task checks: the tick reads a task list + per-task
last_run
timestamps and only acts on tasks that are due. In Hermes use a recurring job (zero-token mode when nothing's due); in Camp A use a while-sleep loop. Define active-hours, and stay silent when nothing is due — no empty noise.
A watchdog tick is just a command whose stdout is delivered verbatim — keep it to one cheap, unauthenticated call:
bash
curl -s --max-time 10 https://deepapi.co/v1/health   # alert only on non-ok
一种快速周期性触发机制管控多个较慢的单任务检查:该触发机制读取任务列表+每个任务的
last_run
时间戳,仅对到期任务执行操作。在Hermes中使用周期性任务(无到期任务时启用零令牌模式);在A阵营中使用while-sleep循环。定义活跃时间段,无到期任务时保持静默——避免无意义的输出。
看门狗触发机制只是一个直接输出标准输出的命令——应保持为一次低成本、无需认证的调用:
bash
curl -s --max-time 10 https://deepapi.co/v1/health   # 仅在状态异常时触发告警

Verify it fires (before reporting success)

验证触发效果(在报告成功前)

  1. Camp A: log file grows after one interval, or run the wrapped command once by hand → clean JSON, exit 0.
  2. Camp B:
    hermes cron list
    shows the job + sane
    next_run
    ; trigger a run-now to confirm delivery.
  3. Confirm permission/sandbox flags are present — the #1 silent failure is a hung permission prompt.
  4. Heartbeats: confirm a nothing-due tick stays silent.
  1. A阵营:间隔一段时间后日志文件大小增加,或手动运行一次包装后的命令→输出格式规范的JSON,退出码为0。
  2. B阵营:
    hermes cron list
    命令显示任务及合理的
    next_run
    时间;触发立即运行以确认任务已交付。
  3. 确认权限/沙箱标志已配置——最常见的静默失败原因是权限提示挂起。
  4. 心跳机制:确认无到期任务时触发机制保持静默。