# External notes: the inbox other projects write to (dev/handoffs/external-notes.md). # Sourced by handoff.sh, which sets root, base, notes, today, project, stale_days and # skill_dir, and defines days_since and folders. # # An entry is a heading, a metadata line and a free body: # ### EN-003: # - type: idea · from: <project> <tool> · received: YYYY-MM-DD · status: planned · issue: #12 # Open entries sit under "## Open"; final statuses turn them into one line at the top of # "## Handled". Longer material for an entry lives in external-notes/<id>.md; a final status # moves it to _archive/external-notes/<id>.md. Writers hold a lock (a directory) so two # sessions never write one inbox at the same time. # Legacy notes (from before this file) carry the mark # (added by the <project> <tool> session, YYYY-MM-DD ...) # or live in a dated file <YYYY-MM-DD>-from-<project>-session.md; `migrate` gathers them. legacy_re='added by the [A-Za-z0-9._-]+ [A-Za-z]+ session, [0-9]{4}-[0-9]{2}-[0-9]{2}' sep=' · ' moved_marker='<!-- moved to external-notes' die() { echo "$1" >&2 exit "${2:-2}" } notes_ensure() { # file: create from the template if missing [ -f "$1" ] || cp "$skill_dir/templates/root/external-notes.md" "$1" } one_line() { # value label: refuse tabs, line breaks and the field separator case "$1" in *"$(printf '\t')"* | *"$sep"* | *" "*) die "$2 must be one line without tabs or ' · '" ;; esac } notes_lock() { # handoffs folder: wait for the inbox lock (up to about 60 s) local lock="$1/.external-notes.lock" tries=0 until mkdir "$lock" 2> /dev/null; do tries=$((tries + 1)) if [ "$tries" -ge 600 ]; then # A lock older than two minutes was left by a session that died. if [ -n "$(find "$lock" -maxdepth 0 -mmin +2 2> /dev/null)" ]; then rmdir "$lock" 2> /dev/null || true tries=0 continue fi die "the inbox is busy (another session is writing it): try again" 1 fi sleep 0.1 done notes_locked="$lock" trap 'rmdir "$notes_locked" 2> /dev/null || true' EXIT } rewrite() { # file awk-program [awk args...]: rewrite file through awk, atomically local file="$1" prog="$2" tmp shift 2 tmp="$(mktemp "$file.XXXXXX")" || die "cannot write next to $file" 1 if awk "$@" "$prog" "$file" > "$tmp"; then mv "$tmp" "$file" else rm -f "$tmp" die "could not update $file" 1 fi } # Open entries, one per line, tab-separated, "-" for an empty field: # id, title, type, from, received, status, issue, pr. notes_open() { local file="${1:-$notes}" [ -f "$file" ] || return 0 awk ' function field(line, key, n, parts, i, kv) { n = split(line, parts, " · ") for (i = 1; i <= n; i++) { kv = parts[i] sub(/^- /, "", kv) if (index(kv, key ": ") == 1) return substr(kv, length(key) + 3) } return "-" } /^## Open/ { open = 1; next } /^## / { open = 0 } open && /^### EN-[0-9]+:/ { id = $2; sub(/:$/, "", id) title = $0; sub(/^### EN-[0-9]+: */, "", title) want = 1; next } open && want && /^- type:/ { printf "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n", id, title, field($0, "type"), field($0, "from"), field($0, "received"), field($0, "status"), field($0, "issue"), field($0, "pr") want = 0 } ' "$file" } entry() { # id [file] -> its notes_open line, or nothing notes_open "${2:-$notes}" | awk -F'\t' -v id="$1" '$1 == id' } next_id() { # handoffs folder -> the next free EN-nnn (never reuses one, archived or not) local n n="$({ grep -hoE '^### EN-[0-9]+|^- EN-[0-9]+ · ' "$1/external-notes.md" 2> /dev/null || true ls "$1/external-notes" "$1/_archive/external-notes" 2> /dev/null | grep -oE '^EN-[0-9]+' || true } | grep -oE '[0-9]+' | sort -n | tail -1)" printf 'EN-%03d' $((10#${n:-0} + 1)) } # notes_add file type from received title body -> prints the new id. The caller holds the # lock. Body lines that look like inbox structure (headings, Handled rows) are escaped so # they stay text inside the entry; {{ID}} in the body becomes the new id. notes_add() { local file="$1" id notes_ensure "$file" id="$(next_id "$(dirname "$file")")" BODY="$6" ID="$id" HEAD="### $id: $5" \ META="- type: $2${sep}from: $3${sep}received: $4${sep}status: new" rewrite "$file" ' function entry( n, lines, i, out, l) { out = ENVIRON["HEAD"] "\n" ENVIRON["META"] "\n\n" n = split(ENVIRON["BODY"], lines, "\n") for (i = 1; i <= n; i++) { l = lines[i] gsub(/\{\{ID\}\}/, ENVIRON["ID"], l) if (l ~ /^#/ || l ~ /^- EN-[0-9]/) l = "\\" l out = out l "\n" } return out } /^## Handled/ && !done { print entry(); done = 1 } { print } END { if (!done) { print ""; print entry() } } ' echo "$id" } project_root() { # any path in a project -> its main checkout local common common="$(git -C "$1" rev-parse --path-format=absolute --git-common-dir 2> /dev/null)" || die "not a git repository: $1" (cd "$common/.." && pwd) } notes_note() { # project-dir type tool title [details-file] (body on stdin) local dir="$1" type="$2" tool="$3" title="$4" details="${5:-}" troot tbase body id case "$type" in idea | decision | fyi | stale | opened) ;; *) die "type must be idea, decision, fyi, stale or opened" ;; esac one_line "$title" "the title" one_line "$tool" "the tool" [ -z "$details" ] || [ -f "$details" ] || die "no such details file: $details" troot="$(project_root "$dir")" tbase="$troot/dev/handoffs" [ "$troot" != "$root" ] || die "that is this project: write into your own handoff areas instead" if [ ! -d "$tbase" ] || ! git -C "$troot" check-ignore -q dev/handoffs/probe.md; then die "refusing: $troot has no gitignored dev/handoffs/" 1 fi body="$(cat)" [ -n "$body" ] || die "write the note's body on stdin" [ -z "$details" ] || body="$body Details: \`external-notes/{{ID}}.md\`" notes_lock "$tbase" id="$(notes_add "$tbase/external-notes.md" "$type" "$project $tool" "$today" "$title" "$body")" if [ -n "$details" ]; then mkdir -p "$tbase/external-notes" { printf '# %s: %s\n\nDetails for %s, from %s %s, %s.\n\n' "$id" "$title" "$id" "$project" "$tool" "$today" cat "$details" } > "$tbase/external-notes/$id.md" fi echo "added $id to $tbase/external-notes.md${details:+ (details: external-notes/$id.md)}" } # set -C <project-dir> ...: the sender closes its own entry in another project's inbox, e.g. # after the PR it opened there (at the user's request) was merged or closed. notes_set_in() { # project-dir id status [detail] local troot from troot="$(project_root "$1")" shift case "${2:-}" in done | rejected) ;; *) die "in another project you may only close your own entry: done or rejected" ;; esac base="$troot/dev/handoffs" notes="$troot/dev/handoffs/external-notes.md" from="$(entry "${1:-}" | cut -f4)" [ "${from%% *}" = "$project" ] || die "${1:-} in $troot is not from $project: its own sessions handle it" notes_set "$@" } notes_set() { # id status [detail] local id="$1" status="$2" detail="${3:-}" line title type from received old issue pr meta row [[ "$id" =~ ^EN-[0-9]+$ ]] || die "not an entry id: $id (EN-007)" one_line "$detail" "the detail" case "$status" in triaged | planned | in-progress | done | future | rejected | duplicate | noted) ;; *) die "status must be triaged, planned, in-progress, done, future, rejected, duplicate or noted" ;; esac [ -f "$notes" ] || die "no dev/handoffs/external-notes.md here" notes_lock "$base" line="$(entry "$id")" [ -n "$line" ] || die "no open entry $id in dev/handoffs/external-notes.md" IFS="$(printf '\t')" read -r _ title type from received old issue pr <<< "$line" [ "$issue" != "-" ] || issue="" [ "$pr" != "-" ] || pr="" case "$status" in planned) [ -n "$detail" ] || die "planned needs the issue (#12)" issue="$detail" detail="" ;; in-progress) [ -n "$detail" ] || die "in-progress needs the PR (PR #13)" pr="$detail" detail="" ;; future) [ -n "$detail" ] || [ -n "$issue" ] || die "future needs the issue that tracks it (#12)" [ -z "$detail" ] || issue="$detail" detail="" ;; rejected | duplicate) [ -n "$detail" ] || die "$status needs a detail: the reason or the earlier id" ;; noted) [ -z "$issue" ] || die "$id has an issue ($issue): close it as done, future or rejected" ;; esac case "$status" in triaged | planned | in-progress) meta="- type: $type${sep}from: $from${sep}received: $received${sep}status: $status${detail:+ ($detail)}" meta="$meta${issue:+$sep}${issue:+issue: $issue}${pr:+$sep}${pr:+pr: $pr}" META="$meta" rewrite "$notes" ' index($0, "### " id ": ") == 1 { inside = 1; print; next } /^##/ { inside = 0 } inside && /^- type:/ { print ENVIRON["META"]; inside = 0; next } { print } ' -v id="$id" ;; *) row="- $id$sep$type${sep}from $from$sep$received$sep**$status** $today: $title" row="$row${detail:+ — $detail}${issue:+ (issue $issue)}${pr:+ ($pr)}" if [ -f "$base/external-notes/$id.md" ]; then [ ! -e "$base/_archive/external-notes/$id.md" ] || die "_archive/external-notes/$id.md exists already: move it aside first" 1 mkdir -p "$base/_archive/external-notes" mv "$base/external-notes/$id.md" "$base/_archive/external-notes/$id.md" rmdir "$base/external-notes" 2> /dev/null || true row="$row (details: \`_archive/external-notes/$id.md\`)" fi ROW="$row" rewrite "$notes" ' index($0, "### " id ": ") == 1 { skip = 1; next } skip && /^##/ { skip = 0 } skip { next } /^## Handled/ { print; print ""; print ENVIRON["ROW"]; blank = 1; next } blank && /^$/ { next } { blank = 0; print } ' -v id="$id" ;; esac echo "$id: $status${detail:+ ($detail)}${issue:+ issue $issue}${pr:+ $pr}" } # Legacy notes still outside external-notes.md, one per line, tab-separated: # "file<TAB>path" for a dated note file, "line<TAB>path:line<TAB>text" for a marked line # written by another project (outside ``` code blocks). notes_legacy() { local file rel [ -d "$base" ] || return 0 find "$base" -mindepth 1 \( -name '_*' -o -name '.*' -o -path "$base/external-notes" \) -prune -o \ -type f -name '*.md' -print | sort | while read -r file; do [ "$file" != "$notes" ] || continue rel="${file#"$base"/}" case "$(basename "$file")" in [0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]-from-*-session.md) grep -qF "$moved_marker" "$file" || printf 'file\t%s\n' "$rel" continue ;; esac awk -v rel="$rel" -v own="$project" ' /^[[:space:]]*```/ { fence = !fence; next } fence || index($0, "[moved to external-notes") { next } /added by the [A-Za-z0-9._-]+ [A-Za-z]+ session, [0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]/ { s = $0 sub(/.*added by the /, "", s) split(s, w, " ") if (w[1] != own) printf "line\t%s:%d\t%s\n", rel, NR, $0 } ' "$file" done } notes_migrate() { # [--dry-run] local dry="${1:-}" kind loc text file rel from date id para n count=0 items [ -d "$base" ] || die "no dev/handoffs/ here" items="$(notes_legacy)" [ -n "$items" ] || { echo "no legacy notes to migrate" return 0 } if [ "$dry" = "--dry-run" ]; then printf '%s\n' "$items" | awk -F'\t' '{ printf "would migrate %s %s\n", $1, $2 }' return 0 fi notes_lock "$base" while IFS="$(printf '\t')" read -r kind loc text; do if [ "$kind" = "file" ]; then file="$base/$loc" text="$({ grep -m1 -oE "$legacy_re" "$file" || true; })" from="unknown (legacy)" [ -z "$text" ] || from="$(echo "$text" | sed -E 's/added by the ([^ ]+) ([^ ]+) session.*/\1 \2/')" date="$(basename "$loc" | cut -c1-10)" id="$(notes_add "$notes" fyi "$from" "$date" "Legacy note file $loc" \ "The note, unchanged: \`external-notes/{{ID}}.md\`. Read it there and spread what holds into the areas.")" mkdir -p "$base/external-notes" mv "$file" "$base/external-notes/$id.md" # A stub at the old path keeps links to it working. printf '%s %s -->\nMoved to `external-notes/%s.md`, entry %s in `external-notes.md` (once the entry is\nhandled, the file moves to _archive/external-notes/).\n' "$moved_marker" "$id" "$id" "$id" > "$file" else rel="${loc%:*}" n="${loc##*:}" file="$base/$rel" from="$(echo "$text" | sed -E 's/.*added by the ([A-Za-z0-9._-]+) ([A-Za-z]+) session.*/\1 \2/')" date="$(echo "$text" | sed -E 's/.*added by the [^,]*, ([0-9-]{10}).*/\1/')" # The note is the paragraph or list item around the marked line. para="$(awk -v n="$n" ' function item(l) { return l ~ /^[[:space:]]*([-*+]|[0-9]+\.)[[:space:]]/ } /^[[:space:]]*$/ { if (NR < n) { p = "" } else if (NR > n) { exit }; next } item($0) && NR <= n { p = "" } item($0) && NR > n { exit } { p = p "> " $0 "\n" } END { printf "%s", p } ' "$file")" id="$(notes_add "$notes" fyi "$from" "$date" "Legacy note in $rel (line $n)" "$para")" rewrite "$file" 'NR == n { $0 = $0 tag } { print }' -v n="$n" -v tag=" [moved to external-notes $id]" fi count=$((count + 1)) done <<< "$items" echo "migrated $count legacy note(s) into dev/handoffs/external-notes.md: triage them now" } notes_inbox() { # [--new] local items legacy seen id title type from received status issue pr [ -d "$base" ] || return 0 items="$(notes_open)" legacy="$(notes_legacy | grep -c . || true)" if [ "${1:-}" = "--new" ]; then # Each new entry once per session: remember the ids already shown. mkdir -p "$base/.inbox-seen" seen="$base/.inbox-seen/${HANDOFF_SESSION:-default}" touch "$seen" find "$base/.inbox-seen" -type f -mtime +7 -delete 2> /dev/null || true items="$(printf '%s\n' "$items" | while IFS="$(printf '\t')" read -r id title type from received status issue pr; do [ "$status" = "new" ] || continue if ! grep -qxF "$id" "$seen"; then echo "$id" >> "$seen" printf '%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n' "$id" "$title" "$type" "$from" "$received" "$status" "$issue" "$pr" fi done)" if [ "$legacy" != "0" ] && ! grep -qxF legacy "$seen"; then echo legacy >> "$seen" else legacy=0 fi fi if [ -n "$items" ]; then echo "External notes waiting in dev/handoffs/external-notes.md:" printf '%s\n' "$items" | while IFS="$(printf '\t')" read -r id title type from received status issue pr; do printf ' %s [%s] %s from %s, %s: %s' "$id" "$status" "$type" "$from" "$received" "$title" [ "$issue" = "-" ] || printf ' (issue %s)' "$issue" [ "$pr" = "-" ] || printf ' (%s)' "$pr" [ ! -f "$base/external-notes/$id.md" ] || printf ' (+ external-notes/%s.md)' "$id" echo done fi if [ "$legacy" != "0" ]; then echo "$legacy legacy note(s) outside external-notes.md: run 'handoff.sh migrate --dry-run', then 'migrate', then triage them." fi # Run by hand, say so when nothing waits; the hook (--new) stays silent. if [ -z "$items" ] && [ "$legacy" = "0" ] && [ "${1:-}" != "--new" ]; then echo "No external notes waiting." fi return 0 } notes_status_line() { local open new legacy open="$(notes_open | grep -c . || true)" new="$(notes_open | awk -F'\t' '$6 == "new"' | grep -c . || true)" legacy="$(notes_legacy | grep -c . || true)" if [ "$open" != "0" ] || [ "$legacy" != "0" ]; then printf '%-16s %s open (%s new), %s legacy: run '"'"'handoff.sh inbox'"'"'\n' "inbox" "$open" "$new" "$legacy" fi } valid_date() { [[ "$1" =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}$ ]] && [ -n "$(days_since "$1")" ]; } notes_check() { # WARN lines for check local id title type from received status issue pr age legacy notes_open | while IFS="$(printf '\t')" read -r id title type from received status issue pr; do if ! valid_date "$received"; then echo "WARN external-notes: $id has a malformed received date: $received" continue fi case "${status%% *}" in new | triaged) ;; planned | in-progress) continue ;; *) echo "WARN external-notes: $id has an unknown status: $status" continue ;; esac age="$(days_since "$received")" if [ "$age" -gt "$stale_days" ]; then echo "WARN external-notes: $id ($status, from $from) has waited $age days: triage it" fi done legacy="$(notes_legacy | grep -c . || true)" if [ "$legacy" != "0" ]; then echo "WARN external-notes: $legacy legacy note(s) outside external-notes.md: run 'handoff.sh migrate'" fi } # Before a merge: exit 1 while an entry from another project has stayed new for a day or # more, or an entry's date can't be read (the gate would otherwise pass silently). notes_gate() { local out out="$(notes_open | while IFS="$(printf '\t')" read -r id title type from received status issue pr; do if ! valid_date "$received"; then printf '%s has a malformed received date (%s): fix it in dev/handoffs/external-notes.md.\n' "$id" "$received" elif [ "$status" = "new" ] && [ "$(days_since "$received")" -ge 1 ]; then printf '%s from %s (%s) is still new: triage it first.\n' "$id" "$from" "$received" fi done)" [ -n "$out" ] || return 0 printf '%s\n' "$out" >&2 exit 1 }