Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(calls): open the record when the last participant leaves a channel call (#8693)

Eric Hayes3c928f07cde6
diff --git a/apps/web/AGENTS.md b/apps/web/AGENTS.md
index 40813ab7c0..23e52b5716 100644
--- a/apps/web/AGENTS.md
+++ b/apps/web/AGENTS.md
@@ -228,7 +228,8 @@ ownership. Cancel, logout, and the ten-minute timeout retire pending callbacks.
the canonical event read, excluding team projections.
- Route explicit call exits using the server's `callEnded` result: ended calls
open their record; ongoing channel and scheduled calls return to the channel
- and Calendar. Preserve the session ID before disconnecting.
+ and Calendar. Preserve the session ID and send the server leave before media
+ teardown; once the last participant drops, the server ends the call first.
- Diagnose browser call media with `call.media.*` spans and the `meeting.setup.id`
on `meeting.join`; keep provider messages, device labels/IDs, and call-link
tokens out of telemetry. See [call diagnostics](../../docs/agent-guide/calls.md#browser-call-diagnostics).
diff --git a/apps/web/src/features/channel/Call/call-lifecycle.ts b/apps/web/src/features/channel/Call/call-lifecycle.ts
index 7137b2f919..88c9612868 100644
--- a/apps/web/src/features/channel/Call/call-lifecycle.ts
+++ b/apps/web/src/features/channel/Call/call-lifecycle.ts
@@ -474,6 +474,14 @@ export function createCallLifecycle(options: {
LEAVE_TIMEOUT_MS
);
async function disconnect() {
+ // A hangup reaches the server before media teardown: the last
+ // participant's RTC departure archives the call, and a later leave
+ // could no longer report that it ended. Native-end cleanup may yield
+ // to a restored session or rejoin, so its leave waits.
+ const releasing =
+ !request.skipServerLeave && request.options?.endNativeCall !== false
+ ? leaveServer(request.channelId)
+ : undefined;
let result: LeaveResult = undefined;
try {
try {
@@ -481,7 +489,8 @@ export function createCallLifecycle(options: {
if (!active) return;
options.onLeft(request.channelId);
} finally {
- if (active && !request.skipServerLeave)
+ if (releasing) result = await releasing;
+ else if (active && !request.skipServerLeave)
result = await leaveServer(request.channelId);
}
finish(undefined, result);
diff --git a/apps/web/src/features/channel/Call/tests/call-lifecycle.test.ts b/apps/web/src/features/channel/Call/tests/call-lifecycle.test.ts
index 41fd9ed4ab..80312c006b 100644
--- a/apps/web/src/features/channel/Call/tests/call-lifecycle.test.ts
+++ b/apps/web/src/features/channel/Call/tests/call-lifecycle.test.ts
@@ -436,8 +436,9 @@ describe('shared call lifecycle', () => {
expect(vi.getTimerCount()).toBe(0);
});
- it('leaves the channel view before slow media teardown completes', async () => {
- const { lifecycle, ports, join } = setup();
+ it('leaves the channel view and the server before slow media teardown completes', async () => {
+ const leave = vi.fn(async () => ({ callEnded: true }));
+ const { lifecycle, ports, join } = setup({ leave });
await join();
const teardown = deferred<void>();
ports.disconnect.mockReturnValueOnce(teardown.promise);
@@ -445,17 +446,25 @@ describe('shared call lifecycle', () => {
lifecycle.onLeave(onLeave);
const leaving = lifecycle.leave(call.channelId);
expect(onLeave).toHaveBeenCalledExactlyOnceWith(call.channelId);
- expect(ports.leave).not.toHaveBeenCalled();
+ // The last participant's RTC departure ends the call on the server, so a
+ // leave sent after teardown could not report it.
+ expect(leave).toHaveBeenCalledExactlyOnceWith(call.channelId);
teardown.resolve();
- await leaving;
+ await expect(leaving).resolves.toEqual({ callEnded: true });
expect(onLeave).toHaveBeenCalledOnce();
});
it('handles native call end through the same leave lifecycle', async () => {
const { ports, nativeEnd, join } = setup();
await join();
+ const teardown = deferred<void>();
+ ports.disconnect.mockReturnValueOnce(teardown.promise);
nativeEnd();
await vi.advanceTimersByTimeAsync(0);
+ // A restored session may still supersede native-end cleanup.
+ expect(ports.leave).not.toHaveBeenCalled();
+ teardown.resolve();
+ await vi.advanceTimersByTimeAsync(0);
expect(ports.disconnect).toHaveBeenCalledExactlyOnceWith({
endNativeCall: false,
});
@@ -583,7 +592,8 @@ describe('shared call lifecycle', () => {
oldDisconnect.resolve();
await vi.advanceTimersByTimeAsync(0);
expect(ports.disconnect).toHaveBeenCalledTimes(2);
- expect(ports.leave).toHaveBeenCalledOnce();
+ // Each hangup reaches the server before teardown; leaving is idempotent.
+ expect(ports.leave).toHaveBeenCalledTimes(2);
expect(ports.onLeft).toHaveBeenCalledOnce();
}
);