Skip to content

fix(bin): make the open-decisions fold linear in line length - #15

Merged
Hozzy02 merged 1 commit into
mainfrom
fm/scout-fm-send-latency
Sep 25, 2026
Merged

Hozzy02 merged 1 commit into
mainfrom
fm/scout-fm-send-latency

Conversation

@Hozzy02

@Hozzy02 Hozzy02 commented Sep 25, 2026

Copy link
Copy Markdown
Owner

Intent

Fix fm-send.sh latency: FM_HOME= bin/fm-send.sh --resolve-key "..." took 60-90s (several exceeded a 60s tool timeout) when answering decisions of a secondmate with a large status log (sm-lifeline-dash: 365 lines, 307 KB). A scout traced it to the whole-log open-decisions fold that fm-send runs before sending to confirm the key is open: in bin/fm-classify-lib.sh, _fm_decision_fold_line (and the sibling _fm_status_open_activities_stream) tested for a blank line with stripped=${line//[[:space:]]/}, which is quadratic in line length under macOS /bin/bash 3.2 with a UTF-8 locale (~500 ms per 1.6 KB line; 84 s for that log). Herdr and the submit confirmation loop were measured fast and bounded, so they are deliberately untouched. The captain approved shipping ONLY the minimal fix: replace the blank-line test with an equivalent linear case glob ([![:space:]]) in both functions and drop the now-unused 'stripped' locals, with no change to fold semantics, output, or fold version. Plus a regression test in the existing tests/fm-classify-decision-key.test.sh: whitespace-only lines are still skipped by both folds while indented events still fold, and a ~243-line, ~1.5 KB-per-line log (with multibyte text) folds under a UTF-8 locale (when the host has one) within a generous 20 s wall-clock bound; measured 1 s with the fix and 98 s without it on macOS. A timing bound is the deliberate choice here since the defect is purely a performance regression; it is generous to avoid CI flakiness and trivially passes on bash 5 hosts. Out of scope by decision: routing fm-send through the cursor-backed incremental fold (shared-cursor interaction with fm-wake-drain), reducing per-line subshell forks, or changing the FM_SEND_SETTLE pause.

What Changed

  • In bin/fm-classify-lib.sh, _fm_decision_fold_line and _fm_status_open_activities_stream now check for blank lines with a case glob (*[![:space:]]*) instead of ${line//[[:space:]]/}. The old substitution took time quadratic in line length under macOS /bin/bash 3.2 with a UTF-8 locale. This made fm-send.sh --resolve-key take 60–90s on secondmates with large status logs. The now-unused stripped locals are removed. Fold semantics, output and fold version stay the same. On a log with space, tab, CR, NBSP and VT/FF-only lines, the old and new libraries produced byte-identical output.
  • Added regression cases to tests/fm-classify-decision-key.test.sh:
    • Both folds still skip whitespace-only lines.
    • Both folds still fold indented events.
    • A ~243-line log with ~1.5 KB multibyte lines must fold within a generous 20s wall-clock bound under a UTF-8 locale. It took 1s with the fix and 71s against the base library.
  • In a manual end-to-end fm-send.sh sm --resolve-key run against a 353 KB multibyte status log, the time dropped from 33s to 3s.

Risk Assessment

✅ Low: This is a small change that does exactly what the intent asks for: in both folds, a linear *[![:space:]]* case glob replaces the blank-line test and gives the same answer; the unused stripped locals are removed, fold semantics and version are unchanged, and the regression tests cover whitespace-only lines, indented events, and a bounded large-log fold under a UTF-8 locale.

Testing

I ran the updated regression test under macOS /bin/bash 3.2 with a UTF-8 locale and it passes, with the large-log fold taking 1s. Against the pre-fix library the same test fails at 71s, so it does catch the slowdown. I also ran the real fm-send.sh --resolve-key command end-to-end on a lifeline-dash-sized log (366 lines, 353 KB, multibyte text, stubbed tmux, disposable home). It took 3s with the fix and 33s without. Both runs delivered the answer and closed the key. The old and new libraries give byte-identical fold output on whitespace-only and indented lines, so the fold's behavior is unchanged. The worktree is clean after the temporary library swaps were restored, and there are no findings.

Evidence: E2E fm-send --resolve-key on a 353 KB log, with the fix (3s)

status log: 366 lines, 353513 bytes fm-send rc=0 elapsed=3s typed to worker: use recharts ledger tail: resolved [key=chart-lib]: answered: use recharts

status log:      366 lines,   353513 bytes
●━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
●  WATCHER DOWN - SUPERVISION IS OFF
●  1 task(s) in flight, but no watcher has a fresh beacon (last beat: never, grace 300s).
●  Trust the emitted supervision protocol for this harness; do not use shell & for watcher repair.
●  This is a supervision warning only; the requested message WILL still be sent.
●  watcher supervision needs Stop-owned automatic recovery; inspect the hook registration and startup status before ending the turn.
●━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
fm-send rc=0 elapsed=3s
typed to worker: use recharts
ledger tail: resolved [key=chart-lib]: answered: use recharts
Evidence: Same E2E run against the pre-fix library (33s)

status log: 366 lines, 353513 bytes fm-send rc=0 elapsed=33s typed to worker: use recharts ledger tail: resolved [key=chart-lib]: answered: use recharts

status log:      366 lines,   353513 bytes
fm-send rc=0 elapsed=33s
typed to worker: use recharts
ledger tail: resolved [key=chart-lib]: answered: use recharts
Evidence: E2E reproduction script
#!/bin/bash
# E2E: real fm-send.sh --resolve-key against a ~365-line, ~300 KB status log, /bin/bash 3.2, UTF-8.
set -u
ROOT=$1; W=$(mktemp -d); home=$W/home; fb=$W/fb; mkdir -p "$home/state" "$fb"
cat > "$fb/tmux" <<'SH'
#!/usr/bin/env bash
case "${1:-}" in
  send-keys) shift; l=0; while [ $# -gt 0 ]; do case "$1" in -t) shift 2;; -l) l=1; shift;; *) break;; esac; done
    [ $l = 1 ] && printf '%s' "${1:-}" >> "$FM_SEND_LOG"; exit 0;;
  display-message) for a in "$@"; do case "$a" in *cursor_y*) echo 1; exit 0;; esac; done; echo fakepane; exit 0;;
  capture-pane) printf '╭────╮\n│    │\n╰────╯\n'; exit 0;;
