File size: 6,298 Bytes
d197cf3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import { describe, expect, it, vi } from "vitest";
import { clawPackageRemovalRequestSchema } from "./package-remove-contract.js";
import { applyClawPackageRemovalPhase } from "./package-remove-phase.js";
import { digestClawPackageRemovalPlan } from "./package-remove-plan.js";
import {
  planClawPackageRemovals,
  type ClawPackageRemovalDecision,
  type ClawReferencedCleanup,
} from "./package-remove.js";
import type { PersistedClawPackageRef } from "./provenance.js";

const mocks = vi.hoisted(() => ({ local: vi.fn() }));
vi.mock("./package-remove.js", async (original) => ({
  ...(await original<typeof import("./package-remove.js")>()),
  applyClawPackageRemovals: (...args: unknown[]) => mocks.local(...args),
}));
vi.mock("./provenance.js", async (original) => ({
  ...(await original<typeof import("./provenance.js")>()),
  readClawInstallRecord: () => undefined,
}));

function reference(relationship: "managed" | "referenced"): PersistedClawPackageRef {
  return {
    schemaVersion: "openclaw.clawPackageRef.v1",
    agentId: "worker",
    clawName: "worker",
    kind: "plugin",
    source: "clawhub",
    ref: "audit",
    version: "1.0.0",
    integrity: "sha256:audit",
    status: "complete",
    relationship,
    origin: "claw-introduced",
    independentOwner: false,
    installedAtMs: 1,
    updatedAtMs: 1,
  };
}
const application = { operationId: "runtime-removal", generation: 2, pluginIds: ["audit"] };
const uninstalled = {
  kind: "plugin" as const,
  ref: "audit",
  version: "1.0.0",
  action: "uninstalled" as const,
};
const options = {
  agentId: "worker",
  operationId: "deletion-operation",
  assertCurrent: () => undefined,
};

async function decisions(relationship: "managed" | "referenced", cleanup: ClawReferencedCleanup) {
  const ref = reference(relationship);
  return await planClawPackageRemovals({ workspace: "/synthetic-workspace" }, [ref], {
    referencedCleanup: cleanup,
    deps: {
      readPackageRefs: () => [ref],
      resolvePlugin: async () => ({
        status: "found",
        pluginId: "audit",
        installedVersion: "1.0.0",
        record: {
          source: "clawhub",
          integrity: ref.integrity,
          installedAt: "1970-01-01T00:00:00.001Z",
        },
      }),
    },
  });
}

describe("Claw package phase handoff", () => {
  it.each(["retain", "remove-if-unused"] as const)(
    "keeps referenced plugins retained under %s",
    async (mode) => {
      const cleanup = { mode };
      const planned = await decisions("referenced", cleanup);
      expect(planned[0]?.action).toBe("retain");
      const packageGateway = vi.fn();
      mocks.local.mockResolvedValue({ packages: [{ ...uninstalled, action: "retained" }] });
      await applyClawPackageRemovalPhase(planned, {
        ...options,
        referencedCleanup: cleanup,
        packageGateway,
      });
      expect(packageGateway).not.toHaveBeenCalled();
    },
  );

  it.each(["managed", "referenced"] as const)(
    "hands off the canonical %s plugin removal decision without local mutation",
    async (relationship) => {
      mocks.local.mockClear();
      const cleanup: ClawReferencedCleanup =
        relationship === "managed"
          ? { mode: "retain" }
          : { mode: "remove-selected", selected: ["plugin:audit@1.0.0"] };
      const planned = await decisions(relationship, cleanup);
      expect(planned[0]?.action).toBe("uninstall");
      const packageGateway = vi.fn(async (request) => {
        const wireRequest = JSON.stringify(request);
        const received = clawPackageRemovalRequestSchema.parse({
          ...JSON.parse(wireRequest),
          binding: {
            configPath: "/synthetic-config",
            statePath: "/synthetic-state",
            cronStorePath: "/synthetic-cron",
          },
        });
        expect(received.expectedPackagePlanDigest).toBe(
          digestClawPackageRemovalPlan(planned, received.cleanup),
        );
        return { packages: [uninstalled], application };
      });
      expect(
        await applyClawPackageRemovalPhase(planned, {
          ...options,
          referencedCleanup: cleanup,
          packageGateway,
        }),
      ).toEqual({ packages: [uninstalled], application });
      expect(packageGateway).toHaveBeenCalledOnce();
      expect(mocks.local).not.toHaveBeenCalled();
    },
  );

  it("preserves managed-before-referenced ordering for a mixed phase", async () => {
    mocks.local.mockClear();
    const cleanup = { mode: "remove-selected" as const, selected: ["plugin:audit@1.0.0"] };
    const planned = await decisions("referenced", cleanup);
    const skill: ClawPackageRemovalDecision = {
      ...planned[0]!,
      packageRef: { ...reference("managed"), kind: "skill", ref: "triage" },
    };
    const packages = [{ ...uninstalled, kind: "skill" as const, ref: "triage" }, uninstalled];
    const packageGateway = vi.fn(async () => ({ packages, application }));
    expect(
      (
        await applyClawPackageRemovalPhase([...planned, skill], {
          ...options,
          referencedCleanup: cleanup,
          packageGateway,
        })
      ).packages,
    ).toEqual(packages);
    expect(mocks.local).not.toHaveBeenCalled();
  });

  it.each(["transport", "missing outcome", "wrong owner", "missing application"])(
    "rejects %s without retry or local fallback",
    async (failure) => {
      mocks.local.mockClear();
      const cleanup = { mode: "remove-selected" as const, selected: ["plugin:audit@1.0.0"] };
      const planned = await decisions("referenced", cleanup);
      const packageGateway = vi.fn(async () => {
        if (failure === "transport") {
          throw new Error("connection lost");
        }
        return {
          packages:
            failure === "missing outcome"
              ? []
              : [{ ...uninstalled, ref: failure === "wrong owner" ? "other" : "audit" }],
          ...(failure === "missing application" ? {} : { application }),
        };
      });
      await expect(
        applyClawPackageRemovalPhase(planned, {
          ...options,
          referencedCleanup: cleanup,
          packageGateway,
        }),
      ).rejects.toThrow();
      expect(packageGateway).toHaveBeenCalledOnce();
      expect(mocks.local).not.toHaveBeenCalled();
    },
  );
});