File size: 12,680 Bytes
76289e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// Plugin state runtime tests cover runtime-backed plugin state storage.
import { afterEach, describe, expect, it, vi } from "vitest";
import { resolveStateDir } from "../config/paths.js";
import { requireNodeSqlite } from "../infra/node-sqlite.js";
import type { PluginRecord } from "../plugins/registry-types.js";
import { createPluginRegistry } from "../plugins/registry.js";
import type { PluginRuntime } from "../plugins/runtime/types.js";
import { closeOpenClawAgentDatabasesForTest } from "../state/openclaw-agent-db.js";
import { openOpenClawStateDatabase } from "../state/openclaw-state-db.js";
import { withOpenClawTestState } from "../test-utils/openclaw-test-state.js";
import { resetPluginBlobStoreForTests, type OpenBlobStoreOptions } from "./plugin-blob-store.js";
import { resetPluginStateStoreForTests } from "./plugin-state-store.js";

function createPluginRecord(
  id: string,
  origin: PluginRecord["origin"] = "bundled",
  opts: { trustedOfficialInstall?: boolean } = {},
): PluginRecord {
  return {
    id,
    name: id,
    source: `/plugins/${id}/index.ts`,
    origin,
    trustedOfficialInstall: opts.trustedOfficialInstall,
    enabled: true,
    status: "loaded",
    toolNames: [],
    hookNames: [],
    channelIds: [],
    cliBackendIds: [],
    providerIds: [],
    embeddingProviderIds: [],
    speechProviderIds: [],
    realtimeTranscriptionProviderIds: [],
    realtimeVoiceProviderIds: [],
    mediaUnderstandingProviderIds: [],
    transcriptSourceProviderIds: [],
    imageGenerationProviderIds: [],
    videoGenerationProviderIds: [],
    musicGenerationProviderIds: [],
    webFetchProviderIds: [],
    webSearchProviderIds: [],
    migrationProviderIds: [],
    agentHarnessIds: [],
    cliCommands: [],
    services: [],
    gatewayDiscoveryServiceIds: [],
    commands: [],
    httpRoutes: 0,
    hookCount: 0,
    configSchema: false,
  } as PluginRecord;
}

function createTestPluginRegistry() {
  return createPluginRegistry({
    logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() },
    runtime: {
      state: {
        resolveStateDir,
        openBlobStore: () => {
          throw new Error("registry plugin runtime proxy should bind openBlobStore");
        },
        openKeyedStore: () => {
          throw new Error("registry plugin runtime proxy should bind openKeyedStore");
        },
        openSyncKeyedStore: () => {
          throw new Error("registry plugin runtime proxy should bind openSyncKeyedStore");
        },
      },
    } as unknown as PluginRuntime,
  });
}

afterEach(() => {
  closeOpenClawAgentDatabasesForTest();
  resetPluginBlobStoreForTests();
  resetPluginStateStoreForTests();
});

