Skip to content

Send mentions and files back bare when rich text is edited - #1046

Merged
robzolkos merged 5 commits into
mainfrom
fix-mention-roundtrip
Nov 20, 2026
Merged

robzolkos merged 5 commits into
mainfrom
fix-mention-roundtrip

Conversation

@robzolkos

@robzolkos robzolkos commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

Editing a post that has an @mention or an image in it corrupts the post. Basecamp returns each mention and file attachment expanded, with an avatar or preview <figure> inside the <bc-attachment> and, for a mention, its rendered HTML in a content attribute. When an agent reads a post, changes some words and sends the content back, Basecamp stores that figure as content of its own. The figure breaks the paragraph around the mention apart, and every edit adds another copy.

richTextToHTML, which every rich-text write goes through, now reduces each <bc-attachment> that names a record (it has an sgid) to the bare tag that the rich text docs say to write. It drops the inner HTML and the content attribute and keeps the tag's other attributes, such as caption. This applies with --format html as well as with Markdown. In Markdown, an attachment written inside a code span or code block is left alone. An attachment without an sgid, such as a horizontal rule, carries its own content and is also left alone.

Fixes #905

Verification

  • TestCommentsUpdateCollapsesAMentionCopiedFromTheAPI fails on main in both formats and passes with this change. New unit tests in internal/richtext/attachments_test.go cover file attributes, attachments without an sgid, and code in Markdown.
  • Tested against a local bc3: create a message with a mention, then edit it twice by sending back the content that messages show returned, once with --format html and once with Markdown.
Build After the HTML edit After the Markdown edit
v0.30.0 2 figures 3 figures, paragraph split in two
this branch 1 figure 1 figure, one paragraph

The same test with an uploaded image instead of a mention gave the same result on both builds: v0.30.0 corrupts the post and this branch keeps it intact. The image's attributes come through the edits unchanged.

  • bin/ci passes.

The root cause is on the server, which accepts the expanded form. A server-side fix would cover every API client, but this PR is limited to the CLI.


Summary by cubic

Fixes editing a post with an @mention or file attachment corrupting it by sending the bare <bc-attachment> tag back instead of the expanded form Basecamp returns.

richTextToHTML now reduces every <bc-attachment> naming a record to the write form Basecamp documents, in both HTML and Markdown. It drops the inner <figure> and content attribute but keeps others like caption and filename. Tags are read with an HTML tokenizer and the Markdown parser, so tags inside comments, code, backslash escapes, attribute values, or that are self-closing or unclosed are left as written. A new test covers a tag split across blockquote lines, which must be read from the parser's segments to keep each line's > out of the tag. Fixes #905.

Written for commit d3ed305. Summary will update on new commits.

View guided diff Turn on auto-fix

Basecamp returns a mention or a file attachment expanded: the avatar or
preview as a <figure> inside the <bc-attachment>, and for a mention its
rendered HTML in a content attribute. An agent that edits a post by sending
back the content it read wrote that expanded form, which Basecamp stores
as content of its own: the figure breaks the paragraph around a mention
apart, and every edit adds another copy.

richTextToHTML now reduces every <bc-attachment> that names a record to
the bare tag Basecamp documents for writing one, in both formats. In
Markdown, attachments written inside code are left as they are.

Fixes #905
@robzolkos
robzolkos requested a review from a team as a code owner November 19, 2026 05:08
Copilot AI balanced review requested due to automatic review settings November 19, 2026 05:08
@github-actions github-actions Bot added commands CLI command implementations tests Tests (unit and e2e) labels Nov 19, 2026

Copilot AI 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.

🟡 Changes recommended

Attachment parsing can silently delete intervening text and mentions after self-closing tags and corrupt attributes containing quoted greater-than characters.

2 open issues
What changed in this PR

This PR addresses #905 by normalizing expanded mentions and file attachments before rich-text writes, preventing repeated edits from duplicating previews or breaking paragraphs.

Changes:

  • Reduce attachments with an sgid to bare tags, removing rendered content while preserving other attributes.
  • Apply normalization to HTML and Markdown, preserving Markdown code and attachments without an sgid.
  • Add unit coverage and a comment-update regression test for both formats.

[!TIP]
If you aren't ready for review, convert to a draft PR.
Click "Convert to draft" or run gh pr ready --undo.
Click "Ready for review" or run gh pr ready to reengage.

File Description
internal/​richtext/​attachments.go Adds attachment normalization with Markdown code exclusions.
internal/​richtext/​attachments_test.go Tests normalization, attribute preservation, and excluded content.
internal/​commands/​richtext_format.go Integrates normalization into shared rich-text conversion.
internal/​commands/​richtext_format_test.go Tests editing API-returned mentions in both formats.

Review effort: Balanced

Comment thread internal/richtext/attachments.go Resolved
Comment thread internal/richtext/attachments.go Resolved
A self-closing <bc-attachment/> was taken as the opening tag of the next
attachment, which dropped everything up to that one's closing tag. And the
first '>' was taken to end an opening tag even inside a quoted value. Walk
the tags instead of matching them with a pattern: skip quoted values, leave
self-closing and unclosed tags as written, and search a copy lowered only
in ASCII so its indexes stay those of the original.
Copilot AI balanced review requested due to automatic review settings November 20, 2026 16:47

Copilot AI 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.

🔵 Needs a closer look

Attachment collapsing misinterprets escaped Markdown tags and tags inside HTML comments as real elements, silently deleting literal text or visible content.

No open issues 🎉

2 previously missed issues

In code that hasn't changed since last review

Medium severity Escaped attachment tags are incorrectly collapsed

internal/​richtext/​attachments.go:39

