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:
- Create
Tools/hooks/guard_overnight_questions.pywith exactly the script below (adjust the wording of MESSAGE only if I ask). - Register it in
.claude/settings.jsonunderhooks.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""}]} - 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.
- Create the skill
.claude/skills/overnight-mode/SKILL.mdwith the content below. Then add one line to AGENTS.md or CLAUDE.md: "For unattended or overnight work, use theovernight-modeskill." Put the skill where this repo keeps its skills if that isn't.claude/skills/. - 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