Skip to content

mergeTooShort() never merges a short first segment#2615

Open
shriii257 wants to merge 1 commit into
tinyhumansai:mainfrom
shriii257:patch-1
Open

mergeTooShort() never merges a short first segment#2615
shriii257 wants to merge 1 commit into
tinyhumansai:mainfrom
shriii257:patch-1

Conversation

@shriii257
Copy link
Copy Markdown
Contributor

@shriii257 shriii257 commented May 25, 2026

The function merges short segments into the preceding one using this guard: if (result.length > 0 && part.length < MIN_SEGMENT_CHARS) When the very first segment is short (e.g. "Ok."), result.length === 0, so it's pushed as-is. The second segment then passes the length check and gets pushed separately — leaving a tiny stub bubble as the first chat message. Fix: After the main loop, if result[0] is still below the minimum, forward-merge it into result[1].

Summary

  • What changed and why.
  • Keep this to 3-6 bullets focused on user-visible or architecture-impacting changes.

Problem

  • What issue or risk this PR addresses.
  • Include context needed for reviewers to evaluate correctness quickly.

Solution

  • How the implementation solves the problem.
  • Note important design decisions and tradeoffs.

Submission Checklist

If a section does not apply to this change, mark the item as N/A with a one-line reason. Do not delete items.

  • Tests added or updated (happy path + at least one failure / edge case) per Testing Strategy
  • Diff coverage ≥ 80% — changed lines (Vitest + cargo-llvm-cov merged via diff-cover) meet the gate enforced by .github/workflows/coverage.yml. Run pnpm test:coverage and pnpm test:rust locally; PRs below 80% on changed lines will not merge.
  • Coverage matrix updated — added/removed/renamed feature rows in docs/TEST-COVERAGE-MATRIX.md reflect this change (or N/A: behaviour-only change)
  • All affected feature IDs from the matrix are listed in the PR description under ## Related
  • No new external network dependencies introduced (mock backend used per Testing Strategy)
  • Manual smoke checklist updated if this touches release-cut surfaces (docs/RELEASE-MANUAL-SMOKE.md)
  • Linked issue closed via Closes #NNN in the ## Related section

Impact

  • Runtime/platform impact (desktop/mobile/web/CLI), if any.
  • Performance, security, migration, or compatibility implications.

Related

  • Closes:
  • Follow-up PR(s)/TODOs:

AI Authored PR Metadata (required for Codex/Linear PRs)

Keep this section for AI-authored PRs. For human-only PRs, mark each field N/A.

Linear Issue

  • Key:
  • URL:

Commit & Branch

  • Branch:
  • Commit SHA:

Validation Run

  • pnpm --filter openhuman-app format:check
  • pnpm typecheck
  • Focused tests:
  • Rust fmt/check (if changed):
  • Tauri fmt/check (if changed):

Validation Blocked

  • command:
  • error:
  • impact:

Behavior Changes

  • Intended behavior change:
  • User-visible effect:

Parity Contract

  • Legacy behavior preserved:
  • Guard/fallback/dispatch parity checks:

Duplicate / Superseded PR Handling

  • Duplicate PR(s):
  • Canonical PR:
  • Resolution (closed/superseded/updated):

Summary by CodeRabbit

  • Bug Fixes
    • Fixed an issue where the first chat message bubble could display as a short, isolated segment. Enhanced the message segmentation system to properly merge short initial segments with subsequent ones, resulting in improved message formatting, better visual consistency, and a more cohesive appearance in conversations.

Review Change Stack

The function merges short segments into the preceding one using this guard:
if (result.length > 0 && part.length < MIN_SEGMENT_CHARS)
When the very first segment is short (e.g. "Ok."), result.length === 0, so it's pushed as-is. The second segment then passes the length check and gets pushed separately — leaving a tiny stub bubble as the first chat message.
Fix: After the main loop, if result[0] is still below the minimum, forward-merge it into result[1].
@shriii257 shriii257 requested a review from a team May 25, 2026 09:59
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 25, 2026

📝 Walkthrough

Walkthrough

The PR fixes message segmentation logic to prevent short "stub" segments from appearing as the first chat bubble. The mergeTooShort utility now merges an initial short segment into the second segment if the first remains below the minimum character threshold after the existing backward-merge pass.

Changes

Message Segmentation First-Segment Fix

Layer / File(s) Summary
Forward-merge fix for short first segments
app/src/utils/messageSegmentation.ts
mergeTooShort detects when the first segment is still shorter than MIN_SEGMENT_CHARS after backward merging and, if a second segment exists, concatenates them with the joiner and removes the short stub. Block comment is expanded to document this behavior.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 A hop-skip through segments we go,
No tiny stubs left high or low,
First bubbles merge when they're too wee,
Segmented chat now flows so free! 📨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main fix: adding logic to merge short first segments that were previously never merged due to the result.length guard condition.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

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

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint skipped: no ESLint configuration detected in root package.json. To enable, add eslint to devDependencies.


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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
app/src/utils/messageSegmentation.ts (1)

84-89: ⚡ Quick win

Add a regression test for the new first-segment forward-merge path.

The logic is correct, but this specific edge case should be locked with a test to prevent regressions.

🧪 Suggested test addition
diff --git a/app/src/utils/__tests__/messageSegmentation.test.ts b/app/src/utils/__tests__/messageSegmentation.test.ts
@@
   it('merges short paragraphs with the previous one', () => {
@@
     expect(segments.every(s => s.length >= 40)).toBe(true);
   });
+
+  it('merges a short first paragraph into the second paragraph', () => {
+    const text =
+      'Ok.\n\n' +
+      'This second paragraph is intentionally long enough to stand alone after merging.';
+    const segments = segmentMessage(text);
+
+    expect(segments.length).toBe(1);
+    expect(segments[0].startsWith('Ok.\n\n')).toBe(true);
+    expect(segments[0].length).toBeGreaterThanOrEqual(40);
+  });
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@app/src/utils/messageSegmentation.ts` around lines 84 - 89, Add a regression
unit test that exercises the first-segment forward-merge branch in
messageSegmentation (the code that uses MIN_SEGMENT_CHARS, result and joiner) by
providing input that yields at least two candidate segments where the first
segment length is < MIN_SEGMENT_CHARS, then assert that the output has one fewer
segment and that result[0] equals the original first + joiner + original second
(i.e., the two were merged), and also assert no leading short stub remains; name
the test to indicate "first-segment forward-merge" and place it alongside
existing messageSegmentation tests.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@app/src/utils/messageSegmentation.ts`:
- Around line 84-89: Add a regression unit test that exercises the first-segment
forward-merge branch in messageSegmentation (the code that uses
MIN_SEGMENT_CHARS, result and joiner) by providing input that yields at least
two candidate segments where the first segment length is < MIN_SEGMENT_CHARS,
then assert that the output has one fewer segment and that result[0] equals the
original first + joiner + original second (i.e., the two were merged), and also
assert no leading short stub remains; name the test to indicate "first-segment
forward-merge" and place it alongside existing messageSegmentation tests.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: ccac0546-21ee-48ae-950b-63246b05e798

📥 Commits

Reviewing files that changed from the base of the PR and between d997394 and 30f45d6.

📒 Files selected for processing (1)
  • app/src/utils/messageSegmentation.ts

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