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(plugin-market): 取消卸载期间的默认插件安装

david7913c3f94290
diff --git a/apps/desktop/src/main/cindy-brain/__tests__/marketGhostSessionBoundary.test.ts b/apps/desktop/src/main/cindy-brain/__tests__/marketGhostSessionBoundary.test.ts
index b897b7634b..37ab2baf44 100644
--- a/apps/desktop/src/main/cindy-brain/__tests__/marketGhostSessionBoundary.test.ts
+++ b/apps/desktop/src/main/cindy-brain/__tests__/marketGhostSessionBoundary.test.ts
@@ -58,4 +58,25 @@ describe('market Ghost session boundary', () => {
expect(leaseIndex).toBeGreaterThan(inspectIndex);
expect(body).toContain('releaseMutation?.();');
});
+
+ it('runs the final market callback before both initial install and update placement', () => {
+ const installStart = source.indexOf(
+ 'async function installOrUpdateMarketGhostPackageLocked(',
+ );
+ const installEnd = source.indexOf(
+ '\n}\n\ntype GhostUninstallLedgerCompletion',
+ installStart,
+ );
+ const body = source.slice(installStart, installEnd);
+ const initialBranch = body.slice(
+ body.indexOf('if (!installed) {'),
+ body.indexOf('const runtime = getGhostRuntime();'),
+ );
+
+ expect(initialBranch.indexOf('expected.beforeCommitInLock?.();')).toBeGreaterThan(-1);
+ expect(initialBranch.indexOf('expected.beforeCommitInLock?.();')).toBeLessThan(
+ initialBranch.indexOf('return installAndDock('),
+ );
+ expect(body.match(/expected\.beforeCommitInLock\?\.\(\);/g)).toHaveLength(2);
+ });
});
diff --git a/apps/desktop/src/main/cindy-brain/index.ts b/apps/desktop/src/main/cindy-brain/index.ts
index 9947f47493..e8c8d94350 100644
--- a/apps/desktop/src/main/cindy-brain/index.ts
+++ b/apps/desktop/src/main/cindy-brain/index.ts
@@ -3729,6 +3729,7 @@ async function installOrUpdateMarketGhostPackageLocked(
// inspect 与 install 各自重读磁盘,临时 .cindy 在两读之间被替换时,
// 所有前置校验(保留前缀/审阅比对/签名/解压上限)都会作用在旧字节上。
// 本地 .cindy 装入通道已强制此对账,市场通道同一口径。
+ expected.beforeCommitInLock?.();
return installAndDock(manager, cindyFilePath, {
ghostId: expected.ghostId,
enable: true,
diff --git a/apps/desktop/src/main/plugin-market/__tests__/service.test.ts b/apps/desktop/src/main/plugin-market/__tests__/service.test.ts
index 5a9a46b3ca..c20a2a57c4 100644
--- a/apps/desktop/src/main/plugin-market/__tests__/service.test.ts
+++ b/apps/desktop/src/main/plugin-market/__tests__/service.test.ts
@@ -459,11 +459,12 @@ describe('PluginMarketService migration and defaultInstall', () => {
expect(runtime.install).toHaveBeenCalledWith(
expect.stringMatching(/\.cindy$/),
- {
+ expect.objectContaining({
ghostId: 'cindy-test',
version: '1.0.0',
reviewedManifest: manifest(),
- },
+ beforeCommitInLock: expect.any(Function),
+ }),
);
expect(snapshot.items[0]).toMatchObject({
installState: 'installed',
@@ -1035,11 +1036,12 @@ describe('PluginMarketService migration and defaultInstall', () => {
expect(runtime.install).toHaveBeenCalledWith(
expect.stringMatching(/\.cindy$/),
- {
+ expect.objectContaining({
ghostId: item.ghostId,
version: item.currentRelease.version,
reviewedManifest: manifest(),
- },
+ beforeCommitInLock: expect.any(Function),
+ }),
);
expect(snapshot.items[0]).toMatchObject({
installState: 'installed',
@@ -1706,6 +1708,41 @@ describe('PluginMarketService migration and defaultInstall', () => {
expect(h.ledger.isDefaultInstallSuppressed('user-1', item.id)).toBe(true);
});
+ it('cancels an in-flight default install when local uninstall records opt-out during download', async () => {
+ const item = summary({ defaultInstall: true });
+ const h = harness([item]);
+ h.ledger.upsertInstallation(recordForTest(item));
+ runtime.ghosts = [ghostEntry(item.ghostId)];
+ const completeLocalUninstall = h.service.prepareLocalUninstallTracking(item.ghostId);
+ expect(completeLocalUninstall).not.toBeNull();
+
+ const downloadMock = vi.mocked(
+ (await import('../download.js')).downloadVerifiedPlugin,
+ );
+ const downloadStarted = deferred();
+ const downloadGate = deferred();
+ downloadMock.mockImplementationOnce(async () => {
+ downloadStarted.resolve();
+ await downloadGate.promise;
+ });
+ runtime.install.mockImplementationOnce(async (_file, options) => {
+ options.beforeCommitInLock?.();
+ throw new Error('runtime install must be cancelled before package placement');
+ });
+
+ // 本地插件页已完成目录移除并广播空清单,账本 completion 紧随其后。
+ runtime.ghosts = [];
+ const snapshotPromise = h.service.snapshot();
+ await downloadStarted.promise;
+ await completeLocalUninstall?.();
+ downloadGate.resolve();
+
+ await expect(snapshotPromise).resolves.toMatchObject({ unavailableReason: null });
+ expect(runtime.install).toHaveBeenCalledOnce();
+ expect(h.ledger.installationForGhost(item.ghostId)?.installed).toBe(false);
+ expect(h.ledger.isDefaultInstallSuppressed('user-1', item.id)).toBe(true);
+ });
+
it('records local-mode defaultInstall opt-out under the local owner', async () => {
runtime.session = {
mode: 'local',
diff --git a/apps/desktop/src/main/plugin-market/service.ts b/apps/desktop/src/main/plugin-market/service.ts
index f334be4f0e..738956121e 100644
--- a/apps/desktop/src/main/plugin-market/service.ts
+++ b/apps/desktop/src/main/plugin-market/service.ts
@@ -86,6 +86,7 @@ type PackagePermissionReviewer = (
class SilentUpgradeBusyError extends Error {}
class SilentUpgradeStaleBaselineError extends Error {}
+class SilentDefaultInstallCancelledError extends Error {}
/**
* 来源增删改的互斥键。自定义市场安装的提交段也要拿这把锁,保证所选来源从
@@ -1617,6 +1618,27 @@ export class PluginMarketService {
// 上限;真实包若额外扩权,installDetail 会安全取消并等待用户之后
// 从详情页手动安装、在原请求窗口完成确认。
reviewedManifest: reviewedManifest.manifest,
+ // 下载期间用户可能从本地插件页完成显式卸载。最终落位前在
+ // ghostId 锁内重读卸载意图,不能让旧 snapshot 把插件装回来。
+ beforeCommitInLock: () => {
+ const commitLedgerData = ledger.read();
+ if (
+ commitLedgerData.defaultInstallOptOuts[installSubject]?.includes(summary.id)
+ ) {
+ throw new SilentDefaultInstallCancelledError(
+ 'Default Plugin was explicitly uninstalled',
+ );
+ }
+ const commitLocal = this.localInstallSnapshot(
+ ledger,
+ commitLedgerData.installations,
+ );
+ if (this.toItem(summary, commitLocal).installState !== 'not-installed') {
+ throw new SilentDefaultInstallCancelledError(
+ 'Default Plugin install state changed',
+ );
+ }
+ },
},
owner,
ledger,
@@ -1624,10 +1646,12 @@ export class PluginMarketService {
});
} catch (error) {
// 单个默认插件失败不拖垮整个市场;下次同步可重试。
- log.warn('default plugin install failed', {
- pluginId: summary.id,
- error: error instanceof Error ? error.message : String(error),
- });
+ if (!(error instanceof SilentDefaultInstallCancelledError)) {
+ log.warn('default plugin install failed', {
+ pluginId: summary.id,
+ error: error instanceof Error ? error.message : String(error),
+ });
+ }
}
}
}