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(email): keep the draft generation through discard, saves, REST reads, and schedule Undo (#8608)

Peter Chinman84ec0d43893f
diff --git a/apps/web/src/features/email-compose/primitives/compose-persistence.test.ts b/apps/web/src/features/email-compose/primitives/compose-persistence.test.ts
index dd38618103..7319932daa 100644
--- a/apps/web/src/features/email-compose/primitives/compose-persistence.test.ts
+++ b/apps/web/src/features/email-compose/primitives/compose-persistence.test.ts
@@ -97,6 +97,40 @@ it('discard waits for an in-flight first save and deletes its returned draft', a
expect.objectContaining({ draftId: 'saved-id' })
);
});
+it('discards a standalone draft at its server generation', async () => {
+ const composeContext = createComposeContext();
+ // Each send that Undo cancels advances the server's generation twice.
+ const root = mountEmailComposer(composeContext, undefined, {
+ draft: message('restored', {
+ is_draft: true,
+ thread_db_id: 'thread',
+ draft_generation: 4,
+ }),
+ });
+ await root.state.deleteDraftAndReset();
+ root.dispose();
+ expect(composeContext.drafts.deleteDraft).toHaveBeenCalledWith(
+ expect.objectContaining({ draftId: 'restored', draftGeneration: 4 })
+ );
+});
+
+it('discards a reply draft at its server generation', async () => {
+ const composeContext = createComposeContext();
+ const root = mountReplyComposer(composeContext, () => message('parent'), {
+ draft: message('restored', {
+ is_draft: true,
+ replying_to_id: 'parent',
+ thread_db_id: 'thread',
+ draft_generation: 4,
+ }),
+ });
+ await root.deleteDraftAndReset();
+ root.dispose();
+ expect(composeContext.drafts.deleteDraft).toHaveBeenCalledWith(
+ expect.objectContaining({ draftId: 'restored', draftGeneration: 4 })
+ );
+});
+
it('keeps the draft editable after failed deletion and saves later edits', async () => {
const composeContext = createComposeContext();
const goBack = vi.fn();
diff --git a/apps/web/src/features/email-compose/primitives/email-composer.ts b/apps/web/src/features/email-compose/primitives/email-composer.ts
index 233faa508d..8baa7e35f0 100644
--- a/apps/web/src/features/email-compose/primitives/email-composer.ts
+++ b/apps/web/src/features/email-compose/primitives/email-composer.ts
@@ -1122,6 +1122,7 @@ export function createEmailComposer(props: EmailComposerOptions) {
props.drafts,
{
draftId,
+ draftGeneration: draftGeneration(),
threadId: currentThreadId(),
inboxId: activeInboxId(),
},
diff --git a/apps/web/src/features/email-compose/primitives/email-send-schedule.ts b/apps/web/src/features/email-compose/primitives/email-send-schedule.ts
index 2585ad040f..7c2138d165 100644
--- a/apps/web/src/features/email-compose/primitives/email-send-schedule.ts
+++ b/apps/web/src/features/email-compose/primitives/email-send-schedule.ts
@@ -233,6 +233,7 @@ export function createEmailSendSchedule(options: {
const undoInitialSchedule = async (input: {
draftId: string;
+ draftGeneration: number | undefined;
threadId: string | undefined;
inboxId: string | undefined;
sendTime: Date;
@@ -240,7 +241,7 @@ export function createEmailSendSchedule(options: {
}) => {
try {
await delivery.unschedule({
- draftGeneration: options.draftGeneration?.(),
+ draftGeneration: input.draftGeneration,
draftId: input.draftId,
threadId: input.threadId,
inboxId: input.inboxId,
@@ -360,10 +361,12 @@ export function createEmailSendSchedule(options: {
return false;
}
+ // Undo runs after a reply composer has reset, so it keeps this value.
+ const draftGeneration = options.draftGeneration?.();
try {
await delivery.schedule(
{
- draftGeneration: options.draftGeneration?.(),
+ draftGeneration,
draftId,
threadId: options.threadId() ?? undefined,
sendTime: requested.toISOString(),
@@ -450,6 +453,7 @@ export function createEmailSendSchedule(options: {
fireAndForget(
undoInitialSchedule({
draftId,
+ draftGeneration,
threadId: threadId ?? undefined,
inboxId,
sendTime: requested,
diff --git a/apps/web/src/features/email-compose/primitives/reply-composer.ts b/apps/web/src/features/email-compose/primitives/reply-composer.ts
index 16ac2e08ec..aab9540ecc 100644
--- a/apps/web/src/features/email-compose/primitives/reply-composer.ts
+++ b/apps/web/src/features/email-compose/primitives/reply-composer.ts
@@ -1455,6 +1455,7 @@ export function createReplyComposer(
props.drafts,
{
draftId,
+ draftGeneration: draftGeneration(),
threadId: savedDraftThreadId(),
inboxId: activeInboxId(),
},
diff --git a/apps/web/src/features/email-compose/primitives/send-schedule.test.ts b/apps/web/src/features/email-compose/primitives/send-schedule.test.ts
index 0f33b06996..5279615d03 100644
--- a/apps/web/src/features/email-compose/primitives/send-schedule.test.ts
+++ b/apps/web/src/features/email-compose/primitives/send-schedule.test.ts
@@ -697,6 +697,38 @@ describe('send and schedule ordering', () => {
}
});
+ it('undoes a scheduled reply at the generation it was scheduled with', async () => {
+ const composeContext = createComposeContext();
+ // A send that Undo cancelled left the server's draft at a later generation.
+ const state = mountReplyComposer(composeContext, undefined, {
+ draft: message('draft', {
+ is_draft: true,
+ replying_to_id: 'parent',
+ thread_db_id: 'thread',
+ draft_generation: 3,
+ }),
+ });
+ try {
+ state.edit('Schedule a previously undone reply');
+ state.handleSendTimeChange(new Date('2026-12-01T12:00:00Z'));
+ await state.sendEmail();
+ expect(composeContext.delivery.schedule).toHaveBeenCalledWith(
+ expect.objectContaining({ draftGeneration: 3 }),
+ 'inbox'
+ );
+ const scheduledNotice = vi
+ .mocked(composeContext.notices.feedback.success)
+ .mock.calls.find(([text]) => text === 'Email scheduled');
+ scheduledNotice?.[1]?.actions?.[0].onClick();
+ await vi.advanceTimersByTimeAsync(0);
+ expect(composeContext.delivery.unschedule).toHaveBeenCalledWith(
+ expect.objectContaining({ draftId: 'draft', draftGeneration: 3 })
+ );
+ } finally {
+ state.dispose();
+ }
+ });
+
it('undoes a scheduled reply with its saved body, envelope, and attachments', async () => {
const composeContext = createComposeContext();
const state = mountReplyComposer(
diff --git a/apps/web/src/features/email-thread/queries/thread-source.ts b/apps/web/src/features/email-thread/queries/thread-source.ts
index e253a92a76..e0aeb92b8b 100644
--- a/apps/web/src/features/email-thread/queries/thread-source.ts
+++ b/apps/web/src/features/email-thread/queries/thread-source.ts
@@ -62,6 +62,7 @@ export function toEmailThread(thread: ApiThread): EmailThread {
})),
created_at: message.created_at,
db_id: message.db_id,
+ draft_generation: message.draft_generation,
from: message.from
? {
email: message.from.email,
diff --git a/apps/web/src/lib/queries/email/graphql/compose.test.ts b/apps/web/src/lib/queries/email/graphql/compose.test.ts
index e5a56e5339..03421299cd 100644
--- a/apps/web/src/lib/queries/email/graphql/compose.test.ts
+++ b/apps/web/src/lib/queries/email/graphql/compose.test.ts
@@ -62,6 +62,11 @@ describe('GraphQL compose transport', () => {
linkId: 'delegated',
});
});
+ it('saves at the draft generation a cancelled send left behind', () => {
+ expect(
+ composeDraftInput({ ...input, db_id: 'restored', draft_generation: 2 })
+ ).toMatchObject({ draftId: 'restored', draftGeneration: 2 });
+ });
it('refuses to send without a stable draft identity', () => {
expect(() => composeDraftInput(input)).toThrow(
'Save the draft before sending'
diff --git a/apps/web/src/lib/queries/email/graphql/compose.ts b/apps/web/src/lib/queries/email/graphql/compose.ts
index 762fe017bc..de91bd469b 100644
--- a/apps/web/src/lib/queries/email/graphql/compose.ts
+++ b/apps/web/src/lib/queries/email/graphql/compose.ts
@@ -21,6 +21,8 @@ export function composeDraftInput(
if (!draftId) throw new Error('Save the draft before sending');
return {
draftId,
+ // Admission and cancellation advance it; a stale value is rejected.
+ draftGeneration: draft.draft_generation ?? 0,
linkId: inboxId,
senderEmail: draft.sender_email,
threadDbId: draft.thread_db_id ?? handles?.threadId,
diff --git a/docs/agent-guide/email.md b/docs/agent-guide/email.md
index a4dc76a5a2..b8aeb74996 100644
--- a/docs/agent-guide/email.md
+++ b/docs/agent-guide/email.md
@@ -637,7 +637,8 @@ The send toast's **Undo** works after navigation closes the composer. It shows
**Cancelling send…** until the server confirms, then **Send cancelled**, and
restores the draft only after that confirmation. The open conversation reads the
thread again, so the cancelled message never stays visible as sent beside the
-restored draft. Cancellation that cannot be confirmed in time, an expired undo
+restored draft. The restored draft carries the server's advanced generation, so
+it can be edited, sent again, or discarded. Cancellation that cannot be confirmed in time, an expired undo
window, and failed cancellation each display a notice. Delivered attempts
remain complete even if the sent message is later deleted from the provider.
After Undo, delay the accepted cache record's refresh: the newer restored draft