|
1 | 1 | // Applies the package, agent, workspace, and managed-file slices of a consented Claw add plan. |
| 2 | +import type { Stats } from "node:fs"; |
2 | 3 | import { lstat, mkdir, rmdir } from "node:fs/promises"; |
3 | 4 | import { dirname, resolve } from "node:path"; |
4 | 5 | import { stableStringify } from "@openclaw/normalization-core"; |
@@ -107,6 +108,16 @@ function hasUnsupportedMutationActions(plan: ClawAddPlan): boolean { |
107 | 108 | ); |
108 | 109 | } |
109 | 110 |
|
| 111 | +function planWithPackageActions( |
| 112 | + plan: ClawAddPlan, |
| 113 | + predicate: (action: ClawAddPlan["actions"][number]) => boolean, |
| 114 | +): ClawAddPlan { |
| 115 | + return { |
| 116 | + ...plan, |
| 117 | + actions: plan.actions.filter((action) => action.kind !== "package" || predicate(action)), |
| 118 | + }; |
| 119 | +} |
| 120 | + |
110 | 121 | function statusAtLeast(status: ClawInstallStatus, phase: ClawInstallStatus): boolean { |
111 | 122 | const order: Record<ClawInstallStatus, number> = { |
112 | 123 | pending: 0, |
@@ -224,38 +235,124 @@ export async function applyClawAddPlan( |
224 | 235 | throw new ClawAddMutationError("provenance_failed", (error as Error).message); |
225 | 236 | } |
226 | 237 |
|
227 | | - const installPackages = options.installPackages ?? installClawPackages; |
228 | | - let packages: PersistedClawPackageRef[] = []; |
229 | | - |
230 | 238 | const workspace = resolve(resolveUserPath(plan.agent.workspace)); |
231 | 239 | const workspacePhaseRecorded = statusAtLeast(installRecord.status, "workspace_ready"); |
232 | | - const workspaceState = workspacePhaseRecorded |
233 | | - ? await lstat(workspace).catch((error: unknown) => { |
234 | | - if ( |
235 | | - typeof error === "object" && |
236 | | - error !== null && |
237 | | - "code" in error && |
238 | | - error.code === "ENOENT" |
239 | | - ) { |
240 | | - return undefined; |
241 | | - } |
242 | | - throw error; |
243 | | - }) |
244 | | - : undefined; |
| 240 | + let workspaceState: Stats | undefined; |
| 241 | + try { |
| 242 | + assertWorkspacePathUnchanged(workspace); |
| 243 | + workspaceState = await lstat(workspace).catch((error: unknown) => { |
| 244 | + if ( |
| 245 | + typeof error === "object" && |
| 246 | + error !== null && |
| 247 | + "code" in error && |
| 248 | + error.code === "ENOENT" |
| 249 | + ) { |
| 250 | + return undefined; |
| 251 | + } |
| 252 | + throw error; |
| 253 | + }); |
| 254 | + } catch (error) { |
| 255 | + clearUnownedInstallRecord(plan.agent.finalId, ["pending", "partial"], options); |
| 256 | + if (error instanceof ClawAddMutationError) { |
| 257 | + throw error; |
| 258 | + } |
| 259 | + throw new ClawAddMutationError( |
| 260 | + "workspace_parent_failed", |
| 261 | + `Could not inspect workspace ${JSON.stringify(workspace)}: ${(error as Error).message}`, |
| 262 | + ); |
| 263 | + } |
| 264 | + |
| 265 | + if (!workspacePhaseRecorded && workspaceState) { |
| 266 | + markInstallStatus(plan.agent.finalId, "partial", ["pending", "partial"], options); |
| 267 | + return partialResult({ |
| 268 | + plan, |
| 269 | + installRecord, |
| 270 | + workspaceCreated: false, |
| 271 | + configCommitted: false, |
| 272 | + packages: [], |
| 273 | + error: { |
| 274 | + code: "workspace_collision", |
| 275 | + message: `Workspace ${JSON.stringify(workspace)} was created after planning.`, |
| 276 | + }, |
| 277 | + nowMs: options.nowMs, |
| 278 | + }); |
| 279 | + } |
245 | 280 | if (workspaceState && !workspaceState.isDirectory()) { |
246 | 281 | throw new ClawAddMutationError( |
247 | 282 | "workspace_collision", |
248 | 283 | `Workspace ${JSON.stringify(workspace)} is no longer a directory.`, |
249 | 284 | ); |
250 | 285 | } |
| 286 | + |
251 | 287 | let workspaceCreated = workspaceState?.isDirectory() ?? false; |
252 | 288 | let configCommitted = statusAtLeast(installRecord.status, "config_committed"); |
| 289 | + const installPackages = options.installPackages ?? installClawPackages; |
| 290 | + let packages: PersistedClawPackageRef[] = []; |
| 291 | + const preserveRecordedPhaseOrMarkPartial = (): ClawInstallStatus => { |
| 292 | + if (workspacePhaseRecorded) { |
| 293 | + return installRecord.status; |
| 294 | + } |
| 295 | + markInstallStatus(plan.agent.finalId, "partial", ["pending", "partial"], options); |
| 296 | + return "partial"; |
| 297 | + }; |
| 298 | + |
| 299 | + const hostRequirementPlan = planWithPackageActions( |
| 300 | + plan, |
| 301 | + (action) => action.details?.kind === "plugin", |
| 302 | + ); |
| 303 | + const hostRequirementActions = hostRequirementPlan.actions.filter( |
| 304 | + (action) => action.kind === "package", |
| 305 | + ); |
| 306 | + if (hostRequirementActions.length > 0) { |
| 307 | + try { |
| 308 | + packages = await installPackages(hostRequirementPlan, options); |
| 309 | + } catch (error) { |
| 310 | + const packageError = |
| 311 | + error instanceof ClawPackageInstallError |
| 312 | + ? error |
| 313 | + : new ClawPackageInstallError( |
| 314 | + "package_install_failed", |
| 315 | + error instanceof Error ? error.message : String(error), |
| 316 | + packages, |
| 317 | + ); |
| 318 | + const installStatus = preserveRecordedPhaseOrMarkPartial(); |
| 319 | + return partialResult({ |
| 320 | + plan, |
| 321 | + installRecord, |
| 322 | + workspaceCreated, |
| 323 | + configCommitted, |
| 324 | + packages: packageError.installedPackages, |
| 325 | + installStatus, |
| 326 | + error: { code: packageError.code, message: packageError.message }, |
| 327 | + nowMs: options.nowMs, |
| 328 | + }); |
| 329 | + } |
| 330 | + } |
253 | 331 |
|
254 | 332 | try { |
255 | 333 | assertWorkspacePathUnchanged(workspace); |
256 | 334 | await mkdir(dirname(workspace), { recursive: true }); |
257 | 335 | assertWorkspacePathUnchanged(workspace); |
258 | 336 | } catch (error) { |
| 337 | + if (packages.length > 0) { |
| 338 | + const installStatus = preserveRecordedPhaseOrMarkPartial(); |
| 339 | + return partialResult({ |
| 340 | + plan, |
| 341 | + installRecord, |
| 342 | + workspaceCreated, |
| 343 | + configCommitted, |
| 344 | + packages, |
| 345 | + installStatus, |
| 346 | + error: { |
| 347 | + code: error instanceof ClawAddMutationError ? error.code : "workspace_parent_failed", |
| 348 | + message: |
| 349 | + error instanceof ClawAddMutationError |
| 350 | + ? error.message |
| 351 | + : `Could not create parent directory for workspace ${JSON.stringify(workspace)}: ${(error as Error).message}`, |
| 352 | + }, |
| 353 | + nowMs: options.nowMs, |
| 354 | + }); |
| 355 | + } |
259 | 356 | clearUnownedInstallRecord(plan.agent.finalId, ["pending", "partial"], options); |
260 | 357 | if (error instanceof ClawAddMutationError) { |
261 | 358 | throw error; |
@@ -310,19 +407,32 @@ export async function applyClawAddPlan( |
310 | 407 | } |
311 | 408 | } |
312 | 409 |
|
| 410 | + // Seed and attest the consented package bootstrap while the workspace is still |
| 411 | + // private. Committing the agent config first makes the agent routable, so a |
| 412 | + // concurrent `sessions.create` can stock-seed BOOTSTRAP.md and strand the add at |
| 413 | + // `config_committed` with a seed conflict that no retry can clear. |
313 | 414 | try { |
314 | 415 | await (options.seedPackageBootstrap ?? seedClawPackageBootstrap)(plan, { |
315 | 416 | ...options, |
316 | 417 | ...(options.nowMs !== undefined ? { nowMs: options.nowMs } : {}), |
317 | 418 | }); |
318 | 419 | } catch (error) { |
| 420 | + const installStatus: ClawInstallStatus = configCommitted |
| 421 | + ? "config_committed" |
| 422 | + : "workspace_ready"; |
| 423 | + markInstallStatus( |
| 424 | + plan.agent.finalId, |
| 425 | + installStatus, |
| 426 | + configCommitted ? ["config_committed"] : ["workspace_ready", "config_committed"], |
| 427 | + options, |
| 428 | + ); |
319 | 429 | return partialResult({ |
320 | 430 | plan, |
321 | 431 | installRecord, |
322 | 432 | workspaceCreated, |
323 | 433 | configCommitted, |
324 | 434 | packages, |
325 | | - installStatus: "workspace_ready", |
| 435 | + installStatus, |
326 | 436 | error: { |
327 | 437 | code: error instanceof ClawBootstrapWriteError ? error.code : "bootstrap_write_failed", |
328 | 438 | message: error instanceof Error ? error.message : String(error), |
@@ -472,23 +582,33 @@ export async function applyClawAddPlan( |
472 | 582 | try { |
473 | 583 | // Skills require their workspace. Recurring work is enabled only after all |
474 | 584 | // package mutation succeeds. |
475 | | - packages = await installPackages(plan, options); |
| 585 | + const workspacePackagePlan = planWithPackageActions( |
| 586 | + plan, |
| 587 | + (action) => action.details?.kind !== "plugin", |
| 588 | + ); |
| 589 | + const workspacePackageActions = workspacePackagePlan.actions.filter( |
| 590 | + (action) => action.kind === "package", |
| 591 | + ); |
| 592 | + if (workspacePackageActions.length > 0) { |
| 593 | + const workspacePackages = await installPackages(workspacePackagePlan, options); |
| 594 | + packages = [...packages, ...workspacePackages]; |
| 595 | + } |
476 | 596 | } catch (error) { |
477 | 597 | const packageError = |
478 | 598 | error instanceof ClawPackageInstallError |
479 | 599 | ? error |
480 | 600 | : new ClawPackageInstallError( |
481 | 601 | "package_install_failed", |
482 | 602 | error instanceof Error ? error.message : String(error), |
483 | | - packages, |
| 603 | + [], |
484 | 604 | ); |
485 | 605 | return partialResult({ |
486 | 606 | plan, |
487 | 607 | installRecord, |
488 | 608 | workspaceCreated, |
489 | 609 | configCommitted, |
490 | 610 | workspaceFiles, |
491 | | - packages: packageError.installedPackages, |
| 611 | + packages: [...packages, ...packageError.installedPackages], |
492 | 612 | installStatus: "config_committed", |
493 | 613 | error: { code: packageError.code, message: packageError.message }, |
494 | 614 | nowMs: options.nowMs, |
|
0 commit comments