跪拜 Guibai
← All articles
Command Line · AI Programming · Git

Claude Code Works the Night Shift: An Overnight Cloud Setup That Lands Commits While You Sleep

By 德育处主任 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

The cost of running an AI agent unattended is mostly discipline, not infrastructure: an isolated branch, a standing policy file, deny rules, and machine-checkable completion criteria are what keep an overnight Claude Code run reviewable. The workflow plugs ApexOne as the cloud machine, but the pattern transfers to any always-on environment.

Summary

Claude Code is a terminal process, so overnight work constrains to two hard problems: session persistence and machine uptime. tmux solves the former, keeping a session alive through SSH drops and display disconnects. An always-on machine — a local Mac with caffeinate -i, a self-hosted VPS, or a pre-configured cloud dev machine like ApexOne — covers the latter. The pattern is machine-agnostic; the hard part is the policy layer.

That policy layer is where the actual design lives. CLAUDE.md gets a set of night rules: completion criteria (npm test passes, lint clean), a commit per subtask, a three-attempts-then-skip rule for stuck problems, and an explicit 'don't ask questions' directive. Task descriptions must say what done looks like, not just what to do; the agent will deliver on the bare wording. Permission mode auto lets routine operations run without prompts while a classifier blocks dangerous invocations, and `.claude/settings.json` adds deny rules for git push, rm -rf, and .env reads. Non-interactive `claude -p` with `--max-turns` makes the same pattern fully headless and cron-compatible.

A failure table catalogs what actually goes wrong overnight: usage-limit stops, context compression causing repeated work, stalls on a pending question, flaky tests, and overscoped tasks. Every fix is structural rather than prompt-based. Morning acceptance is git log, npm test, git diff, and reading NOTES.md, expecting that some overnight output needs rework or disposal.

Takeaways
— Claude Code dies with its terminal and its machine; tmux keeps the session alive across disconnects, and an always-on machine (cloud dev VM, VPS, or local Mac with sleep disabled) is required for overnight runs.
— Overnight tasks run on a dedicated branch with a pre-task commit, so a `git checkout main` is always a rollback path.
— CLAUDE.md rules for night runs must include: completion criteria (npm test pass, lint clean), commit per subtask, a 3-attempt-then-skip rule, and 'don't stop to ask questions.'
— The task description itself must state completion criteria — 'add tests' is too vague because the agent will deliver on the bare wording.
— `--permission-mode auto` gates routine operations while blocking dangerous ones; `bypassPermissions` is only safe in an isolated container or VM.
— Deny rules in `.claude/settings.json` block `git push`, `rm -rf`, and reads of `.env` files, keeping all changes local for morning review.
— `nohup claude &` fails because interactive Claude needs a real TTY; tmux detach with `Ctrl+B D` (not `exit`) is the correct pattern.
— Non-interactive `claude -p` with `--max-turns 80` runs a task to completion unattended, with output to night.log and cron compatibility for scheduled runs.
— Stop Hooks can POST to a WeCom, Feishu, or DingTalk webhook so the developer gets a push notification when the overnight task finishes.
— Known failure modes: usage-limit stops, context compression causing repeated work, stall on a pending question, flaky tests, and oversized task scope — each fixed by a structural rule.
— Morning review checks `git log`, `npm test`, `git diff main...night/add-tests`, and NOTES.md; finished output is expected to partially require rework.
Conclusions

The no-question rule is the load-bearing design decision: any agent question becomes a full stall when nobody is around, so the policy forces forward motion and pushes uncertainty into NOTES.md instead.

CLAUDE.md operates as a runtime policy file, not a memory aid — its real job is telling the agent how to behave under failure: when to retry, when to skip, what to record.

Completion criteria serve two purposes at once: they tell the agent when to stop and constrain it from touching business code. The definition of correctness here is purely mechanical (tests pass, lint clean), which is why the pattern only fits verifiable bulk tasks.

The machine comparison weighs setup time against control: your own computer shares data with the AI, a self-hosted VPS is full DIY, and a cloud dev machine buys zero-setup with isolation. The honest framing is that the cloud option only solves uptime, nothing else.

Context compression, stuck loops, and waiting on input all appear in the failure table, and every fix is a policy change. Operating rules around the agent matter more than how the prompt is phrased.

Concepts & terms
tmux
A terminal multiplexer that keeps a session running after SSH disconnects and the local terminal closes. Detach with Ctrl+B D while the process continues in the background; reattach later with `tmux attach -t session-name`.
claude -p
Claude Code's non-interactive print mode: runs a single prompt to completion without needing a real TTY, streams output to stdout, and exits. Works with `--max-turns` as an iteration cap and pairs with cron for scheduled unattended runs.
permission-mode auto
One of Claude Code's three permission settings. A classifier evaluates each action: routine operations like npm install, tests, and file edits run without prompting, while dangerous ones like forced pushes or piping downloaded scripts to bash are blocked. The alternative modes are acceptEdits (asks on commands, not files) and bypassPermissions (allows everything, for isolated environments only).
Stop Hook
A Claude Code hook that fires a command when a run ends. In this workflow it POSTs to a group-chat webhook so the developer gets a push notification that the overnight task finished.
--max-turns
A Claude Code flag that caps the number of agent iterations, used here as a hard stop against infinite loops on the same problem. After the cap it exits, leaving whatever state the run is in.
CLAUDE.md
A markdown file at the project root that Claude Code reads on every startup. It's the standard place for project rules and conventions; this workflow uses it to inject standing policy for overnight runs.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