esac; exit 0
SH
printf '#!/bin/bash\nexit 0\n' > "$fb/sleep"; chmod +x "$fb/tmux" "$fb/sleep"
. "$ROOT/tests/lib.sh"
fm_write_meta "$home/state/sm.meta" "window=sess:fm-sm" "kind=ship"
pad=$(printf 'progress on the dashboard — café ✓ naïve résumé %.0s' $(seq 1 17))
s=$home/state/sm.status
printf 'needs-decision [key=chart-lib]: pick a chart library\n' > $s
for i in $(seq 1 363); do printf 'working [key=ws%d]: %s\n' $i "$pad" >> $s; done
printf '   \n\t\n' >> $s
echo "status log: $(wc -l < $s) lines, $(wc -c < $s) bytes"
export LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8
start=$(date +%s)
env FM_GATE_REFUSE_BYPASS=1 PATH="$fb:$PATH" FM_ROOT_OVERRIDE="$home" FM_HOME="$home" FM_SEND_LOG="$W/send.log" FM_SEND_SETTLE=0 \
  /bin/bash "$ROOT/bin/fm-send.sh" sm --resolve-key chart-lib "use recharts"; rc=$?
echo "fm-send rc=$rc elapsed=$(( $(date +%s) - start ))s"
echo "typed to worker: $(cat $W/send.log)"
echo "ledger tail: $(tail -1 $s)"
rm -rf "$W"
Evidence: Regression test with the fix (bash 3.2): passes, 1s
ok - a stated [key=X] opens X whether it precedes or follows the verb colon
ok - a keyless needs-decision still opens and closes the default key
ok - a resolution closes its decision regardless of either line's key position
ok - blocked [key=X] opens X in both key positions
ok - two colon-form keyed decisions never collapse into one shared bucket
ok - a [key=x] mentioned mid-note is prose, never an opened or closed key
ok - a malformed stated key is rejected in both positions, never folded as default
ok - status_line_verb strips every bracket tag before the colon, in any order, and recovers the bare verb
ok - a [corr=...] tag ahead of [key=...] no longer swallows the verb: opens and closes under the stated key
ok - a [corr=...] tag with no stated key opens under 'default', exactly like a bare needs-decision line
ok - a [key=x] tag alone (no corr tag) still opens x - no regression from the tag-stripping fix
ok - blocked/resolved parse their bare verb with any bracket-tag order preceding the colon
ok - the incremental fold matches the full fold across appends in both key positions
ok - whitespace-only lines are skipped while indented events still fold
ok - the whole-log fold of a large status log stays within 20s (took 1s)
/bin/bash tests/fm-classify-decision-key.test.sh  0.63s user 1.03s system 84% cpu 1.971 total
Evidence: Regression test against the pre-fix library: fails, 71s

