# 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: · 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/.md; a final status
# moves it to _archive/external-notes/.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 session, YYYY-MM-DD ...)
# or live in a dated file -from--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='\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
}