diff --git a/src/client/channel/session.ts b/src/client/channel/session.ts index 938d7af69..bcc5c4339 100644 --- a/src/client/channel/session.ts +++ b/src/client/channel/session.ts @@ -40,7 +40,7 @@ import { readPreferredQuality, savePreferredQuality, screenVideoSettings, suppor import { audioInputConstraints } from "../media/audio-capture"; import type { ScreenPlaybackError } from "../media/viewer-playback"; import { browserCaptureDevices } from "../media/capture-devices"; -import { RoomInteractionSession, type RoomInteraction } from "../lib/room-interactions"; +import { RoomInteractionSession, type Rejection, type RoomInteraction } from "../lib/room-interactions"; import { MAX_CHAT_CODE_POINTS, isThrow } from "../../shared/room-interactions"; import { FeedbackAudio, type FeedbackCue } from "./feedback-audio"; import { validateCustomFeedbackClip } from "./feedback-clip"; @@ -598,6 +598,7 @@ export class ChannelSession { } else if (event.type === "room_reply") { if (event.requestId) this.forwardingBudget?.capacity_reply(event.requestId); const request = event.requestId ? this.requests.get(event.requestId) : undefined; + if (!request && event.requestId && event.result.type === "error") this.settleInteraction(event.roomId, event.requestId); if (!request || request.roomId !== event.roomId || !event.requestId) return; this.retireRequest(event.requestId); if (event.result.type === "error") request.reject(new Error(event.result.code ?? "operation_failed")); @@ -988,9 +989,8 @@ export class ChannelSession { return; } if (record.type === "error" && !this.requests.has(reply.requestId)) { - if (!interaction.getSnapshot().ready) this.closeInteraction(reply.roomId); - else interaction.receive({ type: "room-interaction-rejected", requestId: reply.requestId, - reason: record.code === "rate_limited" ? "rate-limited" : record.code === "target_unavailable" ? "target-unavailable" : "busy" }); + this.settleInteraction(reply.roomId, reply.requestId, + record.code === "rate_limited" ? "rate-limited" : record.code === "target_unavailable" ? "target-unavailable" : "busy"); return; } } @@ -1444,6 +1444,15 @@ export class ChannelSession { const interactionsByRoom = new Map(this.view.interactionsByRoom); if (interactionsByRoom.delete(roomId)) this.patch({ interactionsByRoom }); } + /** A writer's rejection ends an interaction request; an undelivered reply leaves its outcome unknown. */ + private settleInteraction(roomId: string, requestId: string, rejected?: Rejection): void { + const interaction = this.view.interactionsByRoom.get(roomId); + if (!interaction) return; + // The next readings sync subscribes a replacement for a refused subscription. + if (!interaction.getSnapshot().ready) this.closeInteraction(roomId); + else if (rejected) interaction.receive({ type: "room-interaction-rejected", requestId, reason: rejected }); + else interaction.undelivered(requestId); + } private closeInteractions(): void { for (const id of this.view.interactionsByRoom.keys()) this.closeInteraction(id); } @@ -1469,8 +1478,7 @@ export class ChannelSession { const requestId = message.type === "send-room-interaction" ? message.requestId : createOpaqueId(); void this.conversation.send(command, requestId).catch(() => { if (this.readings.get(reading.roomId) !== current || current.interaction !== interaction) return; - if (command.type === "interaction_ready") this.closeInteraction(reading.roomId); - else interaction.receive({ type: "room-interaction-rejected", requestId, reason: "busy" }); + this.settleInteraction(reading.roomId, requestId, "busy"); }); return nonce === undefined ? true : { nonce }; }, Date.now, undefined, event => this.playFeedback(reading.roomId, event)); diff --git a/src/client/lib/room-interactions.ts b/src/client/lib/room-interactions.ts index 0501a8de5..1c4bc6600 100644 --- a/src/client/lib/room-interactions.ts +++ b/src/client/lib/room-interactions.ts @@ -22,7 +22,7 @@ export interface RoomInteraction { payload: InteractionPayload; contentTarget?: ContentReactionTarget; } -type Rejection = "rate-limited" | "target-unavailable" | "not-subscribed" | "busy"; +export type Rejection = "rate-limited" | "target-unavailable" | "not-subscribed" | "busy"; type InteractionCommand = | { type: "subscribe-room-interactions" } | { type: "send-room-interaction"; requestId: string; payload: InteractionPayload; contentTarget?: ContentReactionTarget; nonce?: number }; @@ -215,7 +215,7 @@ export class RoomInteractionSession { // A late result may settle it until another submission or connection replaces it. this.request = request; this.update({ pending: request, error: null }); - this.pendingTimer = setTimeout(() => this.update({ pending: null, error: "unconfirmed" }), 5000); + this.pendingTimer = setTimeout(() => this.unconfirmed(), 5000); this.startCooldown(); return true; } @@ -229,6 +229,15 @@ export class RoomInteractionSession { this.update({ pending: null, error: null }); } + /** The Server did not deliver this request's reply. It may have committed, so a retry keeps its nonce. */ + undelivered(requestId: string): void { + if (this.state.pending?.requestId === requestId) this.unconfirmed(); + } + private unconfirmed() { + clearTimeout(this.pendingTimer); + this.update({ pending: null, error: "unconfirmed" }); + } + setOverlayEnabled(enabled: boolean) { if (this.closed || this.state.overlayEnabled === enabled) return; this.update({ overlayEnabled: enabled, overlayMessages: [] }); diff --git a/tests/channel-session.test.ts b/tests/channel-session.test.ts index 978ec077f..42e28c73f 100644 --- a/tests/channel-session.test.ts +++ b/tests/channel-session.test.ts @@ -1254,6 +1254,51 @@ test("interaction readiness follows Domain read admission without waiting for a } finally { session.close(); } }); +test("an undelivered reaction reply is unconfirmed at once while a writer rejection keeps its reason", async () => { + vi.useFakeTimers(); + const { session, socket, peer } = await setup(); + try { + await vi.waitFor(() => expect(session.getSnapshot().interactionsByRoom.get("first")?.getSnapshot().ready).toBe(true)); + const interaction = session.getSnapshot().interactionsByRoom.get("first")!; + expect(interaction.send({ kind: "reaction", reaction: "heart" })).toBe(true); + const request = peer.interactions.at(-1)!; + const busy = { type: "error", code: "room_busy" }; + socket.receive({ type: "room_reply", roomId: "first", requestId: "another-readers-reaction", result: busy }); + expect(interaction.getSnapshot()).toMatchObject({ ready: true, pending: { requestId: request.requestId }, error: null }); + socket.receive({ type: "room_reply", roomId: "first", requestId: request.requestId, result: busy }); + expect(interaction.getSnapshot()).toMatchObject({ ready: true, pending: null, error: "unconfirmed" }); + expect(session.getSnapshot().interactionsByRoom.get("first")).toBe(interaction); + vi.advanceTimersByTime(800); + expect(interaction.send({ kind: "reaction", reaction: "heart" })).toBe(true); + expect(request).toMatchObject({ type: "react", nonce: 1 }); + expect(peer.interactions.at(-1)).not.toBe(request); + expect(peer.interactions.at(-1)).toEqual(request); + peer.reply(request, { type: "reaction", nonce: 1, snapshot: null, event: null }); + expect(interaction.getSnapshot()).toMatchObject({ pending: null, error: null, reactions: [] }); + vi.advanceTimersByTime(800); + expect(interaction.send({ kind: "reaction", reaction: "wave" })).toBe(true); + peer.reply(peer.interactions.at(-1)!, { type: "error", code: "target_unavailable" }); + expect(interaction.getSnapshot()).toMatchObject({ pending: null, error: "target-unavailable" }); + } finally { session.close(); } +}); + +test("a refused interaction subscription is subscribed again by the next readings sync", async () => { + const { session, socket, peer } = await setup(1000, true, "room=first", false); + try { + await vi.waitFor(() => expect(peer.interactions).toHaveLength(1)); + const refused = session.getSnapshot().interactionsByRoom.get("first")!; + socket.receive({ type: "room_reply", roomId: "first", requestId: peer.interactions[0].requestId, result: { type: "error", code: "room_busy" } }); + expect(session.getSnapshot().interactionsByRoom.has("first")).toBe(false); + socket.receive({ type: "state", state: session.getSnapshot().state! }); + await vi.waitFor(() => expect(peer.interactions).toHaveLength(2)); + expect(peer.interactions[1]).toMatchObject({ type: "interaction_ready", roomId: "first" }); + const replacement = session.getSnapshot().interactionsByRoom.get("first")!; + expect(replacement).not.toBe(refused); + peer.reply(peer.interactions[1], { type: "interaction_ready", serverTime: Date.now(), reactionIds: ["wave"], publicationReactions: [] }); + expect(replacement.getSnapshot().ready).toBe(true); + } finally { session.close(); } +}); + test("content reaction targets and delayed events stay with the current room binding", async () => { const { session, socket, peer } = await setup(); try { diff --git a/tests/room-interactions.test.ts b/tests/room-interactions.test.ts index 99cc8d83a..06cfd905f 100644 --- a/tests/room-interactions.test.ts +++ b/tests/room-interactions.test.ts @@ -71,6 +71,33 @@ test("an unconfirmed content retry keeps its nonce and a replay acknowledgement session.close(); }); +test("an undelivered reply is unconfirmed at once and its retry keeps the request and nonce", () => { + vi.useFakeTimers(); + let nonce = 0; + const send = vi.fn[0]>(command => + command.type === "send-room-interaction" ? { nonce: command.nonce ?? ++nonce } : true); + const session = new RoomInteractionSession(send, Date.now); + session.authenticated("self_peer_1234"); + session.receive({ type: "room-interactions-ready", serverTime: Date.now() }); + expect(session.send({ kind: "reaction", reaction: "heart" })).toBe(true); + const first = session.getSnapshot().pending!; + session.undelivered("another_request"); + expect(session.getSnapshot().pending).toBe(first); + session.undelivered(first.requestId); + expect(session.getSnapshot()).toMatchObject({ pending: null, error: "unconfirmed" }); + vi.advanceTimersByTime(800); + expect(session.send({ kind: "reaction", reaction: "heart" })).toBe(true); + expect(send).toHaveBeenLastCalledWith(expect.objectContaining({ requestId: first.requestId, nonce: 1 })); + session.confirm(first.requestId, 1); + expect(session.getSnapshot()).toMatchObject({ pending: null, error: null }); + vi.advanceTimersByTime(800); + expect(session.send({ kind: "reaction", reaction: "heart" })).toBe(true); + const next = session.getSnapshot().pending!; + session.close(); + session.undelivered(next.requestId); + expect(session.getSnapshot()).toMatchObject({ pending: null, error: null }); +}); + test.each([ { advertised: undefined, expected: [...REACTION_IDS] }, { advertised: ["heart", "thumbsup", "future-effect"], expected: ["thumbsup", "heart"] },