Skip to content

Commit bbce340

Browse files
authored
fix(plugins): make marketplace installs transactional (#129266)
1 parent 9b4d4d0 commit bbce340

3 files changed

Lines changed: 64 additions & 37 deletions

File tree

‎src/cli/plugins-install-command.ts‎

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,8 @@ import { assertConfigWriteAllowedInCurrentMode } from "../config/config.js";
44
import { reportClawHubPluginInstallTelemetry } from "../infra/clawhub-packages.js";
55
import { parseClawHubPluginSpec } from "../infra/clawhub-spec.js";
66
import { formatErrorMessage } from "../infra/errors.js";
7-
import { createManagedPluginArtifactConsentHandler } from "../plugins/capability-consent.js";
87
import { CLAWHUB_INSTALL_ERROR_CODE } from "../plugins/clawhub.js";
9-
import { resolveDefaultPluginExtensionsDir } from "../plugins/install-paths.js";
10-
import { persistPluginInstall } from "../plugins/install-persistence.js";
11-
import { loadInstalledPluginIndexInstallRecords } from "../plugins/installed-plugin-index-records.js";
128
import { installManagedPluginSource } from "../plugins/management-service.js";
13-
import { installPluginFromMarketplace } from "../plugins/marketplace.js";
149
import { withPluginLifecycleLease } from "../plugins/plugin-lifecycle-lease.js";
1510
import { tracePluginLifecyclePhaseAsync } from "../plugins/plugin-lifecycle-trace.js";
1611
import { defaultRuntime } from "../runtime.js";
@@ -121,46 +116,26 @@ async function runPluginInstallCommandUnlocked(
121116
) {
122117
return runtime.exit(1);
123118
}
124-
const artifactConsent = createManagedPluginArtifactConsentHandler({
125-
config: snapshot.config,
126-
source: "marketplace",
127-
spec: `${raw}@${preflight.marketplace}`,
128-
...(installMode === "update"
129-
? { previousRecords: await loadInstalledPluginIndexInstallRecords() }
130-
: {}),
119+
const result = await installManagedPluginSource({
120+
request: {
121+
source: "marketplace",
122+
marketplace: preflight.marketplace,
123+
plugin: raw,
124+
mode: installMode,
125+
},
126+
snapshot,
131127
...capabilityConsent,
132-
});
133-
const result = await installPluginFromMarketplace({
134-
...safetyOverrides,
135-
marketplace: preflight.marketplace,
136-
mode: installMode,
137-
plugin: raw,
138-
extensionsDir: resolveDefaultPluginExtensionsDir(),
128+
safetyOverrides,
139129
logger: createPluginInstallLogger(runtime),
140-
onBeforePluginArtifactCommit: artifactConsent.onBeforePluginArtifactCommit,
130+
invalidateRuntimeCache,
131+
runtime,
141132
});
142133
if (!result.ok) {
143134
if (!isClawHubBlockedCliFailure(result)) {
144135
runtime.error(result.error);
145136
}
146137
return runtime.exit(1);
147138
}
148-
149-
const install = artifactConsent.applyAcceptedSurface(result.pluginId, {
150-
source: "marketplace",
151-
installPath: result.targetDir,
152-
version: result.version,
153-
marketplaceName: result.marketplaceName,
154-
marketplaceSource: result.marketplaceSource,
155-
marketplacePlugin: result.marketplacePlugin,
156-
});
157-
await persistPluginInstall({
158-
snapshot,
159-
pluginId: result.pluginId,
160-
install,
161-
invalidateRuntimeCache,
162-
runtime,
163-
});
164139
return;
165140
}
166141

@@ -251,6 +226,7 @@ async function runPluginInstallCommandUnlocked(
251226
return runtime.exit(1);
252227
}
253228

229+
case "marketplace":
254230
case "npm-pack":
255231
case "git": {
256232
const result = await installManagedPluginSource({

‎src/plugins/management-service.install-compensation.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ vi.mock("./install.js", async (importOriginal) => ({
3131
installPluginFromNpmPackArchive: (...args: unknown[]) => mocks.install(...args),
3232
installPluginFromPath: (...args: unknown[]) => mocks.install(...args),
3333
}));
34+
vi.mock("./marketplace.js", () => ({
35+
installPluginFromMarketplace: (...args: unknown[]) => mocks.install(...args),
36+
}));
3437
vi.mock("./install-persistence.js", async (importOriginal) => ({
3538
...(await importOriginal<typeof import("./install-persistence.js")>()),
3639
persistPluginInstall: (...args: unknown[]) => mocks.persist(...args),
@@ -46,6 +49,12 @@ const requests = [
4649
{ source: "npm-pack", archivePath: "/incoming.tgz", mode: "update" },
4750
{ source: "git", spec: "git:example/demo", mode: "update" },
4851
{ source: "clawhub", spec: "clawhub:community/demo", mode: "update" },
52+
{
53+
source: "marketplace",
54+
marketplace: "local/repo",
55+
plugin: "demo",
56+
mode: "update",
57+
},
4958
] satisfies ManagedPluginSourceInstallRequest[];
5059

5160
describe("managed plugin install transactions", () => {
@@ -73,6 +82,13 @@ describe("managed plugin install transactions", () => {
7382
params: Parameters<typeof import("./install-persistence.js").persistPluginInstall>[0],
7483
) => {
7584
expect(params.install.acceptedSurface?.tools).toEqual(["demo.write"]);
85+
if (request.source === "marketplace") {
86+
expect(params.install).toMatchObject({
87+
source: "marketplace",
88+
marketplaceSource: request.marketplace,
89+
marketplacePlugin: request.plugin,
90+
});
91+
}
7692
if (failure === "before-commit") {
7793
throw conflict;
7894
}
@@ -115,6 +131,9 @@ describe("managed plugin install transactions", () => {
115131
targetDir,
116132
version: "2.0.0",
117133
extensions: [],
134+
marketplaceName: "Local",
135+
marketplaceSource: "local/repo",
136+
marketplacePlugin: "demo",
118137
git: { url: "https://example.test/demo.git" },
119138
packageName: "community/demo",
120139
clawhub: {

‎src/plugins/management-service.ts‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ import { resolveInstalledPluginPackageOwnership } from "./installed-plugin-packa
9494
import { ManagedPluginLifecycleError } from "./management-lifecycle-error.js";
9595
import type { PluginManifestRecord } from "./manifest-registry.js";
9696
import type { PluginDiagnostic } from "./manifest-types.js";
97+
import { installPluginFromMarketplace } from "./marketplace.js";
9798
import {
9899
resolveTrustedOfficialClawHubPackageName,
99100
resolveTrustedSourceLinkedOfficialClawHubSpec,
@@ -200,6 +201,12 @@ export type ManagedPluginSourceInstallRequest =
200201
mode: "install" | "update";
201202
}
202203
| { source: "git"; spec: string; mode: "install" | "update" }
204+
| {
205+
source: "marketplace";
206+
marketplace: string;
207+
plugin: string;
208+
mode: "install" | "update";
209+
}
203210
| {
204211
source: "clawhub";
205212
spec: string;
@@ -1471,7 +1478,11 @@ async function installResolvedManagedPluginSource(
14711478
config: params.snapshot.config,
14721479
env,
14731480
source,
1474-
...("spec" in request ? { spec: request.spec } : {}),
1481+
...(request.source === "marketplace"
1482+
? { spec: `${request.plugin}@${request.marketplace}` }
1483+
: "spec" in request
1484+
? { spec: request.spec }
1485+
: {}),
14751486
...("expectedIntegrity" in request && request.expectedIntegrity
14761487
? { expectedIntegrity: request.expectedIntegrity }
14771488
: {}),
@@ -1578,6 +1589,27 @@ async function installResolvedManagedPluginSource(
15781589
);
15791590
}
15801591

1592+
if (request.source === "marketplace") {
1593+
return await complete(
1594+
installPluginFromMarketplace({
1595+
...common,
1596+
marketplace: request.marketplace,
1597+
plugin: request.plugin,
1598+
mode: request.mode,
1599+
}),
1600+
{
1601+
install: (result) => ({
1602+
source: "marketplace",
1603+
installPath: result.targetDir,
1604+
version: result.version,
1605+
marketplaceName: result.marketplaceName,
1606+
marketplaceSource: result.marketplaceSource,
1607+
marketplacePlugin: result.marketplacePlugin,
1608+
}),
1609+
},
1610+
);
1611+
}
1612+
15811613
if (request.source === "npm-pack") {
15821614
return await complete(
15831615
installPluginFromNpmPackArchive({

0 commit comments

Comments
 (0)