Repository navigation
fix(bin): make the open-decisions fold linear in line length - #15
Merged
Merged
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
bin/fm-classify-lib.sh,_fm_decision_fold_lineand_fm_status_open_activities_streamnow check for blank lines with acaseglob (*[![:space:]]*) instead of${line//[[:space:]]/}. The old substitution took time quadratic in line length under macOS/bin/bash3.2 with a UTF-8 locale. This madefm-send.sh --resolve-keytake 60–90s on secondmates with large status logs. The now-unusedstrippedlocals 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.tests/fm-classify-decision-key.test.sh:fm-send.sh sm --resolve-keyrun 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 unusedstrippedlocals 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 rechartsEvidence: 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 rechartsEvidence: E2E reproduction script
Evidence: Regression test with the fix (bash 3.2): passes, 1s
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)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.shon 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.shwith 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 regressionManual end-to-end run of the realbin/fm-send.sh sm --resolve-key chart-lib "use recharts"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 appendresolved [key=chart-lib]Differential check:status_open_decisionsand_fm_status_open_activities_streamon 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.