feat(frame): additive threading — ThreadID, ReplyTo + REACT type

Chat bots need replies, threads and reactions. Add two optional, omitempty
envelope fields (ThreadID, ReplyTo) plus a REACT frame type. The fields ride the
cleartext envelope (message-id references, not secret content) and are omitted
when unset, so non-threaded frames are byte-for-byte identical on the wire and
their signatures unchanged — a non-breaking, additive change.

Client gains PublishReply (threaded reply) and React (emoji reaction). The
reaction content travels in the payload, so it is sealed like any message and
stays confidential in E2E rooms; receivers dispatch on Frame.Type == REACT and
read Frame.ReplyTo for the target. Publish is refactored to share one
publishFrame path with the new helpers; its behavior is unchanged.

Tests: frame round-trip of a threaded REACT frame (golden), non-threaded
wire/sig back-compat asserting thr/re keys are absent (edge), Unmarshal of
garbage errors (error path), and an end-to-end reply+reaction round-trip in an
encrypted ModeMatrix room.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-06 18:10:44 +02:00
parent b2e6712dd2
commit 22092834bd
4 changed files with 227 additions and 18 deletions
+100
View File
@@ -302,6 +302,106 @@ func TestMediaBlobRoundTrip(t *testing.T) {
}
}
// TestThreadedReplyAndReaction exercises the additive threading API end to end
// in an encrypted, persisted, signed room (ModeMatrix): A publishes a root
// message, replies to it within a thread, and reacts to it with an emoji. The
// loopback subscriber must observe the reply carrying ReplyTo/ThreadID and the
// reaction as a frame.REACT whose (decrypted) payload is the emoji — proving the
// reaction stays sealed like any message in an E2E room.
func TestThreadedReplyAndReaction(t *testing.T) {
h := newHarness(t)
waitHealth(t, h.ctrlURL)
a, err := client.New(h.natsURL, h.ctrlURL, mustIdentity(t))
if err != nil {
t.Fatalf("connect A: %v", err)
}
defer a.Close()
roomID, err := a.CreateRoom("room.thread", room.ModeMatrix)
if err != nil {
t.Fatalf("create room: %v", err)
}
type rec struct {
f frame.Frame
pt string
}
var mu sync.Mutex
var got []rec
sub, err := a.Subscribe(roomID, func(f frame.Frame, pt []byte) {
mu.Lock()
got = append(got, rec{f: f, pt: string(pt)})
mu.Unlock()
})
if err != nil {
t.Fatalf("subscribe: %v", err)
}
defer sub.Unsubscribe()
time.Sleep(150 * time.Millisecond)
find := func(pred func(rec) bool) (rec, bool) {
mu.Lock()
defer mu.Unlock()
for _, r := range got {
if pred(r) {
return r, true
}
}
return rec{}, false
}
waitRec := func(pred func(rec) bool) (rec, bool) {
deadline := time.Now().Add(2 * time.Second)
for time.Now().Before(deadline) {
if r, ok := find(pred); ok {
return r, true
}
time.Sleep(25 * time.Millisecond)
}
return rec{}, false
}
// 1. Root message.
if err := a.Publish(roomID, []byte("root")); err != nil {
t.Fatalf("publish root: %v", err)
}
rootRec, ok := waitRec(func(r rec) bool { return r.pt == "root" })
if !ok {
t.Fatalf("never observed the root message")
}
rootID := rootRec.f.MsgID
if rootID == "" {
t.Fatalf("root frame has empty MsgID")
}
// 2. Threaded reply to the root.
if err := a.PublishReply(roomID, []byte("child"), rootID, rootID); err != nil {
t.Fatalf("publish reply: %v", err)
}
reply, ok := waitRec(func(r rec) bool { return r.pt == "child" })
if !ok {
t.Fatalf("never observed the threaded reply")
}
if reply.f.ReplyTo != rootID || reply.f.ThreadID != rootID {
t.Fatalf("reply threading lost: ReplyTo=%q ThreadID=%q want %q", reply.f.ReplyTo, reply.f.ThreadID, rootID)
}
// 3. Reaction to the root (emoji rides the encrypted payload).
if err := a.React(roomID, rootID, "👍"); err != nil {
t.Fatalf("react: %v", err)
}
reaction, ok := waitRec(func(r rec) bool { return r.f.Type == frame.REACT })
if !ok {
t.Fatalf("never observed the reaction frame")
}
if reaction.f.ReplyTo != rootID {
t.Fatalf("reaction target lost: ReplyTo=%q want %q", reaction.f.ReplyTo, rootID)
}
if reaction.pt != "👍" {
t.Fatalf("reaction payload mismatch: got %q want 👍 (decryption in E2E room)", reaction.pt)
}
}
// ---- test helpers ---------------------------------------------------------
type collector struct {