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(desktop): 群聊发件箱按请求归属回收暂存附件,在途请求等 main 答复

Dashb208c02ada55
diff --git a/apps/desktop/src/renderer/features/bots/__tests__/botGroupOutbox.test.ts b/apps/desktop/src/renderer/features/bots/__tests__/botGroupOutbox.test.ts
index 1a5717fef0..9157583968 100644
--- a/apps/desktop/src/renderer/features/bots/__tests__/botGroupOutbox.test.ts
+++ b/apps/desktop/src/renderer/features/bots/__tests__/botGroupOutbox.test.ts
@@ -588,13 +588,14 @@ describe('botGroupOutbox', () => {
mocks.owner = 2;
expect(getBotGroupOutbox('g1')).toEqual([]);
- expect(released()).toEqual(['failed']);
+ // Not yet submitted: the outbox owns them and releases them now. The one in flight is main's.
+ expect(released().sort()).toEqual(['failed', 'queued']);
// The in-flight send went out after all: its file stays with the message.
inFlight.resolve({ ok: true, messageId: 'm-late' });
await flush();
- // The queued one never reached main; nothing is sent under the new account.
+ // Nothing is sent under the new account, and nothing more is released.
expect(mocks.send).toHaveBeenCalledTimes(3);
- expect(released()).toEqual(['failed', 'queued']);
+ expect(released().sort()).toEqual(['failed', 'queued']);
});
it('on an account switch: releases an in-flight send that did not go out, and one still burning in', async () => {
@@ -616,27 +617,75 @@ describe('botGroupOutbox', () => {
expect(mocks.send).toHaveBeenCalledTimes(1);
});
- it('discards a group that left the list once a read confirms it is gone, including late answers', async () => {
- const late = deferred();
- mocks.send.mockResolvedValueOnce({ ok: false, errorCode: 'INTERNAL', message: '' }).mockReturnValueOnce(late.promise);
+ const goneGroup = () => {
+ mocks.getBotGroup.mockImplementation(async (groupId: string) =>
+ groupId === 'gone' ? { ok: false, errorCode: 'NOT_FOUND', message: '' } : { ok: true, group: {} });
+ listGroups(['unrelated']);
+ };
+
+ it('discards a group that left the list once a read confirms it is gone; unsubmitted files go at once', async () => {
+ mocks.send.mockResolvedValueOnce({ ok: false, errorCode: 'INTERNAL', message: '' });
enqueueBotGroupSend(draft({ groupId: 'gone', clientId: 'failed' }, { files: [staged('failed')] }));
- enqueueBotGroupSend(draft({ groupId: 'gone', clientId: 'flying' }, { files: [staged('flying')] }));
enqueueBotGroupSend(draft({ groupId: 'kept', clientId: 'other' }, { files: [staged('other')] }));
await flush();
- mocks.getBotGroup.mockImplementation(async (groupId: string) =>
- groupId === 'gone' ? { ok: false, errorCode: 'NOT_FOUND', message: '' } : { ok: true, group: {} });
+ mocks.send.mockReturnValue(new Promise(() => undefined));
// `kept` is missing from the list too (e.g. a legacy id), but still reads fine: untouched.
- listGroups(['unrelated']);
+ goneGroup();
await flush();
expect(mocks.getBotGroup.mock.calls.map(([groupId]) => groupId).sort()).toEqual(['gone', 'kept']);
expect(getBotGroupOutbox('gone')).toEqual([]);
expect(getBotGroupOutbox('kept')).toHaveLength(1);
- expect(new Set(released().flatMap((ids) => ids.split(',')))).toEqual(new Set(['failed', 'flying']));
- // Its late receipt changes nothing and releases nothing more.
- late.resolve({ ok: true, messageId: 'm1' });
+ expect(released()).toEqual(['failed']);
+ });
+
+ it.each([
+ ['went out', { ok: true, messageId: 'm1' }, []],
+ ['was refused', { ok: false, errorCode: 'INTERNAL', message: '' }, ['flying']],
+ ] as const)('A) a send already with main when its group is discarded: files stay until main answers, then kept if it %s', async (_label, answer, expected) => {
+ const late = deferred();
+ mocks.send.mockReturnValueOnce(late.promise);
+ enqueueBotGroupSend(draft({ groupId: 'gone', clientId: 'flying' }, { files: [staged('flying')] }));
+ await flush();
+ expect(mocks.send).toHaveBeenCalledTimes(1);
+ goneGroup();
await flush();
expect(getBotGroupOutbox('gone')).toEqual([]);
- expect(new Set(released().flatMap((ids) => ids.split(',')))).toEqual(new Set(['failed', 'flying']));
+ // Main may still be reading the file and deciding: nothing is cleaned up before it answers.
+ expect(mocks.cleanupStaged).not.toHaveBeenCalled();
+ late.resolve(answer);
+ await flush();
+ expect(released()).toEqual(expected);
+ expect(getBotGroupOutbox('gone')).toEqual([]);
+ });
+
+ it('A) a lost answer (the call itself rejects) after its group is discarded releases the files once', async () => {
+ const late = deferred();
+ mocks.send.mockReturnValueOnce(late.promise);
+ enqueueBotGroupSend(draft({ groupId: 'gone', clientId: 'flying' }, { files: [staged('flying')] }));
+ await flush();
+ goneGroup();
+ await flush();
+ expect(mocks.cleanupStaged).not.toHaveBeenCalled();
+ late.reject(new Error('ipc closed'));
+ await flush();
+ expect(released()).toEqual(['flying']);
+ });
+
+ it('B) a message still burning in when its group is discarded is never sent, and its files go once', async () => {
+ let finishBurn!: (files: AttachedFile[]) => void;
+ const drawn = { ...staged('drawn'), category: 'image', annotationStrokes: [{ points: [{ x: 1, y: 1 }] }] } as unknown as AttachedFile;
+ mocks.materialize.mockImplementationOnce(() => new Promise((resolve) => { finishBurn = resolve; }));
+ enqueueBotGroupSend(draft({ groupId: 'gone', clientId: 'burning' }, { files: [drawn] }));
+ await flush();
+ goneGroup();
+ await flush();
+ expect(getBotGroupOutbox('gone')).toEqual([]);
+ // Submission has started (it owns the files): not released yet, not sent yet.
+ expect(mocks.cleanupStaged).not.toHaveBeenCalled();
+ finishBurn([{ ...drawn, annotated: true } as AttachedFile]);
+ await flush();
+ expect(mocks.send).not.toHaveBeenCalled();
+ expect(released()).toEqual(['drawn']);
});
it('keeps a group that is only missing from the list when the read fails or is not definite', async () => {
diff --git a/apps/desktop/src/renderer/features/bots/botGroupOutbox.ts b/apps/desktop/src/renderer/features/bots/botGroupOutbox.ts
index 460d13ef17..555f500bcb 100644
--- a/apps/desktop/src/renderer/features/bots/botGroupOutbox.ts
+++ b/apps/desktop/src/renderer/features/bots/botGroupOutbox.ts
@@ -18,9 +18,11 @@
* 而服务器副本还没下载好(没有 url / path,或旧版干脆还没出现在列表里)时,按同一 messageId
* 把本地预览接到真实消息上,直到副本可用(见 `mergeBotGroupLocalAttachments`)。
* - 离开输入框的附件不再受草稿保护:发件箱持有的图片地址(含标注原图与本地预览)登记进存储
- * 清理的保护清单,条目移除时同步撤下。尚未进入消息的暂存文件由发件箱回收:群被删除(含
- * 不在当前页的群:列表里消失且读取确认已不存在)时回收该群所有未发出的条目;切换账号时回收
- * 排队中与已失败的条目,在途的那条等它的请求有了结果再定——发出了就不碰,没发出才回收。
+ * 清理的保护清单,条目移除时同步撤下。尚未进入消息的暂存文件按归属回收:还没开始提交的条目
+ * (排队中、已失败)归发件箱,群被删除(含不在当前页的群:列表里消失且读取确认已不存在)或
+ * 切换账号时立即回收;已开始提交的条目归它的那次请求,条目在烧录期间被丢弃就不再发出并回收,
+ * 已交给 main 的等结果——成功(已进入消息)不碰,失败才回收。main 的发送在 IPC 返回前读完
+ * 全部本地文件,失败时不留任何引用暂存路径的本地记录(服务端只引用已上传的副本)。
* - 失败时交还输入框(输入框空着才交还,见 `registerBotGroupComposer`),否则留在时间线上
* 显示没发出去,可重试或编辑;失败时输入框不在(切群、分享模式),回来注册输入框时同样交还。
* - 切换账号整体清空,旧账号的迟到回执一律丢弃;群被删除时丢弃该群的条目。
@@ -145,15 +147,23 @@ function releaseStaged(files: readonly AttachedFile[]): void {
if (files.length > 0) cleanupStagedChatAttachmentFiles(files);
}
+/** Entries whose `submit` has started: that request owns their files until it settles. */
+const submitting = new Set<string>();
+const entryKey = (groupId: string, clientId: string) => `${groupId}\n${clientId}`;
+
+/** Drop entries the outbox still owns; a request in progress settles its own (see `submit`). */
+function releaseUnsubmitted(groupId: string, entries: readonly BotGroupOutboxEntry[]): void {
+ for (const entry of entries) {
+ if (entry.status !== 'sent' && !submitting.has(entryKey(groupId, entry.input.clientId))) releaseStaged(entry.files);
+ }
+}
+
function ensureOwner(): void {
if (isDataOwnerGenerationCurrent(owner)) return;
owner = getDataOwnerGeneration();
generation += 1;
- // The previous account's failures go with it. Its queued and in-flight sends still hold their
- // files and settle them in `submit`, which knows whether they went out.
- for (const group of groups.values()) {
- for (const entry of group.entries) if (entry.status === 'failed') releaseStaged(entry.files);
- }
+ // The previous account's queued and failed sends go with it; one being submitted settles itself.
+ for (const [groupId, group] of groups) releaseUnsubmitted(groupId, group.entries);
groups.clear();
preparing.clear();
protectHeldMedia();
@@ -279,14 +289,30 @@ async function submit(
files: readonly AttachedFile[],
): Promise<void> {
const isCurrent = () => requestGeneration === generation && isDataOwnerGenerationCurrent(owner);
- // Queued under an account that is gone: it never reached main.
- if (!isCurrent()) {
- releaseStaged(files);
- return;
- }
+ // Dropped before its turn (account switch, deleted group): whoever dropped it released its files.
+ if (!isCurrent()) return;
const group = groups.get(groupId);
const entry = group?.entries.find((candidate) => candidate.input.clientId === clientId);
if (!group || !entry || entry.status !== 'sending') return;
+ // From here this request owns the files. Still the outbox's message: same account, still listed.
+ const owned = () =>
+ isCurrent() && (groups.get(groupId)?.entries.some((candidate) => candidate.input.clientId === clientId) ?? false);
+ const key = entryKey(groupId, clientId);
+ submitting.add(key);
+ try {
+ await submitOwned(group, entry, owned, files);
+ } finally {
+ submitting.delete(key);
+ }
+}
+
+async function submitOwned(
+ group: GroupOutbox,
+ entry: BotGroupOutboxEntry,
+ owned: () => boolean,
+ files: readonly AttachedFile[],
+): Promise<void> {
+ const { groupId, clientId } = entry.input;
update(groupId, clientId, { baseline: group.latestSequence });
const api = botGroupApi();
let errorKey = 'bots.groupChat.composer.sendFailed';
@@ -294,8 +320,8 @@ async function submit(
if (!api) throw new Error('HOST_NOT_READY');
// Drawn annotations are burned in first, as in a task; a failure keeps everything for a retry.
const prepared = await prepare(groupId, clientId);
- // The account changed while annotations were burning in: never send it under the new one.
- if (!isCurrent()) {
+ // Dropped while annotations were burning in: it never reaches main.
+ if (!owned()) {
releaseStaged(files);
return;
}
@@ -303,8 +329,9 @@ async function submit(
...entry.input,
...(prepared.length > 0 ? { attachments: toBotGroupAttachmentInputs(prepared) } : {}),
});
- if (!isCurrent()) {
- // A late answer for the previous account: keep what went out, release what did not.
+ if (!owned()) {
+ // A late answer for a dropped message: keep what went out, release what did not. Main has
+ // read the files by the time it answers.
if (!result.ok) releaseStaged(files);
return;
}
@@ -318,7 +345,8 @@ async function submit(
? 'bots.groupChat.composer.attachmentsFailed'
: botGroupErrorKey(result.errorCode, 'bots.groupChat.composer.sendFailed');
} catch (error) {
- if (!isCurrent()) {
+ // Burn-in failed (never sent) or the call failed without an answer from main's handler.
+ if (!owned()) {
releaseStaged(files);
return;
}
@@ -434,9 +462,7 @@ export function discardBotGroupOutbox(groupId: string): void {
const group = groups.get(groupId);
if (!group) return;
groups.delete(groupId);
- for (const entry of group.entries) {
- if (entry.status !== 'sent') releaseStaged(entry.files);
- }
+ releaseUnsubmitted(groupId, group.entries);
emit();
}
@@ -567,6 +593,7 @@ export function resetBotGroupOutboxForTests(): void {
groups.clear();
preparing.clear();
verifying.clear();
+ submitting.clear();
unwatchGroupList?.();
unwatchGroupList = null;
composers.clear();
diff --git a/docs/product-rules/bot-group-chat.md b/docs/product-rules/bot-group-chat.md
index 2e89632902..3e16e4d0f6 100644
--- a/docs/product-rules/bot-group-chat.md
+++ b/docs/product-rules/bot-group-chat.md
@@ -351,7 +351,7 @@ Chris 已明确扩展到多人各带伙伴,并选择「主人按群预先授
同一草稿的点名目标 ID、顺序或所有人标记变化,即使显示名字与正文没变,也必须使用新的 `clientId`。只有正文、点名目标、分工标记和附件均不变时,失败重试才复用原发送尝试。
-Desktop 主群输入框发送即乐观显示:点发送后消息立即接在时间线末尾,输入框与托盘马上空出,可以接着发下一条;同一个群的提交按发出顺序逐条交给 main,慢于 0.6 秒才在气泡下显示「发送中…」。回执的 `messageId` 是替换依据:时间线读到该消息即换成真实消息,回执之后开始的重读没读到也视为已进历史;推送重读先于回执到达时,只对正在提交的那一条按「基线之后、未被回执认领、正文与附件数一致的自己的消息」做显示去重,最终仍以 `messageId` 为准。附件按输入生成临时列表;带手绘标注的图片入队即烧录,完成后气泡改用烧录后的本地预览,提交与重试复用同一份烧录结果,放回输入框的仍是带笔迹的原件。真实消息到达而服务器副本尚未可用(无 url / path,或旧版尚未列出)时,在同一 `messageId` 内配对:数量一致按发送顺序,旧版逐个列出时按顺序同名(烧录后的名字)配对,继续显示本地预览直到副本可用,不退成缺失、不重复、同名文件不互换。离开输入框的图片(含标注原图与本地预览)登记进存储清理的保护清单直到条目释放;尚未进入消息的暂存文件由发件箱回收:群被删除(包括不在当前页、从列表消失且读取确认 `NOT_FOUND` 的群)时回收该群所有未发出的条目;切换账号时回收排队中与已失败的条目,在途的条目按请求结果定——发出的不动,没发出的回收,烧录中途切换账号的不再发出;已发出消息的文件一律不回收。发送失败绝不自动重发:输入框为空(无文字、附件、分工标签)时把正文、点名、标签与附件原样放回并提示原因,再发沿用原 `clientId`;输入框已有新内容时消息留在时间线上显示「没发出去」,可「重试」(同一 `clientId`)或在输入框清空后「编辑」放回;失败时输入框不在(切到别的群、分享模式),回到该群时同样放回空输入框并提示原因。发件箱原样保存并发出完整发送输入(附件在提交时才烧录标注并序列化),首发与重试不挑字段;除 `clientId` 外的全部字段与附件共同决定能否复用原 `clientId`,新增发送字段无需改动发件箱。发件箱只在 renderer 内存中保留,切群后仍按群隔离,切换账号清空、删群丢弃,不持久化。Thread 回复与手机端仍是等待回执后显示。实现见 `renderer/features/bots/botGroupOutbox.ts`,回归见 `botGroupOutbox.test.ts` 与 `BotGroupChatView.test.tsx`。
+Desktop 主群输入框发送即乐观显示:点发送后消息立即接在时间线末尾,输入框与托盘马上空出,可以接着发下一条;同一个群的提交按发出顺序逐条交给 main,慢于 0.6 秒才在气泡下显示「发送中…」。回执的 `messageId` 是替换依据:时间线读到该消息即换成真实消息,回执之后开始的重读没读到也视为已进历史;推送重读先于回执到达时,只对正在提交的那一条按「基线之后、未被回执认领、正文与附件数一致的自己的消息」做显示去重,最终仍以 `messageId` 为准。附件按输入生成临时列表;带手绘标注的图片入队即烧录,完成后气泡改用烧录后的本地预览,提交与重试复用同一份烧录结果,放回输入框的仍是带笔迹的原件。真实消息到达而服务器副本尚未可用(无 url / path,或旧版尚未列出)时,在同一 `messageId` 内配对:数量一致按发送顺序,旧版逐个列出时按顺序同名(烧录后的名字)配对,继续显示本地预览直到副本可用,不退成缺失、不重复、同名文件不互换。离开输入框的图片(含标注原图与本地预览)登记进存储清理的保护清单直到条目释放;尚未进入消息的暂存文件按归属回收:还没开始提交的条目(排队中、已失败)归发件箱,群被删除(包括不在当前页、从列表消失且读取确认 `NOT_FOUND` 的群)或切换账号时立即回收;已开始提交的条目归它的那次请求——烧录期间被丢弃的不再发出并回收,已交给 main 的等 main 答复,成功(已进入消息)不回收,失败或调用异常才回收(main 在答复前读完本地文件,失败时不留引用暂存路径的本地记录)。发送失败绝不自动重发:输入框为空(无文字、附件、分工标签)时把正文、点名、标签与附件原样放回并提示原因,再发沿用原 `clientId`;输入框已有新内容时消息留在时间线上显示「没发出去」,可「重试」(同一 `clientId`)或在输入框清空后「编辑」放回;失败时输入框不在(切到别的群、分享模式),回到该群时同样放回空输入框并提示原因。发件箱原样保存并发出完整发送输入(附件在提交时才烧录标注并序列化),首发与重试不挑字段;除 `clientId` 外的全部字段与附件共同决定能否复用原 `clientId`,新增发送字段无需改动发件箱。发件箱只在 renderer 内存中保留,切群后仍按群隔离,切换账号清空、删群丢弃,不持久化。Thread 回复与手机端仍是等待回执后显示。实现见 `renderer/features/bots/botGroupOutbox.ts`,回归见 `botGroupOutbox.test.ts` 与 `BotGroupChatView.test.tsx`。
完成全部显式目标校验后,最终点名列表继续沿用排除当前用户自己的既有规则;主群、Thread 与手机直连均一致,不因显式列表或所有人标记改变。