Skip to content

fix(chat): keep a mid-sentence triple backtick as prose - #17539

Merged
iamwhatever merged 1 commit into
kirodotdev:mainfrom
devreeda:fix/chat-midline-fence
Oct 13, 2026
Merged

iamwhatever merged 1 commit into
kirodotdev:mainfrom
devreeda:fix/chat-midline-fence

Conversation

@devreeda

@devreeda devreeda commented Oct 6, 2026 •

Copy link
Copy Markdown

Problem / Motivation

Goal: A triple backtick in the middle of a sentence renders as plain text in chat, instead of opening a code block that hides the rest of the reply.

When an assistant reply mentions a fence inside a sentence, such as "edits (which need a ```diff block). Let me produce...", the chat cuts the line at the backticks. A code card follows that either swallows the prose after it or shows the word undefined. A list item such as "Type ``` then Enter" breaks the same way.

Why it matters

Agents often mention fences in prose, for example when they describe a diff. Each time, the reader loses part of the reply with no sign that text is missing.

Not a goal

  • The separate rule that splits a triple backtick followed by a capital letter.
  • The extra code card that main already renders after a complete glued fence (its closing line is read as a new fence).
  • Empty-fence rendering in Mochi.

What changed (motivation → approach → change)

fixCodeFences repairs fences that a model glued to text, such as Here is code:```python. Its regex took everything after the backticks as the info string, so a run in the middle of a sentence was moved onto its own line and became a fence opener.

The repair now runs only when the run is followed by one tag, an optional {...} attribute block, and the end of the line. A run followed by more words stays prose. Mochi's copy of the regex (fixStreamingFences) gets the same rule. The code renderer also shows an empty fence as empty instead of undefined.

The issue suggested repairing only when a closing fence follows. That cannot work here: parseBlocks takes line-start fence lines out of the block before fixCodeFences runs, so the closer of a real glued fence is never visible to it. A check over the whole message is fooled by a later, unrelated block, and a fence that is still streaming has no closer yet.

Backwards compatibility

Compatible: line-start fences, and glued fences with a bare tag, a leading space or a {...} block, render as before. Every existing test passes unchanged.

One repair narrows: a glued fence with other attributes, such as js title="a.js" right after text, now renders as text. No producer in this repository writes that shape.

Reader: website/src/components/MarkdownRenderer.tsx:MarkdownBlock -- chat -- fixCodeFences.test.ts, MarkdownRendererCoverage.test.tsx
Reader: website/src/apps/mochi/src/renderer/ChatPanel.tsx:StreamingMarkdown -- app -- mochiStreamingFences.test.ts

Tests

  • fixCodeFences.test.ts: the issue's line passes through unchanged when it ends the message, when prose follows it, and when a real block with or without a tag follows it. So does the list item. A glued fence whose closer has not streamed in yet is still split.
  • MarkdownRendererCoverage.test.tsx: through the full renderer, the line stays prose next to a real block, a streaming glued fence still renders as code, and an empty fence does not show undefined.
  • mochiStreamingFences.test.ts: the same prose and streaming cases for Mochi's copy.

Manual verification

Rendered one assistant reply holding the issue's lines and a glued Python fence through the real AssistantMessage component, on main and on this branch. Screenshots below.

Screenshots / video

Before (main): the prose is cut off and two cards read undefined.

Before: the chat reply on main

After (this branch): every line is prose, and the glued Python fence still renders as code.

After: the same reply on this branch

Related Issues

Closes #14554

Pattern harvest

Rule candidate: review-prompt
Pattern: a markdown repair regex that matches a fence marker anywhere in a line, where CommonMark cannot open a fence.

Checklist

  • At most two commits (one is the norm), with a Conventional Commits title (feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...)
  • Existing tests pass and new tests added for new functionality
  • Self-review completed; code follows project style guidelines
  • Documentation updated (if applicable)
  • No secrets, credentials, or internal references in the diff

Contribution License Agreement

Contributed under the project's Apache-2.0 license.

@devreeda
devreeda marked this pull request as ready for review October 6, 2026 21:59
@devreeda
devreeda requested a review from a team as a code owner October 6, 2026 21:59
@devreeda
devreeda requested a review from CrysisDeu October 6, 2026 21:59
@github-actions github-actions Bot added fork Pull request from a fork (external contributor) readiness: checking Automated validation is still running labels Oct 7, 2026
@github-actions

github-actions Bot commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

UX Review (Opus 5.5, fork) — 🟡 CONCERNS

UX-level review of bc75fd295d19ea5e888a7af9d97ff6aecea6bfec via the fork AI-review pipeline — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

UX-Verdict: CONCERNS

No blind reader has seen these screenshots, and two changed rendering paths (the empty fence and Mochi's chat) appear in none of them.

Watch

  • Empty fence. The diff changes String(children ?? ''), so an empty code block now renders as an empty card where it used to say "undefined". Neither attachment shows an empty fence, so nobody has seen what an empty card looks like: whether it is a header over a blank row or a collapsed sliver. This rarely comes up and does no harm, but it is unverified. Fix: add one still of a ``` / ``` pair rendered on this branch.
  • Mochi. fixStreamingFences gets the same rule, but both screenshots come from the dashboard's AssistantMessage. Nobody has seen Mochi's streaming chat with a fence mentioned mid-sentence. Because Mochi copies the regex, a mismatch would show up only there. Fix: attach a Mochi capture of the issue's line, or say in the description why the dashboard capture covers it.

[UX-REVIEWED] bc75fd2

Description read

[DESCRIPTION-READ] e77d537408c283534da82a4ff7e303cec48ef9567b2a9a6f90b2c4b5a871082d

That sha256 names everything this verdict read: the PR description, plus the 5 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with .github/scripts/pr-description-capture.sh over the same inputs; if it differs, the description or that evidence changed after this verdict was formed and any finding drawn from either is unproven.

@github-actions

github-actions Bot commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5, fork) — ✅ PASS

Design-level review of bc75fd295d19ea5e888a7af9d97ff6aecea6bfec via the fork AI-review pipeline — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

Design-Verdict: PASS

Check that a sentence ending in a bare tag, like "use a ```diff", still opens a fence when that is not intended.

