Skip to content

fix(chatbridge): drop nested code fences whole in chat bridge excerpts - #958

Open
BaaRai wants to merge 1 commit into
mainfrom
fix/safetext-nested-fences
Open

BaaRai wants to merge 1 commit into
mainfrom
fix/safetext-nested-fences

Conversation

@BaaRai

@BaaRai BaaRai commented Dec 3, 2026

Copy link
Copy Markdown
Owner

Summary

chatbridge.SafeText paired any run of three backticks or tildes with the next one of either kind, so a longer fence quoting markdown was closed at the first inner run: a four-backtick fence around a three-backtick http block, a markdown fence holding inline triple backticks, or a backtick block inside a tilde fence. The rest of the quoted evidence then went to the chat provider as prose when excerpts or shared text were on.

Changes

  • Fences follow the CommonMark rules: a fence line has at most three spaces before its run, and its block ends only on a line holding a run of the same character at least as long and nothing else but spaces or tabs. Runs of the other character, shorter runs and runs followed by text are content.
  • A run inside a list item or a blockquote is content of the fence that is open. Such a fence ends only on the first valid closing line of the same character and length; a list item's two-space or three-space continuation lines no longer end it. An unclosed fence drops to the end of the text.
  • A run elsewhere (after text, or in a title collapsed to one line) ends at the latest end a code span could have; a fence line met in a paragraph starts its own block.
  • Ambiguous markdown errs toward dropping.

Tests

  • chatbridge/outbound_nested_fence_test.go: four- and five-backtick fences, tilde and backtick mixes, blockquotes, unordered list items (2-space continuation), ordered list items (3-space continuation), a longer closing run, unclosed fences, a collapsed one-line case, and inline triple-backtick spans.
  • The two list-item cases fail on the previous head with the leaked line and pass here; go test -race ./chatbridge passes.

Behaviour change

Ambiguous markdown errs toward dropping: a fence opened on the same line as a list marker may drop the list items after it.

SafeText paired any run of three backticks or tildes with the next run of
either kind, so a longer fence quoting markdown was closed at its first inner
run and the rest of the quoted evidence reached the chat provider as prose.

Fences now follow the CommonMark rules: a fence line has at most three spaces
before its run, and its block ends only on a line holding a run of the same
character at least as long and nothing else; an unclosed fence drops to the
end. Other runs (after text, inside a blockquote or list item, in text
collapsed to one line) are dropped up to the latest end a code span or a
container fence could have, and a fence line met first starts its own block.
Ambiguous markdown errs toward dropping.
@BaaRai
BaaRai force-pushed the fix/safetext-nested-fences branch from d155fc7 to a70c0d7 Compare December 4, 2026 00:51
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