Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(plugins): 修复提交快照与未读查询边界
liugaowei78bd0607d98f
diff --git a/apps/desktop/src/main/plugin-contributions/__tests__/host.test.ts b/apps/desktop/src/main/plugin-contributions/__tests__/host.test.tsindex 7959cf7d83..c29266aecb 100644--- a/apps/desktop/src/main/plugin-contributions/__tests__/host.test.ts+++ b/apps/desktop/src/main/plugin-contributions/__tests__/host.test.ts@@ -376,6 +376,25 @@ describe('community submission', () => {expect(harness.commit).not.toHaveBeenCalled();expect(releasePreparedContribution).toHaveBeenCalledWith('prepared-1');});+ // 上传器已区分“可能写入”和“尚未写入”;原成功用例没有覆盖这个错误协议。+ // 最小输入:一次 PUT 抛出带 disposition 的错误;unit,结果仍沿同一个 operation。+ it.each(['commit_same_upload', 'retry_same_url'] as const)('handles PUT disposition %s without another upload', async disposition => {+ harness.put.mockRejectedValueOnce(Object.assign(new Error('upload response lost'), { disposition }));+ await host.prepareContribution('hello', 'manual');+ await host.submitContribution('prepared-1');+ const result = await finished();+ expect(harness.prepare).toHaveBeenCalledTimes(1);+ expect(harness.put).toHaveBeenCalledTimes(1);+ if (disposition === 'commit_same_upload') {+ expect(result.operation?.status).toBe('succeeded');+ expect(result.errorCode).toBeNull();+ expect(harness.commit).toHaveBeenCalledTimes(1);+ } else {+ expect(result.errorCode).toBe('INTERNAL');+ expect(harness.commit).not.toHaveBeenCalled();+ }+ expect(pendingRows()).toEqual([]);+ });it('resolves a lost commit through the normal submit action without re-uploading or saving terminal history', async () => {harness.commit.mockRejectedValueOnce(new Error('commit response lost'));await host.prepareContribution('hello', 'manual');@@ -508,16 +527,29 @@ describe('submission preview ownership', () => {await expect(host.submitContribution('prepared-1')).rejects.toThrow('CONTRIBUTION_PACKAGE_CHANGED');expect(harness.prepare).not.toHaveBeenCalled();expect(harness.put).not.toHaveBeenCalled();+ expect(releasePreparedContribution).toHaveBeenCalledWith('prepared-1');});- it('keeps admitted transfer bytes when a detail cleanup discards its preview', async () => {- const gate = deferred<ReturnType<typeof uploadResponse>>();- harness.prepare.mockReturnValueOnce(gate.promise);+ // 原用例在 submit 返回后才关闭页面,漏掉校验 await 尚未结束时的快照释放竞态。+ // unit 最小输入:校验挂起时关闭页面;真实临时包必须保留到同一提交完成。+ it('keeps transfer bytes when a detail cleanup runs during submission validation', async () => {+ const gate = deferred<void>();+ harness.verifyContent.mockReturnValueOnce(gate.promise);+ const { directory, filePath } = harness.entry!;+ vi.mocked(releasePreparedContribution).mockImplementation(async () => {+ await fsp.rm(directory, { recursive: true, force: true });+ });await host.prepareContribution('hello', 'manual');- await host.submitContribution('prepared-1');+ const starting = host.submitContribution('prepared-1');await host.discardContribution('prepared-1');- expect(await fsp.readFile(harness.entry!.filePath)).toEqual(bytes);- gate.resolve(uploadResponse());+ const retained = fs.existsSync(filePath);+ gate.resolve();+ await starting;await vi.waitFor(() => expect(host.contributionTransferStatus('prepared-1').stage).toBe('complete'));+ const result = host.contributionTransferStatus('prepared-1');+ expect(retained).toBe(true);+ expect(result.operation?.status).toBe('succeeded');+ expect(harness.put).toHaveBeenCalledTimes(1);+ expect(harness.commit).toHaveBeenCalledTimes(1);});it('keeps a first submission without release notes eligible when the server allows it', async () => {delete harness.entry!.summary.releaseNotes;@@ -586,6 +618,25 @@ describe('review result reading state', () => {harness.list.mockRejectedValueOnce(new Error('offline'));await expect(service.reviewUpdates()).rejects.toThrow('offline');});+ // reviewUpdates 的真实分页入口;已有用例只覆盖向前翻页,漏掉重复游标占住 inFlight。+ // unit 最小输入:连续两页返回同一游标;失败后下一次正常读取仍须能得到未读审核结果。+ it.each(['list', 'history'] as const)('rejects a repeated %s cursor and permits the next review refresh', async source => {+ if (source === 'list') {+ harness.list.mockResolvedValueOnce({ plugins: [], nextCursor: 'repeat' })+ .mockResolvedValueOnce({ plugins: [], nextCursor: 'repeat' })+ .mockRejectedValue(new Error('unexpected repeated list request'));+ } else {+ harness.list.mockResolvedValue({ plugins: [plugin({ ...submission(), reviewStatus: 'pending', decidedAt: null })], nextCursor: null });+ harness.detail.mockResolvedValue({ history: { events: [], nextCursor: 'repeat' } });+ harness.history.mockResolvedValueOnce({ events: [], nextCursor: 'repeat' })+ .mockRejectedValue(new Error('unexpected repeated history request'));+ }+ const service = await import('../reviewUpdates');+ await expect(service.reviewUpdates()).rejects.toThrow('PRECONDITION_FAILED');+ expect(source === 'list' ? harness.list : harness.history).toHaveBeenCalledTimes(source === 'list' ? 2 : 1);+ harness.list.mockResolvedValue({ plugins: [plugin()], nextCursor: null });+ expect(await service.reviewUpdates()).toEqual([expect.objectContaining({ pluginId: 'plugin-1', unread: true })]);+ });});diff --git a/apps/desktop/src/main/plugin-contributions/host.ts b/apps/desktop/src/main/plugin-contributions/host.tsindex 294322e481..9c074e9a40 100644--- a/apps/desktop/src/main/plugin-contributions/host.ts+++ b/apps/desktop/src/main/plugin-contributions/host.ts@@ -221,25 +221,40 @@ export async function submitContribution(throwIpcError(blocker?.code && isIpcErrorCode(blocker.code) ? blocker.code : 'PRECONDITION_FAILED',blocker?.message ?? 'Refresh the publishing summary before submitting');}- await assertPreparedContent(entry);- await assertCaller?.();- assertPreparedInstallation(entry);const request = entry.request;if (!request)throwIpcError('PRECONDITION_FAILED', 'Prepare this submission before submitting');- const active = [...submissions.values()].find(item => item.entry.owner === entry.owner &&- item.request.target.ghostId === request.target.ghostId && item.stage !== 'complete');- if (active) {- if (active.entry.contentSha256 !== entry.contentSha256 || active.request.target.version !== entry.summary.version ||+ if (entry.pinned) {+ await assertCaller?.();+ assertPreparedInstallation(entry);+ const active = submissions.get(preparedId);+ if (active) return view(active);+ throwIpcError('PRECONDITION_FAILED', 'This submission is already starting');+ }+ // Take ownership before validation yields, so closing the preview cannot+ // discard the bytes that this submission is about to send.+ entry.pinned = true;+ let active: Submission | undefined;+ try {+ await assertPreparedContent(entry);+ await assertCaller?.();+ assertPreparedInstallation(entry);+ active = [...submissions.values()].find(item => item.entry.owner === entry.owner &&+ item.request.target.ghostId === request.target.ghostId && item.stage !== 'complete');+ if (active && (active.entry.contentSha256 !== entry.contentSha256 || active.request.target.version !== entry.summary.version ||active.entry.installationDir !== entry.installationDir || active.entry.approvalToken !== entry.approvalToken ||- active.entry.publishingSourceKey !== entry.publishingSourceKey)+ active.entry.publishingSourceKey !== entry.publishingSourceKey))throwIpcError('PRECONDITION_FAILED', 'A submission for this plugin is already running');- if (active.entry !== entry) await discardContribution(preparedId);+ } catch (error) {+ await releasePreparedContribution(preparedId);+ throw error;+ }+ if (active) {+ if (active.entry !== entry) await releasePreparedContribution(preparedId);return view(active);}const value: Submission = { entry, prepared: entry.summary, request,stage: 'prepared', operation: null, errorCode: null, controller: new AbortController() };- entry.pinned = true;submissions.set(preparedId, value);if (!hooked) {hooked = true;@@ -352,6 +367,10 @@ async function submit(value: Submission, assertCaller?: () => Promise<void>): Prsignal: controller.signal,onBytes: () => { try { assertCurrent(); } catch { controller.abort(); } },});+ } catch (error) {+ // The uploader may have sent the bytes despite losing the response.+ // Commit this same operation; never upload the package a second time.+ if (!(error instanceof Error) || !('disposition' in error) || error.disposition !== 'commit_same_upload') throw error;} finally {await handle.close();}diff --git a/apps/desktop/src/main/plugin-contributions/reviewUpdates.ts b/apps/desktop/src/main/plugin-contributions/reviewUpdates.tsindex 51cc373736..c3f444e7e2 100644--- a/apps/desktop/src/main/plugin-contributions/reviewUpdates.ts+++ b/apps/desktop/src/main/plugin-contributions/reviewUpdates.ts@@ -41,12 +41,15 @@ async function latestDecision(api: ContributionApi, plugin: MyPluginSummary): Pr// A newer pending/cancelled version must not hide an older unread decision.const detail = await api.detail(plugin.pluginId);let history = detail.history;+ const cursors = new Set<string>();for (;;) {for (const event of history.events) {const result = decision(plugin.pluginId, event);if (result) return result;}if (!history.nextCursor) return null;+ if (cursors.has(history.nextCursor)) throwIpcError('PRECONDITION_FAILED', 'Review history pagination did not advance');+ cursors.add(history.nextCursor);history = await api.history(plugin.pluginId, history.nextCursor);}}@@ -55,6 +58,7 @@ async function refresh(api: ContributionApi): Promise<PluginReviewUpdate[]> {if (cache?.owner !== owner) cache = { owner, entries: new Map() };const held = cache;const visible = new Set<string>();+ const cursors = new Set<string>();let cursor: string | undefined;do {const page = await api.list({ query: '', filter: 'all', ...(cursor ? { cursor } : {}) });@@ -69,6 +73,10 @@ async function refresh(api: ContributionApi): Promise<PluginReviewUpdate[]> {}}cursor = page.nextCursor ?? undefined;+ if (cursor) {+ if (cursors.has(cursor)) throwIpcError('PRECONDITION_FAILED', 'Plugin pagination did not advance');+ cursors.add(cursor);+ }} while (cursor);assertContributionOwner(owner);for (const pluginId of held.entries.keys()) if (!visible.has(pluginId)) held.entries.delete(pluginId);