not ok - whole-log fold of a 243-line, ~1.5 KB-per-line log took 71s (bound 20s, locale en_US.UTF-8)

ok - a stated [key=X] opens X whether it precedes or follows the verb colon
ok - a keyless needs-decision still opens and closes the default key
ok - a resolution closes its decision regardless of either line's key position
ok - blocked [key=X] opens X in both key positions
ok - two colon-form keyed decisions never collapse into one shared bucket
ok - a [key=x] mentioned mid-note is prose, never an opened or closed key
ok - a malformed stated key is rejected in both positions, never folded as default
ok - status_line_verb strips every bracket tag before the colon, in any order, and recovers the bare verb
ok - a [corr=...] tag ahead of [key=...] no longer swallows the verb: opens and closes under the stated key
ok - a [corr=...] tag with no stated key opens under 'default', exactly like a bare needs-decision line
ok - a [key=x] tag alone (no corr tag) still opens x - no regression from the tag-stripping fix
ok - blocked/resolved parse their bare verb with any bracket-tag order preceding the colon
ok - the incremental fold matches the full fold across appends in both key positions
ok - whitespace-only lines are skipped while indented events still fold
not ok - whole-log fold of a 243-line, ~1.5 KB-per-line log took 71s (bound 20s, locale en_US.UTF-8)
/bin/bash tests/fm-classify-decision-key.test.sh  69.84s user 1.65s system 99% cpu 1:12.15 total

Pipeline

Updates from git push no-mistakes

✅ **intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

✅ **Review** - passed

✅ No issues found.

✅ **Test** - passed

✅ No issues found.

  • /bin/bash tests/fm-classify-decision-key.test.sh on the fixed commit (bash 3.2.57, UTF-8): all 14 cases pass; the large-log fold takes 1s against the 20s bound
  • /bin/bash tests/fm-classify-decision-key.test.sh with bin/fm-classify-lib.sh temporarily swapped to the base version (d56df70), then restored: the new timing test fails (71s, bound 20s, locale en_US.UTF-8), so it catches the regression
  • Manual end-to-end run of the real bin/fm-send.sh sm --resolve-key chart-lib &#34;use recharts&#34; under /bin/bash with LC_ALL=en_US.UTF-8, using a throwaway home, a 366-line / 353 KB multibyte status log, stubbed tmux and FM_GATE_REFUSE_BYPASS=1 (as tests/lib.sh sets it): 3s with the fix, 33s with the base library; both deliver the answer and append resolved [key=chart-lib]
  • Differential check: status_open_decisions and _fm_status_open_activities_stream on a log with space, tab, CR, NBSP and VT/FF-only lines plus space- and tab-indented events give byte-identical output with the old and new libraries
✅ **Document** - passed

✅ No issues found.

✅ **Lint** - passed

✅ No issues found.

✅ **Push** - passed

✅ No issues found.

The blank-line test ${line//[[:space:]]/} is quadratic in macOS bash 3.2 under UTF-8,
so fm-send --resolve-key spent 60-90s folding a large secondmate status log.
A case glob keeps the same semantics; a regression test bounds a large-log fold.

Claude-Session: https://claude.ai/code/session_01Q5yvLkybJ8FfxWDTH9mVT1
@Hozzy02
Hozzy02 merged commit a0c8f92 into main Sep 25, 2026
13 checks passed
@Hozzy02
Hozzy02 deleted the fm/scout-fm-send-latency branch September 26, 2026 00:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant