Skip to main content

A job watcher wakes Grok Bot reliably when Codex and Claude Code tasks finish

XXTimothy Callaghan2026-11-14

Code and reposAuthor's claim, unverifiedRead first

Overview

The author's Grok Bot runs Codex CLI and Claude Code CLI. A finished background task did not wake the idle bot, so the author kept asking if it was done. A curl call to a Webhook Routine at the end of the task was unreliable too.

Workflow

  1. Trigger: The bot could not tell when a task finished. The author runs Codex CLI and Claude Code CLI through Grok Bot. Background shells did not wake an idle agent and detached jobs gave no signal, so the author kept asking if the job had finished.
  2. First version: curl a Grok Bot Webhook Routine at the end of the script. This works only if the task reaches that line. The old helper missed 11 of 31 wakeups because a webhook environment variable was absent. Jobs often completed without notifying anyone, and killed jobs went silent. Keeping Grok Bot in an open turn to poll used allowance on waiting.
  3. Bot: Follow the setup prompt to build a deterministic watcher. The author publishes a generic prompt in the companion article and says pasting it into Grok Bot will walk through the same build. It requires Python 3.11+ standard library and SQLite, with no extra services or cloud dependencies, and accounts for a box that may run as a container with tini as PID 1 and no systemd or cron.
  4. Prompt point 1: One Webhook Routine per responsible bot. Each Routine has a URL and bearer key to treat as secrets. Delivery is “at least once”, so the Routine prompt must deduplicate by event_id / Idempotency-Key and treat 200 as receipt only.
  5. Prompt point 2: Keep secrets out of workers. Put credentials outside the workspace and git in a 0600 JSON file, for example ~/.secrets/agent-webhooks.json. Store only an alias-to-{url, key} map and let only the notifier read it. Use only aliases in manifests, and have job-run strip webhook variables from the child environment before starting Codex or Claude.
  6. Prompt point 3: job-run + SQLite outbox + job-watch + job-notify + jobctl. job-run starts the task in a new session, records boot_id + pid + start ticks to guard against PID reuse, forwards TERM/HUP/INT, and records an exit receipt even if the subprocess dies or crashes (143 for SIGTERM). In v1 timeouts warn without killing. Completion policies are commit_push_verified (model, wrapper and push all exit 0, with matching local and remote SHAs) and exit_zero for review or QA jobs that cannot push. job-watch checks process identity, log size and mtime without reading content, and CPU/I/O. job-notify resolves aliases at send time, uses HTTPS without redirects and applies exponential backoff with jitter, respecting Retry-After on 429. Logs omit URLs, secrets and response bodies.
  7. Prompt point 4: Start automatically on a box without systemd. Use ~/.profile and ~/.bashrc hooks and a flock watchdog that runs ensure periodically. The prompt says to be clear that this is not a true boot supervisor. A reboot requires a login or manual ensure.
  8. By hand: Change how workers start. Detach workers through job-run, for example setsid nohup job-run --manifest "$J/manifest.json" -- "$J/run.sh". Remove the trailing curl from migrated scripts so one component owns notifications.
  9. Check: The prompt calls for unit tests with temporary state, real child processes and a localhost-only HTTP server. Cover receipt policy, SIGTERM receipts, registry permissions, retry backoff, single-instance locking and keeping secrets out of logs. Keep tests away from production webhooks. Run a real canary against your Webhook Routine and a real CLI task. Inspect jobctl health and redacted state, including the completion-to-webhook delay. The prompt also forbids adding unrequested features.

Key points and results

  • A trailing curl provides no supervision. A script using nohup and a final curl cannot wake the bot if the wrapper gets killed, crashes, hangs or never reaches curl. Any retries die with it. The old approach missed 11 of 31 wakeups.
  • First two hours (author's claim, not reproduced by Bot in Practice). Twelve real jobs (9 Codex CLI and 3 Claude Code CLI) and one canary. Eight jobs succeeded and four failed. All 13 wakeups were accepted on the first attempt (HTTP 200), with a median 3.7 seconds between completion and webhook.
  • The failure the author designed for occurred. One Codex job took a SIGTERM after 58.5 seconds and its failure wakeup was accepted just over four seconds later. Three more jobs pushed successfully but failed the post-push check and still sent failure wakeups.
  • A 200 means the Routine accepted the wakeup. The agent may still have to act. Wakeups can occasionally arrive twice, so each has an ID and the agent ignores duplicates.
  • Limits the author lists. After a full reboot, the watcher needs a nudge to start. Other programs under the same user can still read the permission-restricted webhook key file, which the author says risks a fake or wasted wakeup. Hanging jobs are flagged without being killed, since they might just be slow.
  • Check for an official solution first. The author says the team ships features quickly and this could be outdated within days. If a built-in feature covers it, the author would retire the watcher and advises readers to use the built-in option.

Source

Platform
X
Author
Timothy Callaghan
Published
Categories
Code and repos

What this evidence label means

Author's claim, unverified

The method and results come from the author's own account. This site has not reproduced them.

Read the editorial and evidence policy

Other cases in this scenario

This unofficial community project is independent of xAI. xAI has neither approved nor endorsed it. No sign-up. The methods and figures come from the original authors.