Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Structured planning workflow that uses files to track tasks, decisions, and project progress.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
scripts/attest-plan.sh
1#!/bin/sh2# planning-with-files: lock the current task_plan.md content with a SHA-256 attestation.3#4# Use after you finalise (or intentionally edit) a plan. The hooks then refuse5# to inject plan content into the model context if the file diverges from the6# attested hash, surfacing a "[PLAN TAMPERED]" warning instead.7#8# Resolution:9# 1. $PLAN_ID env var → ./.planning/$PLAN_ID/10# 2. ./.planning/.active_plan11# 3. Newest ./.planning/<dir>/ by mtime12# 4. Legacy ./task_plan.md at project root13#14# Usage:15# sh scripts/attest-plan.sh # attest the active plan16# sh scripts/attest-plan.sh --show # print the stored hash17# sh scripts/attest-plan.sh --clear # remove the attestation (re-open the plan)1819set -u2021SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"22RESOLVER="${SCRIPT_DIR}/resolve-plan-dir.sh"2324resolve_plan_file() {25plan_dir=""26if [ -f "${RESOLVER}" ]; then27plan_dir="$(sh "${RESOLVER}" 2>/dev/null)"28fi29if [ -n "${plan_dir}" ] && [ -f "${plan_dir}/task_plan.md" ]; then30printf "%s\n" "${plan_dir}/task_plan.md"31return 032fi33if [ -f "./task_plan.md" ]; then34printf "%s\n" "./task_plan.md"35return 036fi37return 138}3940attestation_path_for() {41plan_file="$1"42plan_dir="$(dirname "${plan_file}")"43if [ "${plan_dir}" = "." ]; then44# Legacy mode: store at project root.45printf "%s\n" "./.plan-attestation"46else47printf "%s\n" "${plan_dir}/.attestation"48fi49}5051compute_hash() {52target="$1"53if command -v sha256sum >/dev/null 2>&1; then54sha256sum "${target}" | awk '{print $1}'55elif command -v shasum >/dev/null 2>&1; then56shasum -a 256 "${target}" | awk '{print $1}'57else58printf "ERROR: no sha256 utility available\n" >&259return 160fi61}6263mode="attest"64case "${1:-}" in65--show) mode="show" ;;66--clear) mode="clear" ;;67"") mode="attest" ;;68*)69printf "Usage: %s [--show|--clear]\n" "$0" >&270exit 271;;72esac7374plan_file="$(resolve_plan_file)" || {75printf "[plan-attest] No task_plan.md found. Create a plan first.\n" >&276exit 177}7879attestation_file="$(attestation_path_for "${plan_file}")"8081case "${mode}" in82show)83if [ -f "${attestation_file}" ]; then84printf "Plan: %s\n" "${plan_file}"85printf "Attestation: %s\n" "${attestation_file}"86printf "SHA-256: %s\n" "$(cat "${attestation_file}")"87# Nonce (security A1.4): if init-session generated a per-plan nonce88# next to the attestation, surface it. Informational only here; the89# hooks consume it to build collision-proof BEGIN/END delimiters.90nonce_file="$(dirname "${attestation_file}")/.nonce"91if [ -f "${nonce_file}" ]; then92printf "Nonce: %s\n" "$(tr -d '\r\n[:space:]' < "${nonce_file}" 2>/dev/null)"93fi94else95printf "[plan-attest] No attestation set for %s.\n" "${plan_file}"96exit 197fi98;;99clear)100if [ -f "${attestation_file}" ]; then101rm -f "${attestation_file}"102printf "[plan-attest] Cleared attestation for %s.\n" "${plan_file}"103else104printf "[plan-attest] No attestation to clear.\n"105fi106;;107attest)108hash_val="$(compute_hash "${plan_file}")" || exit 1109110# v2.40: protect the write with an advisory flock when available so111# concurrent legacy-mode sessions (no PLAN_ID, both at the same project112# root) cannot corrupt the .plan-attestation file mid-write. Atomic113# rename of a temp file is the real guarantee on POSIX; flock is the114# cooperative gate around the rename for slow-disk writes.115#116# Note: legacy single-file mode is inherently racey across concurrent117# sessions because both can edit task_plan.md without coordination. The118# canonical parallel-session pattern is slug-mode under119# .planning/<slug>/, where each session pins PLAN_ID and gets its own120# .attestation file. We surface a hint when concurrent activity is121# detected.122if [ -f "${attestation_file}" ]; then123mtime_now="$(date +%s 2>/dev/null || echo 0)"124mtime_prev="$(stat -c '%Y' "${attestation_file}" 2>/dev/null \125|| stat -f '%m' "${attestation_file}" 2>/dev/null \126|| echo 0)"127age=$((mtime_now - mtime_prev))128if [ "${age}" -ge 0 ] && [ "${age}" -lt 30 ] 2>/dev/null; then129# If we're in legacy mode (root .plan-attestation) and another130# session just wrote, warn. Slug-mode files in .planning/<slug>/131# are per-session by construction; no need to warn there.132case "${attestation_file}" in133*./.plan-attestation|*/.plan-attestation)134case "${attestation_file}" in135*./.planning/*) : ;; # slug-mode, ignore136*)137printf "[plan-attest] Note: %s was modified %ss ago by another process.\n" \138"${attestation_file}" "${age}" >&2139printf "[plan-attest] For parallel sessions, prefer slug-mode (init-session.sh <name>) so each session gets its own .attestation file.\n" >&2140;;141esac142;;143esac144fi145fi146147tmp_file="${attestation_file}.tmp.$$"148printf "%s\n" "${hash_val}" > "${tmp_file}" 2>/dev/null || {149printf "[plan-attest] Failed to write %s\n" "${tmp_file}" >&2150exit 1151}152mv_ok=1153if command -v flock >/dev/null 2>&1; then154# Advisory lock around the rename. lock_dir is the dir containing155# the target file. The {} subshell pattern keeps the lock scoped to156# the mv call.157lock_dir="$(dirname "${attestation_file}")"158(159flock -w 5 9 || true160mv -f "${tmp_file}" "${attestation_file}"161) 9>"${lock_dir}/.attestation.lock" 2>/dev/null || mv_ok=0162rm -f "${lock_dir}/.attestation.lock" 2>/dev/null163else164mv -f "${tmp_file}" "${attestation_file}" 2>/dev/null || mv_ok=0165fi166167# Integrity gap fix (security A2.1): a failed atomic rename must not be168# allowed to silently leave a stale attestation when the target already169# existed. The old fallback only wrote when the file was absent, so a170# cross-device or permission-denied mv on an existing attestation left171# the OLD hash in place with a success exit. On mv failure we re-write172# the intended hash through a second atomic rename (never a bare173# redirect onto the live file, which would expose torn reads to174# concurrent verifiers), then verify the on-disk content.175if [ "${mv_ok}" -eq 0 ] || [ ! -f "${attestation_file}" ]; then176fb_tmp="${attestation_file}.fb.$$"177printf "%s\n" "${hash_val}" > "${fb_tmp}" 2>/dev/null \178&& mv -f "${fb_tmp}" "${attestation_file}" 2>/dev/null || {179rm -f "${fb_tmp}" "${tmp_file}" 2>/dev/null180printf "[plan-attest] Failed to write attestation %s\n" "${attestation_file}" >&2181exit 1182}183fi184rm -f "${tmp_file}" 2>/dev/null185186# Read-back verification. Both write paths above are atomic renames, so187# a concurrent verifier always reads a complete 64-hex hash — either our188# own or an identical one from a peer attesting the same plan content.189# A mismatch here therefore means our intended hash genuinely did not190# land (stale content, failed write); fail loudly with a nonzero exit so191# callers never trust a stale attestation.192stored_hash="$(tr -d '\r\n[:space:]' < "${attestation_file}" 2>/dev/null)"193if [ "${stored_hash}" != "${hash_val}" ]; then194printf "[plan-attest] Attestation write verification FAILED for %s\n" "${attestation_file}" >&2195printf "[plan-attest] Expected %s, found %s. The plan is NOT attested.\n" "${hash_val}" "${stored_hash}" >&2196exit 1197fi198199short_hash="$(printf "%s" "${hash_val}" | cut -c1-12)"200printf "[plan-attest] Locked %s\n" "${plan_file}"201printf "[plan-attest] SHA-256: %s... (stored in %s)\n" "${short_hash}" "${attestation_file}"202printf "[plan-attest] Hooks will block injection if the file is modified without re-running this command.\n"203;;204esac205206exit 0207