chore: add project planning-with-files hooks
This commit is contained in:
91
.codex/hooks/codex_hook_adapter.py
Normal file
91
.codex/hooks/codex_hook_adapter.py
Normal file
@@ -0,0 +1,91 @@
|
||||
#!/usr/bin/env python3
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
|
||||
HOOK_DIR = Path(__file__).resolve().parent
|
||||
|
||||
|
||||
def load_payload() -> dict[str, Any]:
|
||||
raw = sys.stdin.read().strip()
|
||||
if not raw:
|
||||
return {}
|
||||
try:
|
||||
payload = json.loads(raw)
|
||||
except json.JSONDecodeError:
|
||||
return {}
|
||||
return payload if isinstance(payload, dict) else {}
|
||||
|
||||
|
||||
def cwd_from_payload(payload: dict[str, Any]) -> Path:
|
||||
cwd = payload.get("cwd")
|
||||
if isinstance(cwd, str) and cwd:
|
||||
return Path(cwd)
|
||||
return Path.cwd()
|
||||
|
||||
|
||||
def session_id_from_payload(payload: dict[str, Any]) -> str | None:
|
||||
sid = payload.get("session_id")
|
||||
if isinstance(sid, str) and sid:
|
||||
return sid
|
||||
env_sid = os.environ.get("PWF_SESSION_ID", "")
|
||||
return env_sid if env_sid else None
|
||||
|
||||
|
||||
def is_session_attached(root: Path, session_id: str | None) -> bool:
|
||||
"""Return True if this session should receive plan context.
|
||||
|
||||
Legacy mode: if .planning/sessions/ does not exist, always return True so
|
||||
existing single-session users are not broken on upgrade.
|
||||
Isolation mode: return True only when the session has an attached sentinel.
|
||||
"""
|
||||
sessions_dir = root / ".planning" / "sessions"
|
||||
if not sessions_dir.exists():
|
||||
return True # legacy — no sessions dir means single-session setup
|
||||
if not session_id:
|
||||
return False # sessions dir exists but caller has no ID — stay silent
|
||||
return (sessions_dir / f"{session_id}.attached").exists()
|
||||
|
||||
|
||||
def emit_json(payload: dict[str, Any]) -> None:
|
||||
if not payload:
|
||||
return
|
||||
json.dump(payload, sys.stdout, ensure_ascii=False)
|
||||
sys.stdout.write("\n")
|
||||
|
||||
|
||||
def parse_json(text: str) -> dict[str, Any]:
|
||||
if not text.strip():
|
||||
return {}
|
||||
try:
|
||||
payload = json.loads(text)
|
||||
except json.JSONDecodeError:
|
||||
return {}
|
||||
return payload if isinstance(payload, dict) else {}
|
||||
|
||||
|
||||
def run_shell_script(script_name: str, cwd: Path) -> tuple[str, str]:
|
||||
result = subprocess.run(
|
||||
["sh", str(HOOK_DIR / script_name)],
|
||||
cwd=str(cwd),
|
||||
text=True,
|
||||
capture_output=True,
|
||||
check=False,
|
||||
)
|
||||
return result.stdout.strip(), result.stderr.strip()
|
||||
|
||||
|
||||
def main_guard(func) -> int:
|
||||
try:
|
||||
func()
|
||||
except Exception as exc: # pragma: no cover
|
||||
print(f"[planning-with-files hook] {exc}", file=sys.stderr)
|
||||
return 0
|
||||
return 0
|
||||
|
||||
33
.codex/hooks/permission_request.py
Normal file
33
.codex/hooks/permission_request.py
Normal file
@@ -0,0 +1,33 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Codex PermissionRequest adapter for planning-with-files (v2.38.0).
|
||||
|
||||
Fires when Codex asks the user to permit a tool call. We surface a short
|
||||
reminder that an active plan exists so the user reviews task_plan.md before
|
||||
approving. Read-only; never blocks the request; always exits cleanly.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import codex_hook_adapter as adapter
|
||||
|
||||
|
||||
def main() -> None:
|
||||
payload = adapter.load_payload()
|
||||
root = adapter.cwd_from_payload(payload)
|
||||
|
||||
if not adapter.is_session_attached(root, adapter.session_id_from_payload(payload)):
|
||||
return
|
||||
|
||||
plan = root / "task_plan.md"
|
||||
if not plan.exists():
|
||||
return
|
||||
|
||||
adapter.emit_json({
|
||||
"systemMessage": (
|
||||
"[planning-with-files] Active plan detected. Review the current phase "
|
||||
"in task_plan.md before approving the tool request."
|
||||
)
|
||||
})
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(adapter.main_guard(main))
|
||||
11
.codex/hooks/post-tool-use.sh
Normal file
11
.codex/hooks/post-tool-use.sh
Normal file
@@ -0,0 +1,11 @@
|
||||
#!/bin/bash
|
||||
# planning-with-files: Post-tool-use hook for Codex
|
||||
|
||||
HOOK_DIR="$(cd "$(dirname "$0")" 2>/dev/null && pwd)"
|
||||
PLAN_DIR="$(sh "${HOOK_DIR}/resolve-plan-dir.sh" 2>/dev/null)"
|
||||
PLAN_FILE="${PLAN_DIR:+${PLAN_DIR}/}task_plan.md"
|
||||
|
||||
if [ -f "$PLAN_FILE" ]; then
|
||||
echo "[planning-with-files] Update progress.md with what you just did. If a phase is now complete, update task_plan.md status."
|
||||
fi
|
||||
exit 0
|
||||
20
.codex/hooks/post_tool_use.py
Normal file
20
.codex/hooks/post_tool_use.py
Normal file
@@ -0,0 +1,20 @@
|
||||
#!/usr/bin/env python3
|
||||
from __future__ import annotations
|
||||
|
||||
import codex_hook_adapter as adapter
|
||||
|
||||
|
||||
def main() -> None:
|
||||
payload = adapter.load_payload()
|
||||
root = adapter.cwd_from_payload(payload)
|
||||
|
||||
if not adapter.is_session_attached(root, adapter.session_id_from_payload(payload)):
|
||||
return
|
||||
|
||||
stdout, _ = adapter.run_shell_script("post-tool-use.sh", root)
|
||||
if stdout:
|
||||
adapter.emit_json({"systemMessage": stdout})
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(adapter.main_guard(main))
|
||||
30
.codex/hooks/pre-compact.sh
Normal file
30
.codex/hooks/pre-compact.sh
Normal file
@@ -0,0 +1,30 @@
|
||||
#!/bin/sh
|
||||
# planning-with-files: PreCompact hook for Codex
|
||||
# Reminds the agent to flush progress before context compaction.
|
||||
|
||||
HOOK_DIR="$(cd "$(dirname "$0")" 2>/dev/null && pwd)"
|
||||
PLAN_DIR="$(sh "${HOOK_DIR}/resolve-plan-dir.sh" 2>/dev/null)"
|
||||
PLAN_FILE="${PLAN_DIR:+${PLAN_DIR}/}task_plan.md"
|
||||
|
||||
if [ ! -f "$PLAN_FILE" ]; then
|
||||
exit 0
|
||||
fi
|
||||
|
||||
if [ -n "$PLAN_DIR" ]; then
|
||||
ATTESTATION_FILE="${PLAN_DIR}/.attestation"
|
||||
else
|
||||
ATTESTATION_FILE=".plan-attestation"
|
||||
fi
|
||||
|
||||
echo "[planning-with-files] PreCompact: context compaction is about to occur."
|
||||
echo "Before compaction completes: ensure progress.md captures recent actions and task_plan.md status reflects current phase."
|
||||
echo "task_plan.md, findings.md, progress.md remain on disk and will be re-read after compaction."
|
||||
|
||||
if [ -f "$ATTESTATION_FILE" ]; then
|
||||
ATTEST="$(tr -d '\r\n[:space:]' < "$ATTESTATION_FILE" 2>/dev/null)"
|
||||
if [ -n "$ATTEST" ]; then
|
||||
echo "Plan-SHA256 at compaction: $ATTEST"
|
||||
fi
|
||||
fi
|
||||
|
||||
exit 0
|
||||
14
.codex/hooks/pre-tool-use.sh
Normal file
14
.codex/hooks/pre-tool-use.sh
Normal file
@@ -0,0 +1,14 @@
|
||||
#!/bin/bash
|
||||
# planning-with-files: Pre-tool-use hook for Codex
|
||||
|
||||
HOOK_DIR="$(cd "$(dirname "$0")" 2>/dev/null && pwd)"
|
||||
PLAN_DIR="$(sh "${HOOK_DIR}/resolve-plan-dir.sh" 2>/dev/null)"
|
||||
PLAN_FILE="${PLAN_DIR:+${PLAN_DIR}/}task_plan.md"
|
||||
|
||||
if [ -f "$PLAN_FILE" ]; then
|
||||
# Log plan context to stderr so the Codex adapter can surface it as systemMessage.
|
||||
head -30 "$PLAN_FILE" >&2
|
||||
fi
|
||||
|
||||
echo '{"decision": "allow"}'
|
||||
exit 0
|
||||
28
.codex/hooks/pre_tool_use.py
Normal file
28
.codex/hooks/pre_tool_use.py
Normal file
@@ -0,0 +1,28 @@
|
||||
#!/usr/bin/env python3
|
||||
from __future__ import annotations
|
||||
|
||||
import codex_hook_adapter as adapter
|
||||
|
||||
|
||||
def main() -> None:
|
||||
payload = adapter.load_payload()
|
||||
root = adapter.cwd_from_payload(payload)
|
||||
|
||||
if not adapter.is_session_attached(root, adapter.session_id_from_payload(payload)):
|
||||
adapter.emit_json({"decision": "allow"})
|
||||
return
|
||||
|
||||
stdout, stderr = adapter.run_shell_script("pre-tool-use.sh", root)
|
||||
|
||||
result = adapter.parse_json(stdout)
|
||||
decision = result.get("decision")
|
||||
if decision and decision != "allow":
|
||||
adapter.emit_json(result)
|
||||
return
|
||||
|
||||
if stderr:
|
||||
adapter.emit_json({"systemMessage": stderr})
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(adapter.main_guard(main))
|
||||
76
.codex/hooks/resolve-plan-dir.sh
Executable file
76
.codex/hooks/resolve-plan-dir.sh
Executable file
@@ -0,0 +1,76 @@
|
||||
#!/bin/sh
|
||||
# planning-with-files: resolve active plan directory.
|
||||
#
|
||||
# Resolution order:
|
||||
# 1. $PLAN_ID env var → ./.planning/$PLAN_ID/ if exists
|
||||
# 2. ./.planning/.active_plan content → matching dir if exists
|
||||
# 3. Newest ./.planning/<dir>/ by mtime
|
||||
# 4. Otherwise empty stdout (caller falls back to legacy ./task_plan.md)
|
||||
#
|
||||
# Always exits 0. Never errors out the agent loop.
|
||||
#
|
||||
# Usage:
|
||||
# PLAN_DIR="$(sh scripts/resolve-plan-dir.sh)"
|
||||
# PLAN_FILE="${PLAN_DIR:+$PLAN_DIR/}task_plan.md"
|
||||
|
||||
set -u
|
||||
|
||||
PLAN_ROOT="${1:-${PWD}/.planning}"
|
||||
ACTIVE_FILE="${PLAN_ROOT}/.active_plan"
|
||||
|
||||
resolve_from_env() {
|
||||
plan_id="${PLAN_ID:-}"
|
||||
[ -z "${plan_id}" ] && return 1
|
||||
candidate="${PLAN_ROOT}/${plan_id}"
|
||||
if [ -d "${candidate}" ]; then
|
||||
printf "%s\n" "${candidate}"
|
||||
return 0
|
||||
fi
|
||||
return 1
|
||||
}
|
||||
|
||||
resolve_from_active_file() {
|
||||
[ -f "${ACTIVE_FILE}" ] || return 1
|
||||
plan_id="$(tr -d '\r\n' < "${ACTIVE_FILE}")"
|
||||
[ -z "${plan_id}" ] && return 1
|
||||
candidate="${PLAN_ROOT}/${plan_id}"
|
||||
if [ -d "${candidate}" ]; then
|
||||
printf "%s\n" "${candidate}"
|
||||
return 0
|
||||
fi
|
||||
return 1
|
||||
}
|
||||
|
||||
resolve_latest_dir() {
|
||||
[ -d "${PLAN_ROOT}" ] || return 1
|
||||
# Portable newest-mtime selector. Avoid `ls -t` BSD/GNU drift.
|
||||
# Only consider dirs that contain task_plan.md — skips system dirs like sessions/.
|
||||
latest=""
|
||||
latest_mtime=0
|
||||
for entry in "${PLAN_ROOT}"/*/; do
|
||||
[ -d "${entry}" ] || continue
|
||||
# Strip trailing slash
|
||||
clean="${entry%/}"
|
||||
# Skip hidden dirs
|
||||
case "$(basename "${clean}")" in
|
||||
.*) continue ;;
|
||||
esac
|
||||
# Skip dirs that are not plan dirs
|
||||
[ -f "${clean}/task_plan.md" ] || continue
|
||||
mtime="$(date -r "${clean}" +%s 2>/dev/null || stat -c '%Y' "${clean}" 2>/dev/null || echo 0)"
|
||||
if [ "${mtime}" -gt "${latest_mtime}" ] 2>/dev/null; then
|
||||
latest_mtime="${mtime}"
|
||||
latest="${clean}"
|
||||
fi
|
||||
done
|
||||
if [ -n "${latest}" ]; then
|
||||
printf "%s\n" "${latest}"
|
||||
return 0
|
||||
fi
|
||||
return 1
|
||||
}
|
||||
|
||||
if resolve_from_env; then exit 0; fi
|
||||
if resolve_from_active_file; then exit 0; fi
|
||||
if resolve_latest_dir; then exit 0; fi
|
||||
exit 0
|
||||
15
.codex/hooks/session-start.sh
Normal file
15
.codex/hooks/session-start.sh
Normal file
@@ -0,0 +1,15 @@
|
||||
#!/bin/sh
|
||||
# planning-with-files: SessionStart hook for Codex
|
||||
# Runs session catchup, then reuses the same prompt context hook as UserPromptSubmit.
|
||||
|
||||
SCRIPT_DIR="$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)"
|
||||
CODEX_ROOT="$(CDPATH= cd -- "$SCRIPT_DIR/.." && pwd)"
|
||||
SKILL_DIR="$CODEX_ROOT/skills/planning-with-files"
|
||||
PYTHON_BIN="${PYTHON_BIN:-$(command -v python3 || command -v python)}"
|
||||
|
||||
if [ -n "$PYTHON_BIN" ] && [ -f "$SKILL_DIR/scripts/session-catchup.py" ]; then
|
||||
"$PYTHON_BIN" "$SKILL_DIR/scripts/session-catchup.py" "$(pwd)"
|
||||
fi
|
||||
|
||||
sh "$SCRIPT_DIR/user-prompt-submit.sh"
|
||||
exit 0
|
||||
25
.codex/hooks/stop.py
Normal file
25
.codex/hooks/stop.py
Normal file
@@ -0,0 +1,25 @@
|
||||
#!/usr/bin/env python3
|
||||
from __future__ import annotations
|
||||
|
||||
import codex_hook_adapter as adapter
|
||||
|
||||
|
||||
def main() -> None:
|
||||
payload = adapter.load_payload()
|
||||
root = adapter.cwd_from_payload(payload)
|
||||
|
||||
if not adapter.is_session_attached(root, adapter.session_id_from_payload(payload)):
|
||||
return
|
||||
|
||||
stdout, _ = adapter.run_shell_script("stop.sh", root)
|
||||
result = adapter.parse_json(stdout)
|
||||
|
||||
message = result.get("followup_message")
|
||||
if not isinstance(message, str) or not message:
|
||||
return
|
||||
|
||||
adapter.emit_json({"systemMessage": message})
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(adapter.main_guard(main))
|
||||
34
.codex/hooks/stop.sh
Normal file
34
.codex/hooks/stop.sh
Normal file
@@ -0,0 +1,34 @@
|
||||
#!/bin/bash
|
||||
# planning-with-files: Stop hook for Codex
|
||||
|
||||
HOOK_DIR="$(cd "$(dirname "$0")" 2>/dev/null && pwd)"
|
||||
PLAN_DIR="$(sh "${HOOK_DIR}/resolve-plan-dir.sh" 2>/dev/null)"
|
||||
PLAN_FILE="${PLAN_DIR:+${PLAN_DIR}/}task_plan.md"
|
||||
|
||||
if [ ! -f "$PLAN_FILE" ]; then
|
||||
exit 0
|
||||
fi
|
||||
|
||||
TOTAL=$(grep -c "### Phase" "$PLAN_FILE" || true)
|
||||
COMPLETE=$(grep -cF "**Status:** complete" "$PLAN_FILE" || true)
|
||||
IN_PROGRESS=$(grep -cF "**Status:** in_progress" "$PLAN_FILE" || true)
|
||||
PENDING=$(grep -cF "**Status:** pending" "$PLAN_FILE" || true)
|
||||
|
||||
if [ "$COMPLETE" -eq 0 ] && [ "$IN_PROGRESS" -eq 0 ] && [ "$PENDING" -eq 0 ]; then
|
||||
COMPLETE=$(grep -c "\[complete\]" "$PLAN_FILE" || true)
|
||||
IN_PROGRESS=$(grep -c "\[in_progress\]" "$PLAN_FILE" || true)
|
||||
PENDING=$(grep -c "\[pending\]" "$PLAN_FILE" || true)
|
||||
fi
|
||||
|
||||
: "${TOTAL:=0}"
|
||||
: "${COMPLETE:=0}"
|
||||
: "${IN_PROGRESS:=0}"
|
||||
: "${PENDING:=0}"
|
||||
|
||||
if [ "$COMPLETE" -eq "$TOTAL" ] && [ "$TOTAL" -gt 0 ]; then
|
||||
echo "{\"followup_message\": \"[planning-with-files] ALL PHASES COMPLETE ($COMPLETE/$TOTAL). If the user has additional work, add new phases to task_plan.md before starting.\"}"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
echo "{\"followup_message\": \"[planning-with-files] Task in progress ($COMPLETE/$TOTAL phases complete). If ending this turn, make sure progress.md is up to date.\"}"
|
||||
exit 0
|
||||
28
.codex/hooks/user-prompt-submit.sh
Normal file
28
.codex/hooks/user-prompt-submit.sh
Normal file
@@ -0,0 +1,28 @@
|
||||
#!/bin/bash
|
||||
# planning-with-files: User prompt submit hook for Codex
|
||||
|
||||
HOOK_DIR="$(cd "$(dirname "$0")" 2>/dev/null && pwd)"
|
||||
PLAN_DIR="$(sh "${HOOK_DIR}/resolve-plan-dir.sh" 2>/dev/null)"
|
||||
PLAN_FILE="${PLAN_DIR:+${PLAN_DIR}/}task_plan.md"
|
||||
PROGRESS_FILE="${PLAN_DIR:+${PLAN_DIR}/}progress.md"
|
||||
|
||||
# Session isolation: if .planning/sessions/ exists, only attached sessions see
|
||||
# plan context. Absence of the sessions dir means legacy single-session mode —
|
||||
# all sessions in the cwd receive context to preserve backward compatibility.
|
||||
if [ -d ".planning/sessions" ]; then
|
||||
SESSION_ID="${PWF_SESSION_ID:-}"
|
||||
if [ -z "$SESSION_ID" ] || [ ! -f ".planning/sessions/${SESSION_ID}.attached" ]; then
|
||||
exit 0
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ -f "$PLAN_FILE" ]; then
|
||||
echo "[planning-with-files] ACTIVE PLAN — current state:"
|
||||
head -50 "$PLAN_FILE"
|
||||
echo ""
|
||||
echo "=== recent progress ==="
|
||||
tail -20 "$PROGRESS_FILE" 2>/dev/null
|
||||
echo ""
|
||||
echo "[planning-with-files] Read findings.md for research context. Continue from the current phase."
|
||||
fi
|
||||
exit 0
|
||||
Reference in New Issue
Block a user