From 1124758a8b196c62b22bbf201432eb9433cfeb8a Mon Sep 17 00:00:00 2001 From: bchanot Date: Thu, 12 Nov 2026 21:55:30 +0100 Subject: [PATCH] feat(digest): todo lines fall back to the file's first items, +N marker on overflow --- hooks/session-start.sh | 60 +++++++++++++++++++------- lib/tests/session-digest.test.sh | 72 +++++++++++++++++++++++++++++--- 2 files changed, 112 insertions(+), 20 deletions(-) diff --git a/hooks/session-start.sh b/hooks/session-start.sh index 1927130..eba1ff9 100644 --- a/hooks/session-start.sh +++ b/hooks/session-start.sh @@ -326,9 +326,21 @@ _digest_blockers() { # [ -n "$more" ] && echo "- open: $more" } -_digest_todo() { # → first 3 unchecked Actionable items - awk '/^### Actionable/ { on = 1; next } /^#/ { on = 0 } - on && sub(/^- \[ \] /, "") && n++ < 3 { print "- todo: " $0 }' \ +# → first 3 unchecked Actionable items, else the file's first 3. +# Comment blocks (as _digest_journal) hide headings and items; indented +# items never count. +_digest_todo() { + awk '/^[ \t]*/ { c = 0 } + END { if (a) { n = a; for (i = 1; i <= n; i++) print "- todo: " A[i] } + else for (i = 1; i <= f; i++) print "- todo: " F[i] }' \ "$1/.claude/tasks/TODO.md" } @@ -339,22 +351,42 @@ _digest_journal() { # → last journal bullet [ -n "$last" ] && echo "- journal: ${last#- }" } +# How many leading todo lines fit in chars (newline included). +_digest_fit() { # ; reads the array todo of the caller + local line size=0 shown=0 + for line in "${todo[@]}"; do + size=$((size + ${#line} + 1)) + [ "$size" -le "$1" ] || break + shown=$((shown + 1)) + done + echo "$shown" +} + +_digest_marker() { # + echo "- todo: +$1 not shown, read .claude/tasks/TODO.md" +} + # The block: header + open BLK lines + count line + todo lines + journal. # Todo lines are added in order while the block stays <= 800 chars, so the -# last ones drop first; header, open BLK lines (3 x 160 max), count line and -# journal always stay and always fit. The only loop is bounded by the todo -# count (3). Stays bash 3.2 safe (checked by SD11). +# last ones drop first; when one drops, room is reserved for a marker line +# that counts the dropped ones. Header, open BLK lines (3 x 160 max), count +# line, marker and journal always stay and always fit. The only loops are +# bounded by the todo count (3). Stays bash 3.2 safe (checked by SD11). # Built in a variable and printed once, so a failure mid-way prints nothing. _digest_assemble() { # reads the arrays blk, cnt, todo, jr of the caller - local head="memory digest:" tail="" body="" line size - for line in "${blk[@]}" "${cnt[@]}"; do head+=$'\n'"$line"; done + local out="memory digest:" tail="" line room shown + for line in "${blk[@]}" "${cnt[@]}"; do out+=$'\n'"$line"; done for line in "${jr[@]}"; do tail=$'\n'"$line"; done - size=$((${#head} + ${#tail})) - for line in "${todo[@]}"; do - [ $((size + ${#line} + 1)) -le 800 ] || break - body+=$'\n'"$line"; size=$((size + ${#line} + 1)) - done - printf '%s%s%s\n' "$head" "$body" "$tail" + room=$((800 - ${#out} - ${#tail})) + shown=$(_digest_fit "$room") + if [ "$shown" -lt "${#todo[@]}" ]; then + line=$(_digest_marker 1) + shown=$(_digest_fit $((room - ${#line} - 1))) + fi + for line in "${todo[@]:0:shown}"; do out+=$'\n'"$line"; done + [ "$shown" -lt "${#todo[@]}" ] && + out+=$'\n'"$(_digest_marker $((${#todo[@]} - shown)))" + printf '%s%s\n' "$out" "$tail" } _memory_digest() { # diff --git a/lib/tests/session-digest.test.sh b/lib/tests/session-digest.test.sh index 5ea2e61..0b0a533 100644 --- a/lib/tests/session-digest.test.sh +++ b/lib/tests/session-digest.test.sh @@ -71,12 +71,57 @@ check SD3-empty-source "$(run "$p" - | grep -c 'memory digest:')/$rc" 1/0 mkdir -p "$WORK/sd4" check SD4-no-memory "$(run "$WORK/sd4" - | grep -c 'memory digest:')" 0 -# SD5: TODO without ### Actionable -> no todo line (others stay) +# todos : write the project TODO.md fixture (\n expands) +todos() { + local IFS= p=$1; shift + printf '%b' "$*" > "$p/.claude/tasks/TODO.md" +} +# todo_lines : the digest's todo lines, one per output line +todo_lines() { run "$1" - | digest | grep '^- todo:'; } + +# SD5: TODO without Actionable -> fallback to the file's first item p=$(mkproj sd5) blk "$p" 1 open "Only blocker" -printf '### Backlog\n- [ ] hidden\n' > "$p/.claude/tasks/TODO.md" +todos "$p" '### Backlog\n- [ ] hidden\n' check SD5-no-actionable "$(run "$p" - | digest)" 'memory digest: -- open: BLK-001 Only blocker' +- open: BLK-001 Only blocker +- todo: hidden' + +# SD5b: a `##` Actionable section wins over earlier items +p=$(mkproj sd5b) +todos "$p" '### Backlog\n- [ ] early\n## Actionable\n- [ ] act\n' +check SD5b-section-wins "$(todo_lines "$p")" '- todo: act' + +# SD5c: empty section (only checked) -> fallback, capped at 3 +p=$(mkproj sd5c) +todos "$p" '## Actionable\n- [x] done\n\n## Later\n' \ + '- [ ] fb1\n- [ ] fb2\n- [ ] fb3\n- [ ] fb4\n' +check SD5c-fallback-cap "$(todo_lines "$p")" '- todo: fb1 +- todo: fb2 +- todo: fb3' + +# SD5d: indented items never count, in the section nor in the fallback +p=$(mkproj sd5d) +todos "$p" '## Actionable\n- [ ] top\n - [ ] sub\n' +check SD5d-section "$(todo_lines "$p")" '- todo: top' +todos "$p" '# T\n - [ ] sub\n- [ ] top\n - [ ] deep\n' +check SD5d-fallback "$(todo_lines "$p")" '- todo: top' + +# SD5e: items and headings inside never count +p=$(mkproj sd5e) +todos "$p" '- [ ] first\n\n- [ ] real\n' +check SD5e-fallback "$(todo_lines "$p")" '- todo: first +- todo: real' +todos "$p" '## Actionable\n\n- [ ] real\n' \ + '\n## Z\n- [ ] after\n' +check SD5e-in-section "$(todo_lines "$p")" '- todo: real' + +# SD5f: `#### Actionable` and `### Actionables` are not sections +p=$(mkproj sd5f) +todos "$p" '- [ ] first\n#### Actionable\n- [ ] x\n### Actionables\n- [ ] y\n' +check SD5f-not-sections "$(todo_lines "$p")" '- todo: first +- todo: x +- todo: y' # SD6: a 400-char journal line with an em dash, cut at 160, valid UTF-8 p=$(mkproj sd6) @@ -92,7 +137,8 @@ out=$(cd "$p" && LC_ALL=C.UTF-8 bash "$SS" /dev/null | digest | tai check SD6-len-160 "${#out}" 160 # SD7: 3 open BLK + 3 long todos + a journal line over the 800 cap: todo -# lines drop from the last, the journal and every BLK line stay +# lines drop from the last, a marker counts them, the journal and every +# BLK line stay rep() { printf "%$2s" '' | tr ' ' "$1"; } p=$(mkproj sd7) for i in 1 2 3; do blk "$p" "$i" open "$(rep b 100)"; done @@ -104,12 +150,24 @@ want="memory digest: - open: BLK-002 $(rep b 100) - open: BLK-003 $(rep b 100) - todo: A$(rep x 150)… -- todo: B$(rep x 150)… +- todo: +2 not shown, read .claude/tasks/TODO.md - journal: $(rep j 100)" out=$(run "$p" - | digest) check SD7-drops-last-todo "$out" "$want" check SD7-le-800 "$([ "${#out}" -le 800 ] && echo yes || echo no)" yes +# SD7c: 3 todos fit between 751 and 800 chars: all shown, no marker +p=$(mkproj sd7c) +for i in 1 2 3; do blk "$p" "$i" open "$(rep b 100)"; done +printf '### Actionable\n- [ ] A%s\n- [ ] B%s\n- [ ] C%s\n' \ + "$(rep x 91)" "$(rep x 91)" "$(rep x 91)" > "$p/.claude/tasks/TODO.md" +printf '## d\n- %s\n' "$(rep j 100)" > "$p/.claude/memory/journal.md" +out=$(run "$p" - | digest) +check SD7c-3-todos "$(printf '%s\n' "$out" | grep -c '^- todo: [ABC]')" 3 +check SD7c-no-marker "$(printf '%s\n' "$out" | grep -c 'not shown')" 0 +check SD7c-size \ + "$([ "${#out}" -gt 751 ] && [ "${#out}" -le 800 ] && echo ok)" ok + # SD7b: the worst case (10 open BLK, long titles, 3 long todos, 160-char # journal): all todos drop, 3 BLK + count + journal stay, whole lines, <= 800 p=$(mkproj sd7b) @@ -120,7 +178,9 @@ printf '### Actionable\n- [ ] %s\n- [ ] %s\n' "$(rep x 300)" "$(rep x 300)" \ printf '## d\n- %s\n' "$(rep j 300)" > "$p/.claude/memory/journal.md" out=$(run "$p" - | digest) check SD7b-le-800 "$([ "${#out}" -le 800 ] && echo yes || echo no)" yes -check SD7b-no-todo "$(printf '%s\n' "$out" | grep -c '^- todo:')" 0 +check SD7b-no-todo "$(printf '%s\n' "$out" | grep -c '^- todo: [x]')" 0 +check SD7b-marker "$(printf '%s\n' "$out" | + grep -c '^- todo: +2 not shown, read .claude/tasks/TODO.md$')" 1 check SD7b-3-blk "$(printf '%s\n' "$out" | grep -c '^- open: BLK-')" 3 check SD7b-count-line "$(printf '%s\n' "$out" | grep -x -- '- open: +7 more, +1 upstream')" '- open: +7 more, +1 upstream' check SD7b-journal "$(printf '%s\n' "$out" | tail -1)" "- journal: $(rep j 148)…"