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(update): 结果写回统一「绝不静默丢弃」——记录文件含无效条目时整份移走,内存通知不截断

Dash709ee4e906fb
diff --git a/apps/desktop/src/main/agent-app-update/__tests__/agentAppUpdateService.test.ts b/apps/desktop/src/main/agent-app-update/__tests__/agentAppUpdateService.test.ts
index 802db28547..f095a6c673 100644
--- a/apps/desktop/src/main/agent-app-update/__tests__/agentAppUpdateService.test.ts
+++ b/apps/desktop/src/main/agent-app-update/__tests__/agentAppUpdateService.test.ts
@@ -360,6 +360,24 @@ describe('Agent app update install', () => {
expect(harness.deps.notify).not.toHaveBeenCalled();
});
+ it('keeps every undelivered failure notice, however many there are', async () => {
+ const harness = setup({
+ apply: vi.fn(async () => {
+ harness.switchOwner(null);
+ return { status: 'failed' as const, reason: 'x', errorCode: 'download_failed' };
+ }),
+ });
+ for (let i = 0; i < 25; i += 1) {
+ harness.switchOwner(ownerA);
+ await harness.service.install({ sessionId: `task-${i}`, sessionInstanceId: `instance-${i}` });
+ await flush();
+ }
+ harness.switchOwner(ownerA);
+ await harness.service.deliverPendingResult();
+ // Every task got its notice (some on a later request's retry), none was dropped.
+ expect(new Set(vi.mocked(harness.deps.notify).mock.calls.map((call) => call[1])).size).toBe(25);
+ });
+
it('writes the restart record only at the last gate, and clears it if the spawn then fails', async () => {
const harness = setup({
apply: vi.fn(async ({ beforeRelaunch, beforeSpawn }) => {
diff --git a/apps/desktop/src/main/agent-app-update/__tests__/restartRecords.test.ts b/apps/desktop/src/main/agent-app-update/__tests__/restartRecords.test.ts
index e3d628a9f4..ad2854731d 100644
--- a/apps/desktop/src/main/agent-app-update/__tests__/restartRecords.test.ts
+++ b/apps/desktop/src/main/agent-app-update/__tests__/restartRecords.test.ts
@@ -66,3 +66,22 @@ describe('restart records capacity', () => {
expect(listRestartRecords(file)[0]!.requestId).toBe('r0');
});
});
+
+describe('restart records with an invalid entry', () => {
+ const withInvalid = () =>
+ fs.writeFileSync(file, JSON.stringify({ records: [record('a'), { requestId: 'broken' }] }));
+
+ it('moves the whole file aside before appending instead of dropping the entry', () => {
+ withInvalid();
+ addRestartRecord(file, record('new'));
+ expect(listRestartRecords(file).map((entry) => entry.requestId)).toEqual(['new']);
+ const aside = fs.readdirSync(dir).filter((name) => name.includes('.corrupt-'));
+ expect(JSON.parse(fs.readFileSync(path.join(dir, aside[0]!), 'utf8')).records).toHaveLength(2);
+ });
+
+ it('keeps the invalid entry when removing a delivered one', () => {
+ withInvalid();
+ removeRestartRecord(file, 'a');
+ expect(JSON.parse(fs.readFileSync(file, 'utf8')).records).toEqual([{ requestId: 'broken' }]);
+ });
+});
diff --git a/apps/desktop/src/main/agent-app-update/agentAppUpdateService.ts b/apps/desktop/src/main/agent-app-update/agentAppUpdateService.ts
index 9c462a0e4e..45009ca034 100644
--- a/apps/desktop/src/main/agent-app-update/agentAppUpdateService.ts
+++ b/apps/desktop/src/main/agent-app-update/agentAppUpdateService.ts
@@ -142,7 +142,6 @@ const UNDELIVERED_REASONS = new Set([
'duplicate_request_id',
]);
const MARKER_MAX_AGE_MS = 7 * 24 * 60 * 60 * 1000;
-const MAX_PENDING_NOTICES = 20;
interface PendingNotice {
/** Account id, not a captured scope: a later sign-in of the same account still matches. */
@@ -272,9 +271,9 @@ export function createAgentAppUpdateService(deps: AgentAppUpdateDeps) {
* while their own account is active. Nothing here blocks a new install.
*/
const pendingNotices: PendingNotice[] = [];
+ // Never truncated: a notice leaves only once written or its task is gone.
const queueNotice = (notice: PendingNotice) => {
pendingNotices.push(notice);
- if (pendingNotices.length > MAX_PENDING_NOTICES) pendingNotices.shift();
};
const flushNotices = async () => {
const owner = deps.captureOwner();
diff --git a/apps/desktop/src/main/agent-app-update/restartRecords.ts b/apps/desktop/src/main/agent-app-update/restartRecords.ts
index 0272bf40e6..4eb8262292 100644
--- a/apps/desktop/src/main/agent-app-update/restartRecords.ts
+++ b/apps/desktop/src/main/agent-app-update/restartRecords.ts
@@ -47,9 +47,13 @@ function readRecords(file: string, mode: 'lenient' | 'before-write'): AgentAppUp
try {
const parsed = JSON.parse(raw) as { records?: unknown };
if (!Array.isArray(parsed.records)) throw new Error('records missing');
- return parsed.records
- .map(parseRecord)
- .filter((record): record is AgentAppUpdateMarker => !!record);
+ const records = parsed.records.map(parseRecord);
+ // Before a write, one invalid entry makes the whole file untrusted: move it
+ // aside intact rather than rewriting it without that entry.
+ if (mode === 'before-write' && records.some((record) => !record)) {
+ throw new Error('invalid record');
+ }
+ return records.filter((record): record is AgentAppUpdateMarker => !!record);
} catch {
if (mode === 'before-write') {
// Unparseable content is moved aside rather than overwritten: nothing is
@@ -65,7 +69,7 @@ function readRecords(file: string, mode: 'lenient' | 'before-write'): AgentAppUp
}
}
-function writeRecords(file: string, records: AgentAppUpdateMarker[]): void {
+function writeRecords(file: string, records: readonly unknown[]): void {
if (records.length === 0) {
for (const target of [file, `${file}.bak`]) fs.rmSync(target, { force: true });
return;
@@ -86,10 +90,27 @@ export function addRestartRecord(file: string, record: AgentAppUpdateMarker): vo
}
export function removeRestartRecord(file: string, requestId: string): void {
- const records = readRecords(file, 'lenient');
- const remaining = records.filter((record) => record.requestId !== requestId);
- // Nothing of ours (or an unreadable file): leave it untouched.
- if (remaining.length === records.length) return;
+ // Works on the raw entries so an entry this module cannot parse is kept intact.
+ let entries: unknown[];
+ try {
+ const raw = readAtomicFileSync(file);
+ if (!raw) return;
+ const parsed = JSON.parse(raw) as { records?: unknown };
+ if (!Array.isArray(parsed.records)) return;
+ entries = parsed.records;
+ } catch (error) {
+ log.warn('agent app update marker read failed', { error: String(error) });
+ return;
+ }
+ const remaining = entries.filter(
+ (entry) =>
+ !(
+ entry &&
+ typeof entry === 'object' &&
+ (entry as { requestId?: unknown }).requestId === requestId
+ ),
+ );
+ if (remaining.length === entries.length) return;
try {
writeRecords(file, remaining);
} catch (error) {
diff --git a/docs/product-rules/agent-app-update.md b/docs/product-rules/agent-app-update.md
index 4744b7d062..0220dbb242 100644
--- a/docs/product-rules/agent-app-update.md
+++ b/docs/product-rules/agent-app-update.md
@@ -24,10 +24,10 @@ Agent 只能反复引导用户去点「检查更新」;那次收紧的起因
- **真的要重启时**:更新器启动前的最后一道检查里,在账号私有目录写一份重启标记(写不进就不重启)。
新版本启动、该账号数据库就绪后,把结果(新版本号或「仍是旧版本」)作为一条普通助手消息写回发起任务,
桌面和手机重连后都能看到;写入暂时失败保留标记下次再写(按 clientId 去重),任务已不存在或超过
- 7 天才丢弃。标记文件按请求逐条保存重启记录,新记录追加而不覆盖(不设条数上限,只在写回、任务不存在或超过 7 天时移除),每条各自写回、各自清除。
+ 7 天才丢弃。标记文件按请求逐条保存重启记录,新记录追加而不覆盖(不设条数上限,只在写回、任务不存在或超过 7 天时移除;文件里有任一条读不懂时整份移到一边再写,删除已送达记录时原样保留读不懂的条目),每条各自写回、各自清除。
设置页或空闲自动安装已经在重启时,Agent 不认领那次重启,直接说明 Cindy 已在重启更新。
- **没有重启时**(下载失败、版本变化、切换账号取消、更新器没启动等):不写磁盘,按错误码生成本地化原因
- 放进内存待写回列表,立即写回;写不进或原账号不在时保留,在该账号下次就绪或下次发起更新时重试。
+ 放进内存待写回列表(不设条数上限,只在写回或任务已不存在后移除),立即写回;写不进或原账号不在时保留,在该账号下次就绪或下次发起更新时重试。
这类通知不阻止发起新的更新;进程退出时未写回的会丢失(可接受,不为此新增持久化)。
- 自动更新开关写入既有 `auto-update-settings-store`(用户显式 override),与设置页同一开关。
`check_app_update` 在有可安装更新且开关未开时返回一次 `autoUpdateHint`,同一任务内只提示一次。