[DESIGN-REVIEWED] bc75fd2

Description read

[DESCRIPTION-READ] 3e6764c3615ec1cea84b0d1a36dd2e7e36053309894eba3a13c2d1ccb4c0782c

That sha256 names everything this verdict read: the PR description, plus the 1 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with .github/scripts/pr-description-capture.sh over the same inputs; if it differs, the description or that evidence changed after this verdict was formed and any finding drawn from either is unproven.

@github-actions

github-actions Bot commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Opus 5.5 Review (fork) — ✅ no blocking findings

Reviewed bc75fd295d19ea5e888a7af9d97ff6aecea6bfec via the fork AI-review pipeline; updated in place on each push.

Review details

No findings.

[OPUS-REVIEWED] bc75fd2

@github-actions

github-actions Bot commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5, fork) — 🟡 CONCERNS

Premise-level review of bc75fd295d19ea5e888a7af9d97ff6aecea6bfec via the fork AI-review pipeline — why this exists and whether the shipped surface is the smallest honest version. Updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

First-Principles-Verdict: CONCERNS

The fix copies the same new regex into two places instead of Mochi using the dashboard's, and Mochi already imports from MarkdownRenderer.

Not justified as shipped

  • Item 2 — duplicate of website/src/components/MarkdownRenderer.tsx:fixCodeFences: the diff writes the identical pattern /([^\n])(\n?)(```(?:[ \t]*[^\s]+)?(?:[ \t]+{[^\n]*\})?[ \t\r]*\n)/g into both files. A grep for the old pattern [^\n]* ``` found exactly 2 copies. Mochi's ChatPanel.tsx:42already imports from../../../../components/MarkdownRenderer. Its own comment says it should be "matching the dashboard's fixCodeFences", so a hand-synced twin is how the two spellings drift apart again. Subtraction: delete Mochi's regex literal and have fixStreamingFencesuse the dashboard's glue pattern through the import it already has. The second hand-written copy goes away. Clears when: one spelling of the glue regex is left inwebsite/src`.
  • Item 4 — undeclared: fixStreamingFences is newly exported, and its only consumer is the new test file (one consumer, a test).

What this change ships

Inventory (5 items) — 2 justified

Intent: stop a triple backtick in the middle of a sentence from opening a code block that hides the rest of a chat reply. This is a FIX.

  1. A mid-sentence ``` in dashboard chat stays prose, and a glued fence with a bare tag or {...} is still split. — justified
  2. The same rule in Mochi's streaming renderer. — duplicate of MarkdownRenderer.tsx:fixCodeFences
  3. An empty fence renders empty instead of "undefined". — rides along
  4. fixStreamingFences becomes an exported symbol. — undeclared
  5. A glued fence with other attributes (js title=…) after text now renders as text. — justified

[FIRST-PRINCIPLES-REVIEWED] bc75fd2

Description read

[DESCRIPTION-READ] c2586701e6f62e0111044f65315a04271875616fc10ac487d3c7d3f0c2e8af19

That sha256 names the PR description this verdict read. Recompute it with .github/scripts/pr-description-capture.sh; if it differs, the description changed after this verdict was formed and any finding drawn from the description is unproven.

@github-actions

github-actions Bot commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

GPT 6.1 Review (fork) — ✅ no blocking findings

Reviewed bc75fd295d19ea5e888a7af9d97ff6aecea6bfec via the fork AI-review pipeline; updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] bc75fd2

@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention and removed readiness: checking Automated validation is still running labels Oct 7, 2026
@devreeda

devreeda commented Oct 7, 2026

Copy link
Copy Markdown
Author

CI failed on two flakes outside this change:

  • Frontend Tests (1): every test passed (662 files, 12,202 tests), but a Vitest worker was killed mid-run with "Worker exited unexpectedly". That is the coverage OOM case noted in website/vite.config.ts. Frontend Coverage Merge and Coverage Gate failed only because of it.
  • Backend Tests (3.12, 5): test_free_port_is_not_hardcoded got port 38331 twice, which is Flaky: test_free_port_is_not_hardcoded asserts two ephemeral-port samples differ #14011.

I can't re-run jobs from a fork. Could a maintainer re-run the failed jobs in https://github.com/kirodotdev/KiroCrew/actions/runs/37536744398?

@devreeda

devreeda commented Oct 9, 2026

Copy link
Copy Markdown
Author

@bolichen97 @iamwhatever @CrysisDeu friendly ping: this has been waiting two days. The fix is a small T1 change for #14554, and all five AI reviews pass. The only red checks are two known flakes outside this change (a Vitest worker OOM where all 12,202 tests passed, and test_free_port_is_not_hardcoded from #14011). Could someone re-run the failed jobs and take a look? Thanks.

The glued-fence repair in fixCodeFences treated any text after a
triple-backtick run as the info string. A run in the middle of a
sentence was moved onto its own line and opened a code block, which
hid the rest of the reply or rendered "undefined".

Repair a glued fence only when the run is followed by one tag, an
optional {...} attribute block and the end of the line. Apply the
same rule to Mochi's copy, and render an empty fence as empty.

Closes kirodotdev#14554
@devreeda
devreeda force-pushed the fix/chat-midline-fence branch from 3956d5c to bc75fd2 Compare October 13, 2026 10:05
@devreeda

Copy link
Copy Markdown
Author

@iamwhatever since you reviewed #18405 (same fixCodeFences / useBlockAssembler area): this PR is the mid-sentence triple-backtick case from #14554, which #18405 does not cover. I rebased it onto current main: no conflicts, and all 1376 related tests pass, including #18405's. The new mid-sentence tests still fail on main without this change. Could you take a look, and approve the CI run if needed?

@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: passed Eligible automated validation passed for the current revision and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Oct 13, 2026

@iamwhatever iamwhatever left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This PR fixes #14554, where a triple backtick in the middle of a sentence was moved onto its own line and opened a code card that swallowed the rest of the reply. The root cause was the glue-repair regex in fixCodeFences (MarkdownRenderer.tsx:650) and its Mochi copy (ChatPanel.tsx:1872), which treated everything after the backticks as the info string. Both now repair only when the run is followed by a single tag, an optional {...} block, and the end of the line. I traced the new pattern by hand: "```diff block ..." cannot match through backtracking because it needs { or a line end after the tag, line-start fences keep their `\n?` path and pass through unchanged, and a glued fence that is still streaming is still split. The one narrowing (a glued `js title="a.js"` now stays text) is declared in the description, and I found nothing that produces that shape. The String(children ?? '') change only removes the literal "undefined" from an empty fence. The new tests would fail on base, all checks have a SUCCESS, and the First Principles note about the duplicated Mochi regex is fair advice but describes duplication that already existed, not a defect. — crew-pr-reviewer (deep re-check)

@iamwhatever
iamwhatever enabled auto-merge (squash) October 13, 2026 16:51
@iamwhatever
iamwhatever merged commit 81d8250 into kirodotdev:main Oct 13, 2026
104 checks passed
@github-actions github-actions Bot removed the readiness: passed Eligible automated validation passed for the current revision label Oct 13, 2026
@devreeda devreeda mentioned this pull request Oct 20, 2026
5 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fork Pull request from a fork (external contributor)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

chat: a mid-sentence triple backtick hides the rest of the line and renders a code card reading "undefined"

2 participants