Skip to content

The open alternative to Salesforce, designed for AI.

Call recorder: reschedule a re-requested bot at once after a confirmed cancel (#26818)

Vlad Bisceanu6434f5c0500e
diff --git a/packages/twenty-apps/public/call-recorder/src/logic-functions/__tests__/schedule-recall-bot-on-call-recording-update.test.ts b/packages/twenty-apps/public/call-recorder/src/logic-functions/__tests__/schedule-recall-bot-on-call-recording-update.test.ts
index 8b410f79cea..51a9b42612b 100644
--- a/packages/twenty-apps/public/call-recorder/src/logic-functions/__tests__/schedule-recall-bot-on-call-recording-update.test.ts
+++ b/packages/twenty-apps/public/call-recorder/src/logic-functions/__tests__/schedule-recall-bot-on-call-recording-update.test.ts
@@ -119,9 +119,17 @@ describe('scheduleRecallBotOnCallRecordingUpdateHandler', () => {
);
queryMock.mockReset();
mutationMock.mockReset();
- mutationMock.mockImplementation(async (mutation: any) => ({
- updateCallRecording: { id: mutation.updateCallRecording.__args.id },
- }));
+ mutationMock.mockImplementation(async (mutation: any) =>
+ mutation.updateCallRecordings !== undefined
+ ? {
+ updateCallRecordings: [
+ { id: mutation.updateCallRecordings.__args.filter.id.eq },
+ ],
+ }
+ : {
+ updateCallRecording: { id: mutation.updateCallRecording.__args.id },
+ },
+ );
fetchMock.mockReset();
fetchMock.mockImplementation(async (requestUrl: string) => {
if (requestUrl === RECALL_CREATE_BOT_URL) {
diff --git a/packages/twenty-apps/public/call-recorder/src/logic-functions/data/clear-canceled-recall-bot.util.ts b/packages/twenty-apps/public/call-recorder/src/logic-functions/data/clear-canceled-recall-bot.util.ts
new file mode 100644
index 00000000000..6de37af1da7
--- /dev/null
+++ b/packages/twenty-apps/public/call-recorder/src/logic-functions/data/clear-canceled-recall-bot.util.ts
@@ -0,0 +1,32 @@
+import { type CoreApiClient } from 'twenty-client-sdk/core';
+
+import { NON_TERMINAL_CALL_RECORDING_STATUSES } from 'src/logic-functions/constants/non-terminal-call-recording-statuses';
+
+export const clearCanceledRecallBot = async (
+ client: CoreApiClient,
+ {
+ callRecordingId,
+ externalBotId,
+ }: {
+ callRecordingId: string;
+ externalBotId: string;
+ },
+): Promise<void> => {
+ await client.mutation({
+ updateCallRecordings: {
+ __args: {
+ filter: {
+ id: { eq: callRecordingId },
+ status: { in: NON_TERMINAL_CALL_RECORDING_STATUSES },
+ externalBotId: { eq: externalBotId },
+ },
+ data: {
+ externalBotId: null,
+ botScheduleAttemptedAt: null,
+ botScheduleIdempotencyKey: null,
+ },
+ },
+ id: true,
+ },
+ });
+};
diff --git a/packages/twenty-apps/public/call-recorder/src/logic-functions/data/record-bot-schedule-attempt.util.ts b/packages/twenty-apps/public/call-recorder/src/logic-functions/data/record-bot-schedule-attempt.util.ts
new file mode 100644
index 00000000000..e86a7f650c2
--- /dev/null
+++ b/packages/twenty-apps/public/call-recorder/src/logic-functions/data/record-bot-schedule-attempt.util.ts
@@ -0,0 +1,45 @@
+import { isUndefined } from '@sniptt/guards';
+import { type CoreApiClient } from 'twenty-client-sdk/core';
+
+import { CallRecordingRequestStatus } from 'src/logic-functions/constants/call-recording-request-status';
+import { CallRecordingStatus } from 'src/logic-functions/constants/call-recording-status';
+
+export const recordBotScheduleAttempt = async (
+ client: CoreApiClient,
+ {
+ id,
+ expectedAttemptedAt,
+ attemptedAt,
+ idempotencyKey,
+ }: {
+ id: string;
+ expectedAttemptedAt: string | undefined;
+ attemptedAt: string;
+ idempotencyKey: string;
+ },
+): Promise<boolean> => {
+ const result = await client.mutation({
+ updateCallRecordings: {
+ __args: {
+ filter: {
+ id: { eq: id },
+ recordingRequestStatus: {
+ eq: CallRecordingRequestStatus.REQUESTED,
+ },
+ status: { in: [CallRecordingStatus.SCHEDULED] },
+ externalBotId: { is: 'NULL' },
+ botScheduleAttemptedAt: isUndefined(expectedAttemptedAt)
+ ? { is: 'NULL' }
+ : { eq: expectedAttemptedAt },
+ },
+ data: {
+ botScheduleAttemptedAt: attemptedAt,
+ botScheduleIdempotencyKey: idempotencyKey,
+ },
+ },
+ id: true,
+ },
+ });
+
+ return (result.updateCallRecordings ?? []).length > 0;
+};
diff --git a/packages/twenty-apps/public/call-recorder/src/logic-functions/domain/has-unchanged-bot-schedule-idempotency-key.util.ts b/packages/twenty-apps/public/call-recorder/src/logic-functions/domain/has-unchanged-bot-schedule-idempotency-key.util.ts
index 0314cb973ab..c9c82935a89 100644
--- a/packages/twenty-apps/public/call-recorder/src/logic-functions/domain/has-unchanged-bot-schedule-idempotency-key.util.ts
+++ b/packages/twenty-apps/public/call-recorder/src/logic-functions/domain/has-unchanged-bot-schedule-idempotency-key.util.ts
@@ -20,11 +20,13 @@ export const hasUnchangedBotScheduleIdempotencyKey = ({
workspaceId: string;
}): boolean => {
const storedIdempotencyKey = callRecording.botScheduleIdempotencyKey;
+ const attemptedAt = callRecording.botScheduleAttemptedAt;
const meetingUrl = calendarEvent.conferenceLinkUrl;
const meetingStartsAt = calendarEvent.startsAt;
if (
isUndefined(storedIdempotencyKey) ||
+ isUndefined(attemptedAt) ||
isUndefined(meetingUrl) ||
isUndefined(meetingStartsAt)
) {
@@ -40,6 +42,7 @@ export const hasUnchangedBotScheduleIdempotencyKey = ({
callRecordingId: callRecording.id,
workspaceId,
}),
+ attemptedAt,
})
);
};
diff --git a/packages/twenty-apps/public/call-recorder/src/logic-functions/flows/__tests__/reconcile-call-recorder.test.ts b/packages/twenty-apps/public/call-recorder/src/logic-functions/flows/__tests__/reconcile-call-recorder.test.ts
index 26d66006181..ab21bd9656f 100644
--- a/packages/twenty-apps/public/call-recorder/src/logic-functions/flows/__tests__/reconcile-call-recorder.test.ts
+++ b/packages/twenty-apps/public/call-recorder/src/logic-functions/flows/__tests__/reconcile-call-recorder.test.ts
@@ -77,6 +77,30 @@ type CallRecordingNode = {
callRecorderFailureReason?: string | null;
};
+const matchesCallRecordingFilter = (
+ callRecording: CallRecordingNode,
+ filter: Record<string, { eq?: unknown; in?: unknown[]; is?: 'NULL' }>,
+): boolean =>
+ Object.entries(filter).every(([field, condition]) => {
+ const value = callRecording[field as keyof CallRecordingNode] ?? null;
+
+ if (condition.is === 'NULL') {
+ return value === null || value === '';
+ }
+
+ if (condition.in !== undefined) {
+ return condition.in.includes(value);
+ }
+
+ if ('eq' in condition) {
+ return value === condition.eq;
+ }
+
+ throw new Error(
+ `Unhandled filter on ${field}: ${JSON.stringify(condition)}`,
+ );
+ });
+
type FakeCoreApiClientFixture = {
calendarEvents: CalendarEventNode[];
callRecordings?: CallRecordingNode[];
@@ -152,6 +176,25 @@ class FakeCoreApiClient {
};
}
+ if (mutation.updateCallRecordings !== undefined) {
+ const { filter, data } = mutation.updateCallRecordings.__args;
+ const matchingCallRecordings = this.callRecordings.filter(
+ (callRecording) => matchesCallRecordingFilter(callRecording, filter),
+ );
+
+ matchingCallRecordings.forEach((callRecording) => {
+ Object.assign(callRecording, data);
+ this.mutations.push({
+ name: 'updateCallRecording',
+ args: { id: callRecording.id, data },
+ });
+ });
+
+ return {
+ updateCallRecordings: matchingCallRecordings.map(({ id }) => ({ id })),
+ };
+ }
+
if (mutation.updateCallRecording !== undefined) {
const { id, data } = mutation.updateCallRecording.__args;
const callRecording = this.callRecordings.find(
@@ -922,8 +965,8 @@ describe('reconcileCallRecorderForCalendarEventIds', () => {
class CancelCleanupFailureFakeCoreApiClient extends FakeCoreApiClient {
override async mutation(mutation: any): Promise<any> {
if (
- mutation.updateCallRecording !== undefined &&
- mutation.updateCallRecording.__args.data.externalBotId === null
+ mutation.updateCallRecordings !== undefined &&
+ mutation.updateCallRecordings.__args.data.externalBotId === null
) {
throw new Error('recall exploded');
}
diff --git a/packages/twenty-apps/public/call-recorder/src/logic-functions/flows/__tests__/retry-failed-recall-cancellations.test.ts b/packages/twenty-apps/public/call-recorder/src/logic-functions/flows/__tests__/retry-failed-recall-cancellations.test.ts
index 04bf0f26236..c86228eddb3 100644
--- a/packages/twenty-apps/public/call-recorder/src/logic-functions/flows/__tests__/retry-failed-recall-cancellations.test.ts
+++ b/packages/twenty-apps/public/call-recorder/src/logic-functions/flows/__tests__/retry-failed-recall-cancellations.test.ts
@@ -81,8 +81,9 @@ class FakeCoreApiClient {
const matchingCallRecordings = this.callRecordings.filter(
(callRecording) =>
callRecording.id === filter.id.eq &&
- callRecording.recordingRequestStatus ===
- filter.recordingRequestStatus.eq &&
+ (filter.recordingRequestStatus === undefined ||
+ callRecording.recordingRequestStatus ===
+ filter.recordingRequestStatus.eq) &&
filter.status.in.includes(callRecording.status) &&
(filter.externalBotId.is === 'NULL'
? callRecording.externalBotId === null
@@ -186,7 +187,14 @@ describe('retryFailedRecallCancellations', () => {
expect.objectContaining({ method: 'DELETE' }),
);
expect(client.mutations).toEqual([
- { id: 'call-recording-1', data: { externalBotId: null } },
+ {
+ id: 'call-recording-1',
+ data: {
+ externalBotId: null,
+ botScheduleAttemptedAt: null,
+ botScheduleIdempotencyKey: null,
+ },
+ },
]);
expect(result.canceledExternalBotCallRecordingIds).toEqual([
'call-recording-1',
@@ -316,7 +324,14 @@ describe('retryFailedRecallCancellations', () => {
id: 'call-recording-1',
data: { externalBotId: 'recall-bot-recovered' },
},
- { id: 'call-recording-1', data: { externalBotId: null } },
+ {
+ id: 'call-recording-1',
+ data: {
+ externalBotId: null,
+ botScheduleAttemptedAt: null,
+ botScheduleIdempotencyKey: null,
+ },
+ },
]);
expect(result.canceledExternalBotCallRecordingIds).toEqual([
'call-recording-1',
diff --git a/packages/twenty-apps/public/call-recorder/src/logic-functions/flows/__tests__/schedule-recall-bots-for-pending-call-recordings.test.ts b/packages/twenty-apps/public/call-recorder/src/logic-functions/flows/__tests__/schedule-recall-bots-for-pending-call-recordings.test.ts
index 5989c15e179..a079cf526cd 100644
--- a/packages/twenty-apps/public/call-recorder/src/logic-functions/flows/__tests__/schedule-recall-bots-for-pending-call-recordings.test.ts
+++ b/packages/twenty-apps/public/call-recorder/src/logic-functions/flows/__tests__/schedule-recall-bots-for-pending-call-recordings.test.ts
@@ -50,6 +50,30 @@ type CalendarEventNode = {
conferenceLink?: { primaryLinkUrl?: string | null } | null;
};
+const matchesCallRecordingFilter = (
+ callRecording: CallRecordingNode,
+ filter: Record<string, { eq?: unknown; in?: unknown[]; is?: 'NULL' }>,
+): boolean =>
+ Object.entries(filter).every(([field, condition]) => {
+ const value = callRecording[field as keyof CallRecordingNode] ?? null;
+
+ if (condition.is === 'NULL') {
+ return value === null || value === '';
+ }
+
+ if (condition.in !== undefined) {
+ return condition.in.includes(value);
+ }
+
+ if ('eq' in condition) {
+ return value === condition.eq;
+ }
+
+ throw new Error(
+ `Unhandled filter on ${field}: ${JSON.stringify(condition)}`,
+ );
+ });
+
class FakeCoreApiClient {
callRecordings: CallRecordingNode[];
calendarEvents: CalendarEventNode[];
@@ -99,6 +123,21 @@ class FakeCoreApiClient {
}
async mutation(mutation: any): Promise<any> {
+ if (mutation.updateCallRecordings !== undefined) {
+ const { filter, data } = mutation.updateCallRecordings.__args;
+ const matchingCallRecordings = this.callRecordings.filter(
+ (callRecording) => matchesCallRecordingFilter(callRecording, filter),
+ );
+
+ matchingCallRecordings.forEach((callRecording) =>
+ Object.assign(callRecording, data),
+ );
+
+ return {
+ updateCallRecordings: matchingCallRecordings.map(({ id }) => ({ id })),
+ };
+ }
+
if (mutation.updateCallRecording !== undefined) {
const { id, data } = mutation.updateCallRecording.__args;
const callRecording = this.callRecordings.find(
@@ -381,6 +420,7 @@ describe('scheduleRecallBotsForPendingCallRecordings', () => {
twentyWorkspaceId: WORKSPACE_ID,
twentyCallRecordingId: 'call-recording-1',
},
+ attemptedAt: '2026-01-01T11:55:00.000Z',
});
const client = new FakeCoreApiClient({
callRecordings: [
@@ -419,6 +459,7 @@ describe('scheduleRecallBotsForPendingCallRecordings', () => {
twentyWorkspaceId: WORKSPACE_ID,
twentyCallRecordingId: 'call-recording-1',
},
+ attemptedAt: '2025-12-30T12:00:00.000Z',
});
const client = new FakeCoreApiClient({
callRecordings: [
@@ -447,6 +488,7 @@ describe('scheduleRecallBotsForPendingCallRecordings', () => {
twentyWorkspaceId: WORKSPACE_ID,
twentyCallRecordingId: 'call-recording-1',
},
+ attemptedAt: '2026-01-01T11:55:00.000Z',
});
const client = new FakeCoreApiClient({
callRecordings: [
diff --git a/packages/twenty-apps/public/call-recorder/src/logic-functions/flows/cancel-call-recording-request.util.ts b/packages/twenty-apps/public/call-recorder/src/logic-functions/flows/cancel-call-recording-request.util.ts
index a3f9e1676e1..f156110ade2 100644
--- a/packages/twenty-apps/public/call-recorder/src/logic-functions/flows/cancel-call-recording-request.util.ts
+++ b/packages/twenty-apps/public/call-recorder/src/logic-functions/flows/cancel-call-recording-request.util.ts
@@ -4,6 +4,7 @@ import { type CoreApiClient } from 'twenty-client-sdk/core';
import { CallRecordingRequestStatus } from 'src/logic-functions/constants/call-recording-request-status';
import { type CallRecordingRecord } from 'src/logic-functions/types/call-recording-record.type';
import { cancelRecallBot } from 'src/logic-functions/recall-api/cancel-recall-bot.util';
+import { clearCanceledRecallBot } from 'src/logic-functions/data/clear-canceled-recall-bot.util';
import { updateCallRecording } from 'src/logic-functions/data/update-call-recording.util';
// Intent-first: the stale-state cron finishes the Recall half when this call fails.
@@ -37,10 +38,8 @@ export const cancelCallRecordingRequest = async ({
return;
}
- await updateCallRecording(client, {
- id: callRecording.id,
- data: {
- externalBotId: null,
- },
+ await clearCanceledRecallBot(client, {
+ callRecordingId: callRecording.id,
+ externalBotId: callRecording.externalBotId,
});
};
diff --git a/packages/twenty-apps/public/call-recorder/src/logic-functions/flows/cancel-recall-bot-for-canceled-call-recording.util.ts b/packages/twenty-apps/public/call-recorder/src/logic-functions/flows/cancel-recall-bot-for-canceled-call-recording.util.ts
index 4fa4922114f..fda6d3e30e3 100644
--- a/packages/twenty-apps/public/call-recorder/src/logic-functions/flows/cancel-recall-bot-for-canceled-call-recording.util.ts
+++ b/packages/twenty-apps/public/call-recorder/src/logic-functions/flows/cancel-recall-bot-for-canceled-call-recording.util.ts
@@ -3,7 +3,7 @@ import { type CoreApiClient } from 'twenty-client-sdk/core';
import { CallRecordingRequestStatus } from 'src/logic-functions/constants/call-recording-request-status';
import { findCallRecordingsByIds } from 'src/logic-functions/data/find-call-recordings-by-ids.util';
-import { replaceCanceledCallRecordingExternalBotId } from 'src/logic-functions/data/replace-canceled-call-recording-external-bot-id.util';
+import { clearCanceledRecallBot } from 'src/logic-functions/data/clear-canceled-recall-bot.util';
import { cancelOrEjectRecallBot } from 'src/logic-functions/recall-api/cancel-or-eject-recall-bot.util';
export type CancelRecallBotForCanceledCallRecordingOutcome =
@@ -38,10 +38,9 @@ export const cancelRecallBotForCanceledCallRecording = async ({
}
if (latestCallRecording.externalBotId === externalBotId) {
- await replaceCanceledCallRecordingExternalBotId(client, {
- id: callRecordingId,
- expectedExternalBotId: externalBotId,
- nextExternalBotId: null,
+ await clearCanceledRecallBot(client, {
+ callRecordingId,
+ externalBotId,
});
}
diff --git a/packages/twenty-apps/public/call-recorder/src/logic-functions/flows/schedule-recall-bot-for-call-recording.util.ts b/packages/twenty-apps/public/call-recorder/src/logic-functions/flows/schedule-recall-bot-for-call-recording.util.ts
index 1430a4e9132..dac5ac26fd7 100644
--- a/packages/twenty-apps/public/call-recorder/src/logic-functions/flows/schedule-recall-bot-for-call-recording.util.ts
+++ b/packages/twenty-apps/public/call-recorder/src/logic-functions/flows/schedule-recall-bot-for-call-recording.util.ts
@@ -7,12 +7,14 @@ import { type MeetingRecording } from 'src/logic-functions/types/meeting-recordi
import { buildRecallBotAutomaticVideoOutput } from 'src/logic-functions/domain/build-recall-bot-automatic-video-output.util';
import { buildRecallRoutingMetadata } from 'src/logic-functions/domain/build-recall-routing-metadata.util';
import { computeRecallBotJoinAt } from 'src/logic-functions/domain/compute-recall-bot-join-at.util';
+import { hasUnchangedBotScheduleIdempotencyKey } from 'src/logic-functions/domain/has-unchanged-bot-schedule-idempotency-key.util';
import { isRecallBotJoinWithinCreditCheckLead } from 'src/logic-functions/domain/is-recall-bot-join-within-credit-check-lead.util';
import { enqueuePreJoinCreditCheck } from 'src/logic-functions/data/enqueue-pre-join-credit-check.util';
import { findCallRecordingsByIds } from 'src/logic-functions/data/find-call-recordings-by-ids.util';
import { getCreditsUnavailableFailureReason } from 'src/logic-functions/data/get-credits-unavailable-failure-reason.util';
import { getCurrentWorkspaceId } from 'src/logic-functions/data/get-current-workspace-id.util';
import { markCallRecordingNotRecorded } from 'src/logic-functions/data/mark-call-recording-not-recorded.util';
+import { recordBotScheduleAttempt } from 'src/logic-functions/data/record-bot-schedule-attempt.util';
import { isCalendarBotSchedulingEnabled } from 'src/logic-functions/utils/is-calendar-bot-scheduling-enabled.util';
import {
computeRecallBotCreationIdempotencyKey,
@@ -97,32 +99,42 @@ export const scheduleRecallBotForCallRecording = async (
callRecordingId: callRecording.id,
workspaceId,
});
+ // Persisted before the POST so a crash leaves proof that a bot creation may
+ // have reached Recall; while the stored key still matches the scheduling
+ // inputs, recovery can re-send the creation idempotently instead of asking
+ // Recall whether a bot already exists. Re-sends of the recorded attempt keep
+ // its timestamp, and with it its key, so repeated unknown outcomes age out of
+ // the resend window instead of staying trusted forever.
+ const attemptedAt =
+ !isUndefined(freshCallRecording.botScheduleAttemptedAt) &&
+ hasUnchangedBotScheduleIdempotencyKey({
+ callRecording: freshCallRecording,
+ calendarEvent,
+ workspaceId,
+ })
+ ? freshCallRecording.botScheduleAttemptedAt
+ : new Date().toISOString();
const idempotencyKey = computeRecallBotCreationIdempotencyKey({
meetingUrl,
joinAt,
metadata,
+ attemptedAt,
});
- // Persisted before the POST so a crash leaves proof that a bot creation may
- // have reached Recall; while the stored key still matches the scheduling
- // inputs, recovery can re-send the creation idempotently instead of asking
- // Recall whether a bot already exists. Re-sends of the same key keep the
- // first attempt's timestamp so repeated unknown outcomes age out of the
- // resend window instead of staying trusted forever.
- const recordedAttemptTimestamp =
- freshCallRecording.botScheduleIdempotencyKey === idempotencyKey
- ? freshCallRecording.botScheduleAttemptedAt
- : undefined;
-
- await updateCallRecording(client, {
+ const isAttemptRecorded = await recordBotScheduleAttempt(client, {
id: callRecording.id,
- data: {
- botScheduleAttemptedAt:
- recordedAttemptTimestamp ?? new Date().toISOString(),
- botScheduleIdempotencyKey: idempotencyKey,
- },
+ expectedAttemptedAt: freshCallRecording.botScheduleAttemptedAt,
+ attemptedAt,
+ idempotencyKey,
});
+ if (!isAttemptRecorded) {
+ return {
+ status: 'skipped',
+ reason: 'another run is already scheduling this bot',
+ };
+ }
+
const scheduleResult = await scheduleRecallBot({
meetingUrl,
meetingStartsAt,
diff --git a/packages/twenty-apps/public/call-recorder/src/logic-functions/recall-api/schedule-recall-bot.util.ts b/packages/twenty-apps/public/call-recorder/src/logic-functions/recall-api/schedule-recall-bot.util.ts
index 308baba709c..63a2d463d7d 100644
--- a/packages/twenty-apps/public/call-recorder/src/logic-functions/recall-api/schedule-recall-bot.util.ts
+++ b/packages/twenty-apps/public/call-recorder/src/logic-functions/recall-api/schedule-recall-bot.util.ts
@@ -99,7 +99,10 @@ export const computeRecallBotCreationIdempotencyKey = ({
meetingUrl,
joinAt,
metadata,
-}: Pick<ScheduleRecallBotArgs, 'meetingUrl' | 'joinAt' | 'metadata'>): string =>
+ attemptedAt,
+}: Pick<ScheduleRecallBotArgs, 'meetingUrl' | 'joinAt' | 'metadata'> & {
+ attemptedAt?: string;
+}): string =>
createHash('sha256')
.update(
JSON.stringify({
@@ -107,6 +110,7 @@ export const computeRecallBotCreationIdempotencyKey = ({
callRecordingId: metadata.twentyCallRecordingId,
meetingUrl,
joinAt,
+ attemptedAt,
}),
)
.digest('hex');