cd /news/ai-agents/overnight-mode-md Β· home β€Ί topics β€Ί ai-agents β€Ί article
[ARTICLE Β· art-174081] src=gist.github.com β†— pub= topic=ai-agents verified=true sentiment=↑ positive

Overnight-mode.md

A developer published a Claude Code Stop hook that enforces an "overnight mode" for unattended agents: while a marker file at ~/.agents/OVERNIGHT exists, the hook inspects the agent's final message and blocks the turn if it ends with a question, instructing the agent to pick the most reasonable reversible option and keep working. After two blocks in a single session the hook allows the stop, so a genuinely stuck agent can still finish. The setup also includes a skill file and a test procedure using piped JSON payloads and exit codes.

recommendation

Gate unattended agent runs with a Stop hook that blocks question-ending turns and forces a reversible default, capping blocks per session so stuck agents can still exit.

more in playbook: Designing agent systems
by read4 min views1 publishedNov 22, 2026

Add an "overnight mode" Stop hook to this repository for Claude Code. Goal: when overnight mode is on, an unattended agent must never end its turn by asking a human a question. If its final message asks one, the hook blocks the stop and tells it to pick the most reasonable (based on what it knows about the project, task, and my preferences) reversible option and keep working. After two blocks in one session it lets the stop through, so a truly stuck agent can still finish. Overnight mode is on only while the file ~/.agents/OVERNIGHT exists. I create it before leaving and delete it in the morning. Steps:

  1. Create Tools/hooks/guard_overnight_questions.py with exactly the script below (adjust the wording of MESSAGE only if I ask).
  2. Register it in .claude/settings.json underhooks.Stop . Merge into any existing hooks; do not overwrite other entries. Use this entry: {"hooks": [{"type": "command", "command": "python3 "${CLAUDE_PROJECT_DIR}/Tools/hooks/guard_overnight_questions.py""}]}
  3. Test it by piping JSON payloads (shown below) and checking exit codes. Clean up any test marker files afterward, and restore ~/.agents/OVERNIGHT to however it was before.
  4. Create the skill .claude/skills/overnight-mode/SKILL.md with the content below. Then add one line to AGENTS.md or CLAUDE.md: "For unattended or overnight work, use theovernight-mode skill." Put the skill where this repo keeps its skills if that isn't.claude/skills/ .
  5. Show me the diff. Do not commit unless I say so. Script:
#!/usr/bin/env python3
import json
import re
import sys
import tempfile
from pathlib import Path
​
MAX_BLOCKS = 2
TAIL_CHARS = 500
CODE_FENCE_RE = re.compile(r"```.*?```", re.DOTALL)
ASK_RE = re.compile(
    r"\?|\b(should i|shall i|want me to|do you want|would you like|let me know (if|whether|which|how)|your call)\b",
    re.IGNORECASE,
)
MESSAGE = (
    "Overnight mode: nobody is available, so do not end your turn with a question.\n"
    "Pick the most reasonable option (based on what you know about me and the project/task) that is reversible and needs no human, start it now, and keep working. "
    "Put debatable calls on a morning list in your report.\n"
    "Stop only when every remaining item truly needs a human (irreversible or external). "
    "Then end with a plain NEEDS INPUT statement listing those items, with no question."
)
​
​
def overnight():
    return (Path.home() / ".agents" / "OVERNIGHT").exists()
​
​
def transcript_message(path):
    try:
        lines = Path(path).read_text(encoding="utf-8").splitlines()
    except (OSError, TypeError):
        return ""
    for line in reversed(lines):
        try:
            entry = json.loads(line)
        except ValueError:
            continue
        message = entry.get("message") or {}
        if entry.get("type") != "assistant" or message.get("role") != "assistant":
            continue
        content = message.get("content")
        if isinstance(content, str):
            return content
        text = "\n".join(part.get("text", "") for part in content or [] if part.get("type") == "text")
        if text.strip():
            return text
    return ""
​
​
def asks_question(text):
    tail = CODE_FENCE_RE.sub("", text or "").strip()[-TAIL_CHARS:]
    return bool(ASK_RE.search(tail))
​
​
def main():
    try:
        payload = json.load(sys.stdin)
    except ValueError:
        return 0
    if not overnight():
        return 0
    text = payload.get("last_assistant_message") or transcript_message(payload.get("transcript_path"))
    counter = Path(tempfile.gettempdir()) / "overnight-stop-guard" / str(payload.get("session_id", "unknown"))
    if not asks_question(text):
        counter.unlink(missing_ok=True)
        return 0
    try:
        count = int(counter.read_text()) + 1
    except (OSError, ValueError):
        count = 1
    if count > MAX_BLOCKS:
        counter.unlink(missing_ok=True)
        return 0
    counter.parent.mkdir(parents=True, exist_ok=True)
    counter.write_text(str(count))
    print(MESSAGE, file=sys.stderr)
    return 2
​
​
if __name__ == "__main__":
    sys.exit(main())

Skill:

---
name: overnight-mode
description: Run unattended work while the human is away; toggles the overnight marker that makes the Stop hook block final questions. Use when asked for overnight, unattended or night-shift work, or at the morning handoff.
---
​
​
The Stop hook `Tools/hooks/guard_overnight_questions.py` blocks any final message that asks a question while `~/.agents/OVERNIGHT` exists. This skill owns turning that marker on and off and working inside it.
​
## Start
​
1. Confirm the request is for unattended work. Never turn it on for interactive sessions.
2. Run `mkdir -p ~/.agents && touch ~/.agents/OVERNIGHT`.
3. Say in one line what you will work on and what you will leave alone.
​
## While working
​
- Do not ask questions. Pick the option that is reversible and needs no human, start it, and keep going.
- Keep a morning list in your report: every debatable call, what you chose, and how to undo it.
- Never do irreversible or external actions (push to shared branches, delete data, send messages, spend money) unless the request explicitly authorized that exact action.
- Stop only when every remaining item truly needs a human. End with a plain `NEEDS INPUT:` statement listing those items, with no question mark.
​
## Morning handoff
​
1. Run `rm -f ~/.agents/OVERNIGHT`.
2. Report: what was done and verified, what was not, the morning list, and the `NEEDS INPUT` items.
​
## If the hook blocks you
​
It allows two blocks per session, then lets the stop through. Treat a block as a signal to rewrite the ending as a decision or a `NEEDS INPUT` statement, not to reword the question

Test payloads (run with the OVERNIGHT file present; use a fresh session_id each time):

  • {"session_id": "t1", "last_assistant_message": "Done. Should I push?"} β†’ exit 2, message on stderr
  • the same payload a 2nd time β†’ exit 2; a 3rd time β†’ exit 0 (limit reached)
  • {"session_id": "t2", "last_assistant_message": "Done. NEEDS INPUT: approve the release."} β†’ exit 0
  • the first payload with ~/.agents/OVERNIGHT removed β†’ exit 0
── more in #ai-agents 4 stories Β· sorted by recency
── more on @claude code 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain β€” perfect for shipping the agent you just read about.

$git push zahid main
β†’ Live at https://your-agent.zahid.host βœ“
Get free account β†’ Pricing
from €0/mo Β· no card required
LIVE [news/overnight-mode-md] indexed:0 read:4min 2026-11-22 Β· β€”