Backslash-escaped attachment tags are literal Markdown, but only code spans and blocks are protected here. For example, Example: \<bc-attachment sgid="x">KEEP THIS TEXT\</bc-attachment> loses KEEP THIS TEXT and the closing tag's escape before Markdown conversion. Restrict collapsing to parser-recognized HTML, or exclude escaped tags while respecting Markdown context and odd/even backslash runs, and add a regression test.

Medium severity Attachment tags inside HTML comments delete visible content

internal/​richtext/​attachments.go:93

The substring search treats attachment markup inside HTML comments as real elements. With --format html, <!-- <bc-attachment sgid="x"> --><p>KEEP THIS TEXT</p><!-- </bc-attachment> --> becomes <!-- <bc-attachment sgid="x"></bc-attachment> -->, silently deleting the visible paragraph. Skip comment contents when locating both opening and closing tags, preferably with HTML tokenization, and add a regression test.

2 resolved since last review

Review effort: Balanced

A tag inside an HTML comment, or a Markdown tag escaped with a backslash,
is text rather than markup, but was read as an attachment, and collapsing
it deleted what lay between it and its closing tag. Pass both by when
looking for opening and closing tags.
@robzolkos
robzolkos requested a balanced review from Copilot November 20, 2026 16:59
@robzolkos

Copy link
Copy Markdown
Collaborator Author

Copilot's review of c203354 raised two cases in its summary, with no inline threads. Both are fixed in 8c49be3:

  • Escaped Markdown tags: a < after an odd run of backslashes is literal Markdown, so neither an escaped opening nor an escaped closing tag counts as markup (TestCollapseAttachmentsInMarkdownLeavesEscapedTags).
  • Tags inside HTML comments: comment contents, including an unterminated comment, are skipped when finding both opening and closing tags (TestCollapseAttachmentsLeavesTagsInComments).

Both tests fail on c203354 and pass now. bin/ci is green locally.

Copilot AI 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.

🔵 Needs a closer look

Attachment normalization can delete legitimate text by treating quoted attribute content and escaped HTML blocks as real attachment markup, and can leave preview markup behind.

No open issues 🎉

2 previously missed issues

In code that hasn't changed since last review

Medium severity Ignore attachment tags inside quoted HTML attributes

internal/​richtext/​attachments.go:149

Tag discovery treats attachment markup inside another tag's quoted attributes as real elements, so <span title='<bc-attachment sgid="x">'>KEEP THIS TEXT</span><span title='</bc-attachment>'>after</span> loses KEEP THIS TEXT and the intervening markup. A closing-tag string inside an image's alt attribute can also end a real attachment prematurely, leaving preview markup behind. Track surrounding tag and attribute boundaries before pairing attachment tags, and add regression tests for both cases.

Medium severity Preserve attachments in escaped HTML blocks

internal/​richtext/​attachments.go:271

Only Markdown code nodes are protected, but renderHTMLBlock also renders unsupported HTML blocks entirely as escaped text (richtext.go:386–420). Consequently, Markdown input such as <section><bc-attachment sgid="x">KEEP THIS TEXT</bc-attachment></section> loses KEEP THIS TEXT before the renderer escapes the block, even though no attachment would have been emitted. Please also protect HTML blocks that the renderer will escape, so normalization only removes children of actual outgoing attachments.

Review effort: Balanced

A hand-rolled scan took attachment markup inside another tag's attribute
value for a tag, and in Markdown collapsed an attachment inside an HTML
block that the renderer shows as escaped text. Read HTML with x/net/html's
tokenizer, which already knows comments and quoted values, and in Markdown
take only the tags the renderer writes as markup: inline raw HTML and the
HTML blocks it passes through. Code, escapes and escaped blocks then need
no rules of their own.
@robzolkos
robzolkos requested a balanced review from Copilot November 20, 2026 17:12
@robzolkos

Copy link
Copy Markdown
Collaborator Author

Copilot's review of 8c49be3 raised two more cases in its summary: attachment markup inside another tag's attribute value, and attachments in Markdown HTML blocks that the renderer escapes. Instead of adding more rules to the hand-written scanner, cc072da finds tags the way the output is actually read:

  • HTML: golang.org/x/net/html's tokenizer (already a dependency), so comments, quoted attribute values and self-closing tags are handled as HTML handles them.
  • Markdown: only tags the renderer writes as markup, meaning inline raw HTML nodes and the HTML blocks renderHTMLBlock passes through. Code, backslash escapes and escaped HTML blocks hold no tags.

New tests TestCollapseAttachmentsIgnoresTagsInAttributeValues (including a </bc-attachment> in an alt) and TestCollapseAttachmentsInMarkdownLeavesEscapedHTMLBlocks fail on 8c49be3 and pass now. Every earlier test passes unchanged, and bin/ci is green locally.

Copilot AI 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.

🟡 Changes recommended

Multiline attachment tags in blockquotes are parsed incorrectly, silently dropping content-type and caption attributes when blockquote prefixes are reintroduced.

1 open issue

Review effort: Balanced

Comment thread internal/richtext/attachments.go Resolved
A tag split across blockquote lines was read from the source between its
first and last segment, which put each line's "> " back, and the
tokenizer took that '>' for the tag's end. Tokenize the segments joined,
mapping each byte back to the source, and rebuild the tag from what was
read.
Copilot AI balanced review requested due to automatic review settings November 20, 2026 17:27

Copilot AI 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.

🟢 Approved

No active review issues were identified for the attachment and mention preservation changes.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@robzolkos
robzolkos merged commit 43e18b9 into main Nov 20, 2026
32 checks passed
@robzolkos
robzolkos deleted the fix-mention-roundtrip branch November 20, 2026 17:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

commands CLI command implementations tests Tests (unit and e2e)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Editing a comment with a mention breaks the mention: comments show returns rendered mention HTML that comments update cannot take back

2 participants