describe("plugin runtime state proxy", () => {
  it("binds openKeyedStore to the bundled plugin id and keeps resolveStateDir", async () => {
    await withOpenClawTestState({ label: "plugin-state-runtime" }, async (state) => {
      const registry = createTestPluginRegistry();
      const record = createPluginRecord("discord", "bundled");
      registry.registry.plugins.push(record);
      const api = registry.createApi(record, { config: {} });

      expect(api.runtime.state.resolveStateDir()).toBe(state.stateDir);
      const native = requireNodeSqlite();
      const sql = [
        vi.spyOn(native.DatabaseSync.prototype, "prepare"),
        vi.spyOn(native.DatabaseSync.prototype, "exec"),
        ...(["get", "all", "run", "iterate"] as const).map((method) =>
          vi.spyOn(native.StatementSync.prototype, method),
        ),
      ];
      try {
        const store = api.runtime.state.openKeyedStore<{ plugin: string }>({
          namespace: "runtime",
          maxEntries: 10,
        });
        await expect(store.registerIfAbsent("k", { plugin: "discord" })).resolves.toBe(true);
        await expect(store.registerIfAbsent("k", { plugin: "duplicate" })).resolves.toBe(false);

        const telegram = createPluginRecord("telegram", "bundled");
        registry.registry.plugins.push(telegram);
        const telegramApi = registry.createApi(telegram, { config: {} });
        const telegramStore = telegramApi.runtime.state.openKeyedStore<{ plugin: string }>({
          namespace: "runtime",
          maxEntries: 10,
        });
        await expect(telegramStore.lookup("k")).resolves.toBeUndefined();
        await expect(telegramStore.count?.()).resolves.toBe(0);
        await expect(store.count?.()).resolves.toBe(1);
        await expect(telegramStore.lookupMany?.(["k"])).resolves.toEqual([
          { ok: true, value: undefined },
        ]);
        await expect(store.lookupMany?.(["k", "missing", "k"])).resolves.toEqual([
          { ok: true, value: { plugin: "discord" } },
          { ok: true, value: undefined },
          { ok: true, value: { plugin: "discord" } },
        ]);
        await expect(store.lookup("k")).resolves.toEqual({ plugin: "discord" });

        await store.register("temporary", { plugin: "discord" });
        await expect(store.consume("temporary")).resolves.toEqual({ plugin: "discord" });
        await store.register("deleted", { plugin: "discord" });
        await expect(store.delete("deleted")).resolves.toBe(true);
        await telegramStore.register("retained", { plugin: "telegram" });
        await store.clear();
        await expect(store.entries()).resolves.toEqual([]);
        await expect(telegramStore.lookup("retained")).resolves.toEqual({ plugin: "telegram" });
        for (const method of sql) {
          expect(method).not.toHaveBeenCalled();
        }
      } finally {
        sql.forEach((method) => method.mockRestore());
      }

      const syncStore = api.runtime.state.openSyncKeyedStore<{ plugin: string }>({
        namespace: "sync-runtime",
        maxEntries: 10,
      });
      expect(syncStore.registerIfAbsent("k", { plugin: "discord" })).toBe(true);
      expect(syncStore.lookup("k")).toEqual({ plugin: "discord" });
      expect(syncStore.lookupMany?.(["k", "missing"])).toEqual([
        { ok: true, value: { plugin: "discord" } },
        { ok: true, value: undefined },
      ]);
    });
  });

  it("allows trusted official global plugins to use keyed state", async () => {
    await withOpenClawTestState({ label: "plugin-state-trusted-global" }, async () => {
      const registry = createTestPluginRegistry();
      const record = createPluginRecord("slack", "global", { trustedOfficialInstall: true });
      registry.registry.plugins.push(record);
      const api = registry.createApi(record, { config: {} });

      const store = api.runtime.state.openKeyedStore<{ plugin: string }>({
        namespace: "runtime",
        maxEntries: 10,
      });
      await expect(store.register("thread", { plugin: "slack" })).resolves.toBeUndefined();
      await expect(store.lookup("thread")).resolves.toEqual({ plugin: "slack" });
    });
  });

  it("binds blob stores to the trusted plugin id", async () => {
    await withOpenClawTestState({ label: "plugin-blob-runtime" }, async () => {
      const registry = createTestPluginRegistry();
      const record = createPluginRecord("diffs", "global", { trustedOfficialInstall: true });
      registry.registry.plugins.push(record);
      const api = registry.createApi(record, { config: {} });

      const store = api.runtime.state.openBlobStore<{ kind: string }>({
        namespace: "runtime",
        maxEntries: 10,
        maxBytesPerEntry: 1024,
        maxBytesPerNamespace: 4096,
      });
      await expect(
        store.registerIfAbsent("viewer", new Uint8Array([1, 2, 3]), { kind: "viewer" }),
      ).resolves.toBe(true);
      await expect(store.lookup("viewer")).resolves.toMatchObject({
        key: "viewer",
        metadata: { kind: "viewer" },
        sizeBytes: 3,
      });

      const otherRecord = createPluginRecord("other", "bundled");
      registry.registry.plugins.push(otherRecord);
      const otherStore = registry
        .createApi(otherRecord, { config: {} })
        .runtime.state.openBlobStore<{ kind: string }>({
          namespace: "runtime",
          maxEntries: 10,
          maxBytesPerEntry: 1024,
          maxBytesPerNamespace: 4096,
        });
      await expect(otherStore.lookup("viewer")).resolves.toBeUndefined();
    });
  });

  it("keeps blob and keyed namespace option policies independent", async () => {
    await withOpenClawTestState({ label: "plugin-state-policy-independence" }, async () => {
      const registry = createTestPluginRegistry();
      const record = createPluginRecord("diffs", "bundled");
      registry.registry.plugins.push(record);
      const state = registry.createApi(record, { config: {} }).runtime.state;

      const blob = state.openBlobStore({
        namespace: "shared-policy",
        maxEntries: 2,
        maxBytesPerEntry: 8,
        maxBytesPerNamespace: 16,
        overflowPolicy: "reject-new",
        defaultTtlMs: 100,
      });
      const keyed = state.openKeyedStore({
        namespace: "shared-policy",
        maxEntries: 3,
        overflowPolicy: "evict-oldest",
        defaultTtlMs: 200,
      });

      await expect(blob.register("blob", new Uint8Array([1]), {})).resolves.toBeUndefined();
      await expect(keyed.register("keyed", { ok: true })).resolves.toBeUndefined();
    });
  });

  it("ignores plugin-supplied state directory overrides", async () => {
    await withOpenClawTestState({ label: "plugin-blob-runtime-env" }, async (state) => {
      const registry = createTestPluginRegistry();
      const record = createPluginRecord("diffs", "global", { trustedOfficialInstall: true });
      registry.registry.plugins.push(record);
      const api = registry.createApi(record, { config: {} });
      const redirectedEnv = {
        ...state.env,
        OPENCLAW_STATE_DIR: `${state.stateDir}-redirected`,
      };

      const store = api.runtime.state.openBlobStore<{ kind: string }>({
        namespace: "runtime-env",
        maxEntries: 10,
        maxBytesPerEntry: 1024,
        maxBytesPerNamespace: 4096,
        env: redirectedEnv,
      } as OpenBlobStoreOptions & { env: NodeJS.ProcessEnv });
      await store.register("viewer", new Uint8Array([1]), { kind: "viewer" });

      resetPluginBlobStoreForTests();
      const { db } = openOpenClawStateDatabase({ env: state.env });
      expect(
        db
          .prepare(
            `SELECT COUNT(*) AS count FROM plugin_blob_entries
             WHERE plugin_id = ? AND namespace = ? AND entry_key = ?`,
          )
          .get("diffs", "runtime-env", "viewer"),
      ).toEqual({ count: 1 });
    });
  });

  it("rejects external plugins in this release", () => {
    const registry = createTestPluginRegistry();
    const record = createPluginRecord("external-plugin", "workspace");
    registry.registry.plugins.push(record);
    const api = registry.createApi(record, { config: {} });

    expect(() =>
      api.runtime.state.openKeyedStore({ namespace: "runtime", maxEntries: 10 }),
    ).toThrow("openKeyedStore is only available for trusted plugins");
    expect(() =>
      api.runtime.state.openSyncKeyedStore({ namespace: "runtime", maxEntries: 10 }),
    ).toThrow("openSyncKeyedStore is only available for trusted plugins");
    expect(() =>
      api.runtime.state.openBlobStore({
        namespace: "runtime",
        maxEntries: 10,
        maxBytesPerEntry: 1024,
        maxBytesPerNamespace: 4096,
      }),
    ).toThrow("openBlobStore is only available for trusted plugins");
  });

  it("names the denied capability, plugin, and origin for channel ingress queues", () => {
    const registry = createTestPluginRegistry();
    const record = createPluginRecord("slack", "config");
    registry.registry.plugins.push(record);
    const api = registry.createApi(record, { config: {} });

    expect(() => api.runtime.state.openChannelIngressQueue()).toThrow(
      /openChannelIngressQueue is only available for trusted plugins in this release\. Plugin "slack" loaded with origin "config"/,
    );
  });

  it("rejects untrusted global plugins", () => {
    const registry = createTestPluginRegistry();
    const record = createPluginRecord("diffs", "global");
    registry.registry.plugins.push(record);
    const api = registry.createApi(record, { config: {} });

    expect(() =>
      api.runtime.state.openKeyedStore({ namespace: "runtime", maxEntries: 10 }),
    ).toThrow("openKeyedStore is only available for trusted plugins");
    expect(() =>
      api.runtime.state.openBlobStore({
        namespace: "runtime",
        maxEntries: 10,
        maxBytesPerEntry: 1024,
        maxBytesPerNamespace: 4096,
      }),
    ).toThrow("openBlobStore is only available for trusted plugins");
  });
});