Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(mail): preserve draft edits while choosing attachments

Dash0d5ad472b993
diff --git a/apps/desktop/src/main/cindy-brain/__tests__/cardActionDispatch.test.ts b/apps/desktop/src/main/cindy-brain/__tests__/cardActionDispatch.test.ts
index b143d7d198..6a210cbde1 100644
--- a/apps/desktop/src/main/cindy-brain/__tests__/cardActionDispatch.test.ts
+++ b/apps/desktop/src/main/cindy-brain/__tests__/cardActionDispatch.test.ts
@@ -349,6 +349,49 @@ it('deletion requires an active host draft with provider support and rejects all
expect(f.sendToGhost).toHaveBeenCalledTimes(1);
});
+// PR #6404 P1: picker/upload + wake can yield while the other draft view edits.
+// Existing tests covered terminal states after picking, but not changed fields
+// or a terminal state reached during wake. Unit: DI dispatch, no OS or sleeps.
+it('attachment delivery rebases disjoint edits after selection and wake, and rejects conflicts or terminal drafts', async () => {
+ const { renderMailCard } = await import('../mailCards');
+ for (const source of ['file', 'image', 'phone'] as const) for (const phase of ['selection', 'wake'] as const) {
+ for (const outcome of ['merge', 'conflict', 'sent', 'superseded'] as const) {
+ const ghost = fakeGhost({ card: false }); ghost.manifest.mail = true;
+ const mail = { accountId: 'a', draftId: 'd', key: '12345678-1234-1234-1234-123456789abc', from: '[email protected]',
+ fields: [...['to', 'cc', 'bcc', 'subject', 'body'].map(name => ({ name, label: name, value: name })),
+ { name: 'attachmentItems', label: 'Files', value: '[]', kind: 'mail-attachments' as const }],
+ attachments: [], state: 'draft' as const, status: 'Synced', sendLabel: 'Send', fromLabel: 'From' };
+ let html = renderMailCard(mail);
+ const values = Object.fromEntries(mail.fields.map(f => [f.name, f.value]));
+ values.subject = 'unsynced click edit';
+ const newer = { ...mail, fields: mail.fields.map(f => ({ ...f, value: f.name === 'body' ? 'other view body' :
+ f.name === 'attachmentItems' ? '[{"key":"existing-file","filename":"existing.pdf"}]' :
+ outcome === 'conflict' && f.name === 'subject' ? 'other view subject' : f.value })),
+ ...(outcome === 'sent' ? { state: 'sent' as const } : {}), ...(outcome === 'superseded' ? { supersededBy: 'new-call' } : {}) };
+ const staged = { attachment: { path: '/selected/file.pdf', filename: 'file.pdf', bytes: 12, sha256: 'a'.repeat(64) },
+ accepted: vi.fn(), discard: vi.fn(async () => {}) };
+ const select = async () => { if (phase === 'selection') html = renderMailCard(newer); return staged; };
+ const f = makeDispatcher({ getGhost: () => ghost, pickMailAttachment: select, prepareMailUpload: select,
+ isRunning: () => false, wake: async () => { if (phase === 'wake') html = renderMailCard(newer); },
+ resolveLiveInfo: () => ({ ghostId: 'cindy-mivo', sessionId: 's' }),
+ resolvePersistedCard: async () => ({ ghostId: 'cindy-mivo', sessionId: 's', html }) });
+ const result = await f.dispatcher.dispatch('call', `mail:${source === 'image' ? 'image' : 'attach'}:${mail.key}`, undefined,
+ source === 'phone' ? 'phone-page' : undefined, values, false,
+ source === 'phone' ? { url: 'provider-validated-reference', name: 'file.pdf' } : undefined);
+ if (outcome === 'merge') {
+ expect(result).toEqual({ ok: true });
+ expect(f.sendToGhost.mock.calls[0][1]).toMatchObject({ formValues: { body: 'other view body', subject: 'unsynced click edit',
+ attachmentItems: '[{"key":"existing-file","filename":"existing.pdf"}]' }, mailAttachment: staged.attachment });
+ expect(staged.accepted).toHaveBeenCalledOnce(); expect(staged.discard).not.toHaveBeenCalled();
+ } else {
+ expect(result).toEqual({ ok: false, reason: 'mail-view-expired' });
+ expect(f.sendToGhost).not.toHaveBeenCalled(); expect(staged.accepted).not.toHaveBeenCalled();
+ expect(staged.discard).toHaveBeenCalledOnce();
+ }
+ }
+ }
+});
+
it('only a live draft can open the host file picker, and a superseded view cannot deliver its selected file', async () => {
const { renderMailCard } = await import('../mailCards');
const ghost = fakeGhost({ card: false }); ghost.manifest.mail = true;
diff --git a/apps/desktop/src/main/cindy-brain/cardActionDispatch.ts b/apps/desktop/src/main/cindy-brain/cardActionDispatch.ts
index 0ea03cc622..6e80b6e925 100644
--- a/apps/desktop/src/main/cindy-brain/cardActionDispatch.ts
+++ b/apps/desktop/src/main/cindy-brain/cardActionDispatch.ts
@@ -146,6 +146,8 @@ export class GhostCardActionDispatcher {
let mailAction = false;
let mailAttachment: PreparedMailAttachment['attachment'] | null = null;
let staged: PreparedMailAttachment | undefined;
+ let deliveryFields = fields;
+ let attachmentView: ReturnType<typeof readMailCard> = null;
if (upload && (!mobilePageId || change || !actionId.startsWith('mail:attach:'))) return { ok: false, reason: 'bad-mail-upload' };
if (actionId.startsWith('mail:')) {
const stored = await this.deps.resolvePersistedCard(callId);
@@ -165,6 +167,7 @@ export class GhostCardActionDispatcher {
(mail.state === 'sent' && expected !== 'continue'))) return { ok: false, reason: 'mail-view-readonly' };
if (!countdownAction && !deleteAction && expected !== 'continue' && (!fields || Object.keys(fields).length !== mail.fields.length || mail.fields.some(f => !(f.name in fields)))) return { ok: false, reason: 'bad-mail-fields' };
if (attachmentAction) {
+ attachmentView = mail;
if (!sessionId || (mobilePageId ? (!upload || !this.deps.prepareMailUpload) : !this.deps.pickMailAttachment)) return { ok: false, reason: 'mail-attachment-picker-unavailable' };
try {
if (mobilePageId) {
@@ -175,9 +178,6 @@ export class GhostCardActionDispatcher {
catch { return { ok: false, reason: 'mail-attachment-read-failed' }; }
if (!ownerUsable()) { await staged?.discard(); return rejectStaleOwner(ghostId); }
if (!mailAttachment) return { ok: true };
- const latest = await this.deps.resolvePersistedCard(callId);
- const current = latest?.html ? readMailCard(latest.html) : null;
- if (!current || current.key !== mail.key || current.supersededBy || !['draft', 'syncing', 'error'].includes(current.state)) { await staged?.discard(); return { ok: false, reason: 'mail-view-expired' }; }
}
mailAction = ghost.manifest.mail === true;
}
@@ -206,6 +206,29 @@ export class GhostCardActionDispatcher {
await this.deps.wake(ghost);
if (!ownerUsable()) { await staged?.discard(); return rejectStaleOwner(ghostId); }
}
+ if (attachmentView && mailAttachment) {
+ // Selection/materialization and wake may outlive edits in another view.
+ // Rebase only the click's actual edits onto the latest host fields;
+ // never replay its entire old form over a newer draft.
+ const latest = await this.deps.resolvePersistedCard(callId);
+ if (!ownerUsable()) { await staged?.discard(); return rejectStaleOwner(ghostId); }
+ const current = latest?.html ? readMailCard(latest.html) : null;
+ if (!current || latest?.ghostId !== ghostId || latest.sessionId !== sessionId ||
+ current.key !== attachmentView.key || current.supersededBy || !['draft', 'syncing', 'error'].includes(current.state) ||
+ current.fields.length !== attachmentView.fields.length || current.fields.some(f => !attachmentView!.fields.some(base => base.name === f.name))) {
+ await staged?.discard(); return { ok: false, reason: 'mail-view-expired' };
+ }
+ const rebased: Record<string, string> = {};
+ for (const field of current.fields) {
+ const base = attachmentView.fields.find(f => f.name === field.name)!.value;
+ const local = fields![field.name];
+ if (local !== base && field.value !== base && local !== field.value) {
+ await staged?.discard(); return { ok: false, reason: 'mail-view-expired' };
+ }
+ rebased[field.name] = local !== base ? local : field.value;
+ }
+ deliveryFields = rebased;
+ }
// 卡片路径只发这一张一次性票;不另记面板手势,避免一次点击两份凭据。
const userActionToken = change ? null : this.deps.issueUserActionToken?.(ghostId, sessionId) ?? null;
if (!ownerUsable()) { await staged?.discard(); return rejectStaleOwner(ghostId); }
@@ -219,7 +242,7 @@ export class GhostCardActionDispatcher {
...(sessionId ? { sessionId } : {}),
...(userActionToken ? { userActionToken } : {}),
...(promptText !== undefined ? { prompt: promptText } : {}),
- ...(fields ? { formValues: fields } : {}),
+ ...(deliveryFields ? { formValues: deliveryFields } : {}),
...(mailAttachment ? { mailAttachment } : {}),
spawnCallId,
ts: this.now(),