Skip to content

AI-3226: delete a branch copy when Chat deletes its message - #2677

Merged
Loschcode merged 7 commits into
mainfrom
laurentschaffner/ai-3226-delete-a-branch-snapshot-when-its
Nov 27, 2026
Merged

Loschcode merged 7 commits into
mainfrom
laurentschaffner/ai-3226-delete-a-branch-snapshot-when-its

Conversation

@Loschcode

@Loschcode Loschcode commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Why

https://linear.app/stream/issue/AI-3226/delete-a-branch-snapshot-when-its-hidden-chat-message-is-deleted

AI-3198 keeps a router-side copy of every Chat message a regenerate, edit or branch switch hides. A server-side delete of a hidden message never reached the router, so the copy was still shown on ‹ i/n › and sent again when its branch was activated. AI-3198 shipped with that as a documented limitation.

What changed

  • message.deleted is handled on both paths: Stream Chat's signed delivery (receiveChatInbound) and the message webhook (receiveMessageEvent). Both call receiveDeletedMessage (internal/api/messagedeleted.go).
    • It runs on the request and is not queued. The answer is 200, or 503 when the delete fails, so Stream retries.
    • It never answers anything and skips addressedIn and the chat_inbound queue.
    • It is not checked for staleness. Chat retries a delivery for 30 minutes and a hook goes stale after 10, so a stale check would lose late deletes. A replayed delete deletes nothing.
    • deleted_for_me deletes nothing.
  • BranchMessages.DeleteMessage (internal/store/branch_message.go) deletes the copies of that message id in the sessions on the conversation (conversation_id = <channel_type>:<channel_id>) within the hook's app (AppScope on stream_app_pk). When the hook names a customer, it is limited to that customer's sessions.
    • It empties the data_changes of those copies too, as DeleteUser and Sessions.Delete do, because they hold the same words.
    • It is idempotent, and a message with no copy deletes 0 rows.
    • With shards it queues a new shard job, delete_branch_message, and runs this shard's part at once, as DeleteUser does. Other shards run theirs from their workers. shard_jobs.kind has no CHECK constraint, so no migration is needed. A router that does not know the kind leaves its run queued until it is upgraded.
    • EXPLAIN on PG 19: with a customer, it uses agent_sessions_conversation_idx plus the PK. Without one (a hook from the deployment's own app), it scans agent_branch_messages_pkey on original_message_id and joins agent_sessions_pkey. Neither plan is a sequential scan.
  • The chain of copies an activation sent again. An activation sends copy m1 again as a new message m2 with custom.resent_from = m1, and chosenFor shows the newest copy per root. A delete therefore takes every copy on that chain within the conversation, seeded from the deleted message's own resent_from. This covers a hidden m2, and a visible m2, which has no copy of its own.
  • A deleted question blanks its response's said, so the switcher does not show the question's text.
  • Cost. The conversation's copies are read first. The delete uses RETURNING, and data_changes and said are updated only for the rows it returns, so a delete with no copy writes nothing. On shards the job is queued only when session_index lists a session on the conversation on another shard. With no customer, the index cannot be read, so the job is still queued. No migration was needed: on PG 19 the no-customer path finds its sessions by a skip scan of agent_sessions_conversation_idx.
  • Resuming an activation. It now sends what Chat does not hold yet (alreadyResent) instead of resuming at the positional count, because a copy deleted mid-way shifts the rest. An unfinished activation sends what is still kept of its branch and finishes with nothing to send only when nothing is kept. Before, the ErrBranchGone from the target's own copies failed the resume.
  • Race, left as is and commented. A switch that read a message from Chat before it was deleted can copy it after the delete ran. That copy stays until the session is deleted, a memory truncate, or forget-user.
  • Docs. The receiveChatDelivery description is updated in openapi.yaml, and the Go and JS clients are regenerated (description text only). The JS README's branch limits now say that deleting a hidden message through Chat deletes the router's copy.
  • Hooks need no change. Chat's agents-router delivery does not depend on the app's event hooks. The router's own message webhook has no event-type filter, so it already receives message.deleted.

Not covered (follow-up AI-4307)

  • Chat's bulk deletes send no per-message message.deleted, so they do not reach the router: user deletion, truncate, channel delete, ban with delete, and moderation actions.
  • A delete the router could not take within Chat's 30-minute delivery window is dropped.
    Both are stated in the JS README.

Deploy order

Deploy this PR first, then GetStream/chat#21956.

  • That PR has Chat send agent-channel message.deleted through the agents-router delivery.
  • Without it, this PR still covers apps whose message webhook points at the router: Chat already sends message.deleted for rewound messages to webhooks.
  • If Chat shipped first, older routers would answer 200 to the delete and drop it.

Tests

  • internal/store/branch_message_test.go (StoreSuite and ShardedStoreSuite):
    • A copy is deleted only in its own conversation and app.
    • With a customer named, another customer's copy is kept. In the deployment's app, the unpinned and pinned copies are deleted.
    • A message with no copy, or one deleted again, deletes nothing.
    • The recorded changes are emptied.
    • Both a conversation and a message id are required.
    • With shards, this shard deletes at once and the other shard deletes once its job runs.
    • The whole chain of copies sent again is deleted (m1 to m2 to m3).
    • Deleting a visible message that was sent again deletes the copy it was sent from.
    • A copied question's response loses its said, and an answer's copy leaves said alone.
    • With no session on another shard, no job is queued.
  • internal/api/chatinbound_deleted_router_test.go:
    • A delivered delete removes the copy and queues nothing.
    • A deleted_for_me keeps the copy.
    • A delete with no copy is a 200.
    • A webhook delete removes the copy.
    • A closed store answers 503, and the retried delivery deletes.
    • App mode deletes only its own app's copy.
    • Deleting a visible message that was sent again (resent_from in the event) deletes the copy it was sent from.
    • A delivery past hookStaleAfter still deletes.
    • A webhook delete that arrives before the deployment knows its own app is a 503 and changes nothing. Once the app is known, the retried delete removes the copy of a pinned session.
  • internal/conversation/branches_switch_test.go: an activation stopped mid-way whose sent copy was deleted sends the rest. It fails with the positional resume.
  • internal/session/branches_test.go: an unfinished activation whose copies were deleted has nothing to send. It fails without the ErrBranchGone mapping. Also: an unfinished activation whose target's own copies were deleted still sends the rest of its branch. That test fails when the resume answers the empty set.
  • Fail without the fix:
    • With the handler wiring reverted, the five delete tests fail. The deleted_for_me and no-copy tests pass, since they guard against deleting too much.
    • The chain and said tests (store and handler) fail with the delete limited to the message id and without the said update, which is the previous head's behaviour. Mutants are caught: dropping the deleted_for_me check, the own-app wait, the app scope, the customer filter, the data_changes CTE, or the shard-job run each fails at least one test.
  • go test -tags integration ./internal/api -run 'TestChatInbound|TestMessageHook|TestBranch', go test ./internal/api and go vet are clean.

Overlapping open PRs, with small diffs here: #2532 and #2635 (chatinbound.go / messagehooks.go), #2381 (handwritten.go), #2167 and #2667 (sdks/js/README.md).

🤖 Generated with Claude Code

A message.deleted from Stream Chat's delivery or the message webhook now
deletes the router's copies of that message: those of the sessions on its
conversation in the hook's app (and customer, when the hook names one),
with the changes recorded of them emptied, on every shard through a shard
job. A delete_for_me deletes nothing. It is done on the request and not
checked for staleness, so a late or repeated delivery still deletes, and a
failed delete is a 503 Stream delivers again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Loschcode

Copy link
Copy Markdown
Contributor Author

/jeffery review

@getstream-metis

Copy link
Copy Markdown

Thanks for asking Jeffery for review. I'll stay on this PR until I can approve it.

How it works

  • Findings come as inline comments, each starting with [Must Fix], [Should Fix], nit: or consider:.
  • Push fixes whenever you're ready. Once the branch has been quiet for 10 minutes I review the new head and resolve the threads it fixes. To skip the wait, comment /jeffery review.
  • If you disagree or have a question, reply in the thread. I'll answer there, and resolve it if you're right.
  • I approve once every must-fix and should-fix is closed and nobody has requested changes. CI isn't part of that; branch protection still holds the merge until it's green. From round 3 on, open should-fix findings stop blocking.
  • I don't approve release/* branches or changes to protected paths, and I'll say so when that applies.
  • If a must-fix is still open after round 8, I stop and hand the PR to a human. Draft PRs wait until they're marked ready.

Commands (each as a whole comment)

Command What it does
/jeffery review Review the current head now
/jeffery rereview Start over at round 1 on the current head and withdraw my approval. This redoes the whole review and costs as much as the first one, so use it only when you don't trust the earlier rounds. If nothing changed since my last round I say so instead; add force to redo it anyway
/jeffery pause / /jeffery resume Stop or restart automatic reviews on this PR
/jeffery status What I last reviewed, and why I have or haven't approved
/jeffery help This list

/review-agent or a mention of me works in place of /jeffery.

@getstream-metis getstream-metis Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Round 1 on dc4035ce38d9 · 🆕 2 new

getstream-metis[bot]
getstream-metis Bot previously approved these changes Nov 27, 2026

@getstream-metis getstream-metis Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ Approved dc4035ce38d9 after 1 round

Left open, your call:

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Loschcode

Copy link
Copy Markdown
Contributor Author

/jeffery review

@getstream-metis
getstream-metis Bot dismissed their stale review November 27, 2026 14:52

Withdrawing approval until a94a841 is reviewed.

getstream-metis[bot]
getstream-metis Bot previously approved these changes Nov 27, 2026

@getstream-metis getstream-metis Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ Approved a94a8414b9b3 after 2 rounds · 1 fixed · 1 dropped after discussion

Laurent Schaffner and others added 3 commits November 27, 2026 16:11
…estion

An activation sends a copy again as a new message naming it (resent_from),
and the newest copy of a chain is what a branch shows. A delete now takes
every copy on the chain within the conversation, seeded from the deleted
message's own resent_from, so a deleted message sent again is not brought
back from the copy it was sent from. A deleted question's response no
longer says what was asked. The delete reads the conversation's copies,
deletes with RETURNING and forgets only what it deleted; on shards it
queues the job only when the session index places the conversation on
another shard.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A switch finished again now sends what Chat does not hold yet instead of
what follows the count, since a copy deleted after it began shifts the
rest, and one whose copies are all gone finishes with nothing to send.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@getstream-metis
getstream-metis Bot dismissed their stale review November 27, 2026 15:12

Withdrawing approval until 7f7223b is reviewed.

@getstream-metis getstream-metis Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Round 3 on 7f7223b04bb8 · 🆕 1 new
Still open: 1 should-fix. Full list on the checks tab.

getstream-metis[bot]
getstream-metis Bot previously approved these changes Nov 27, 2026

@getstream-metis getstream-metis Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ Approved 7f7223b04bb8 after 3 rounds · 1 fixed · 1 dropped after discussion

Left open, your call:

@Loschcode

Copy link
Copy Markdown
Contributor Author

/jeffery review

@getstream-metis

Copy link
Copy Markdown

/jeffery review

Nothing changed since round 3 on 7f7223b04bb8; comment /jeffery rereview force to redo the full review anyway.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@getstream-metis
getstream-metis Bot dismissed their stale review November 27, 2026 16:01

Withdrawing approval until 8fd14df is reviewed.

@getstream-metis getstream-metis Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ Approved 8fd14df0373b after 4 rounds · 2 fixed · 1 dropped after discussion

@Loschcode

Copy link
Copy Markdown
Contributor Author

/jeffery review

@getstream-metis

Copy link
Copy Markdown

/jeffery review

Nothing changed since round 4 on 8fd14df0373b; comment /jeffery rereview force to redo the full review anyway.

@Loschcode
Loschcode merged commit b237e4d into main Nov 27, 2026
21 of 22 checks passed
@Loschcode
Loschcode deleted the laurentschaffner/ai-3226-delete-a-branch-snapshot-when-its branch November 27, 2026 16:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant