fix(session-end): preserve $-sequences in user messages (summary corruption)#2180
Conversation
…g summary The regenerated summary block embeds raw user-message text and was passed as the *replacement* argument to String.prototype.replace, where $-sequences ($&, $$, $`, $') are special. A user message containing $& re-injected the entire matched block (duplicating the summary markers) and $$ collapsed to $, silently corrupting the persisted session summary. buildSummarySection only escapes newlines and backticks, not $. Fix: use function replacers (() => summaryBlock) at both rewrite sites so the replacement text is treated literally. Adds an end-to-end regression test.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughThe session-end hook's session-summary update logic now uses function replacers to prevent ChangesSession-end hook
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 ESLint
ESLint install failed. For unrecoverable errors, disable the tool in CodeRabbit configuration. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
…g summary (affaan-m#2180) The regenerated summary block embeds raw user-message text and was passed as the *replacement* argument to String.prototype.replace, where $-sequences ($&, $$, $`, $') are special. A user message containing $& re-injected the entire matched block (duplicating the summary markers) and $$ collapsed to $, silently corrupting the persisted session summary. buildSummarySection only escapes newlines and backticks, not $. Fix: use function replacers (() => summaryBlock) at both rewrite sites so the replacement text is treated literally. Adds an end-to-end regression test. Co-authored-by: bymle <229636660+bymle@users.noreply.github.com>
Problem
session-end.js(Stop hook) silently corrupts the persisted session summary when a user message contains a$-sequence ($&,$$,$\``,$') — which is common in dev work (regexes,PASS=$1, prices like$5`, shell snippets).Root cause
The regenerated summary block embeds raw user-message text, and is passed as the replacement argument to
String.prototype.replace(lines 259 & 265):In a replacement string,
$&= the whole match,$$=$, etc.buildSummarySectionescapes newlines and backticks but not$. So on any repeat Stop (when the file already has the summary markers), a user message likerelease $& fallbackmakes$&re-inject the entire matched old block, duplicating the summary markers and dropping the user's text.Verified corruption on a
release $& fallback $$ donemessage (markers duplicated,$$→$):Fix
Use function replacers (
() => summaryBlock) at both rewrite sites — a function replacement is treated literally, so no$-interpretation occurs. (Equivalent to escaping$→$$; the function form is cleaner and covers all sequences.)Tests
Adds
tests/hooks/session-end.test.js— an end-to-end regression test (seeds a marker'd session file + a transcript whose user message contains$&/$$, runs the hook, asserts the text survives verbatim and the markers appear exactly once). Confirmed it fails on the current code and passes with the fix.Summary by cubic
Fixes summary corruption in the session-end hook when user messages include$-sequences (e.g., $ &, $$). Replacements now treat the summary block literally so user text is preserved and markers appear once.
$interpretation byString.prototype.replace.$&,$$,$', and `$`` are preserved and that the summary marker pair appears exactly once.Written for commit d5c3e81. Summary will update on new commits.
Summary by CodeRabbit
Bug Fixes
Tests