Reorganize repo structure and standards references

This commit is contained in:
david committed 2026-06-29 05:03:42 +02:00
1 parent 3cb93a81be
commit be2054274d
5885 files changed
+2588 -195

No files matched your search

@@ -0,0 +1,899 @@
/**
* Phase 06 #21 / F-015 — bootstrapPluginInvokeDeps wiring tests.
*
* Verifies that the production composition wires Prisma, SharedStore,
* env, and logger into a valid PluginInvokeDeps record. The deeper
* end-to-end behaviour of `handlePluginInvokeJob` is covered by
* `plugin-invoke.test.ts`; here we only assert the composition itself.
*/
import { describe, expect, test, vi } from "vitest";
import { bootstrapPluginInvokeDeps, type PluginInvokeBootstrapPrismaSurface } from "./plugin-invoke-bootstrap.js";
function makeFakeStore() {
const counters = new Map<string, number>();
const ttls = new Map<string, number>();
const values = new Map<string, string>();
return {
namespace: "test",
ttlDefault: undefined,
adapter: {} as never,
key: (s: string) => `test:${s}`,
incr: vi.fn(async (key: string, by = 1) => {
const next = (counters.get(key) ?? 0) + by;
counters.set(key, next);
return next;
}),
expire: vi.fn(async (key: string, ttlSeconds: number) => {
ttls.set(key, ttlSeconds);
return true;
}),
get: vi.fn(async (key: string) => values.get(key) ?? null),
set: vi.fn(async (key: string, value: string) => {
values.set(key, value);
return true;
}),
del: vi.fn(async (key: string) => (counters.delete(key) ? 1 : 0)),
mget: vi.fn(async () => []),
scan: vi.fn(async function* () {}),
publish: vi.fn(async () => 0),
subscribe: vi.fn(async () => async () => undefined),
zAdd: vi.fn(async () => 0),
zRemRangeByScore: vi.fn(async () => 0),
zCard: vi.fn(async () => 0),
eval: vi.fn(async () => undefined),
close: vi.fn(async () => undefined),
counters,
ttls,
};
}
type FakePluginRow = {
tenantId: string;
pluginId: string;
version: string;
manifest: unknown;
wasmSha256: string;
signaturePublicKey: string;
enabled: boolean;
};
function makeFakePrisma() {
return {
plugin: {
findUnique: vi.fn<(args: unknown) => Promise<FakePluginRow | null>>(async () => null),
update: vi.fn<(args: unknown) => Promise<unknown>>(async () => ({})),
},
outboxEvent: {
create: vi.fn<(args: unknown) => Promise<unknown>>(async () => ({})),
findUnique: vi.fn<(args: unknown) => Promise<unknown | null>>(async () => null),
},
pluginInvocation: {
create: vi.fn<(args: unknown) => Promise<unknown>>(async () => ({})),
},
tenantPluginCapabilityGrant: {
findMany: vi.fn<(args: unknown) => Promise<Array<{ capabilityName: string; scopeJson: unknown }>>>(async () => []),
},
};
}
const noopLogger = {
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
};
const HOST_IMPORT_WASM = new Uint8Array([
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00,
0x01, 0x0a, 0x02, 0x60, 0x01, 0x7f, 0x00, 0x60, 0x01, 0x7f, 0x01, 0x7f,
0x02, 0x30, 0x02,
0x03, 0x65, 0x6e, 0x76, 0x0c, 0x74, 0x6f, 0x6f, 0x6c, 0x63, 0x74, 0x72, 0x6c, 0x5f,
0x6c, 0x6f, 0x67, 0x00, 0x00,
0x03, 0x65, 0x6e, 0x76, 0x15, 0x74, 0x6f, 0x6f, 0x6c, 0x63, 0x74, 0x72, 0x6c, 0x5f,
0x61, 0x75, 0x64, 0x69, 0x74, 0x5f, 0x72, 0x65, 0x63, 0x6f, 0x72, 0x64, 0x00, 0x00,
0x03, 0x02, 0x01, 0x01,
0x07, 0x07, 0x01, 0x03, 0x72, 0x75, 0x6e, 0x00, 0x02,
0x0a, 0x0e, 0x01, 0x0c, 0x00, 0x20, 0x00, 0x10, 0x00, 0x20, 0x00, 0x10, 0x01, 0x20,
0x00, 0x0b,
]);
const INFINITE_LOOP_WASM = new Uint8Array([
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00,
0x01, 0x04, 0x01, 0x60, 0x00, 0x00,
0x03, 0x02, 0x01, 0x00,
0x07, 0x07, 0x01, 0x03, 0x72, 0x75, 0x6e, 0x00, 0x00,
0x0a, 0x09, 0x01, 0x07, 0x00, 0x03, 0x40, 0x0c, 0x00, 0x0b, 0x0b,
]);
function manifestWithCapabilities(capabilities: Array<{ name: "log" | "audit" }>) {
return {
schemaVersion: 1 as const,
id: "de.toolctrl.test",
version: "1.0.0",
name: "Test",
entry: "plugin.wasm",
wasmSha256: "0".repeat(64),
signature: "0".repeat(128),
signedBy: "0".repeat(64),
allowedHosts: [],
capabilities,
hooks: [{ name: "run", export: "run" }],
};
}
describe("bootstrapPluginInvokeDeps", () => {
test("fails closed in production until the plugin runtime rollout gate is enabled", () => {
expect(() =>
bootstrapPluginInvokeDeps({
env: { NODE_ENV: "production" },
prisma: makeFakePrisma(),
store: makeFakeStore() as never,
logger: noopLogger,
})
).toThrow("PLUGIN_RUNTIME_PRODUCTION_ENABLED=true");
});
test("returns a record with every PluginInvokeDeps seam populated", () => {
const deps = bootstrapPluginInvokeDeps({
env: { PLUGIN_TRUSTED_PUBLIC_KEYS: "aa".repeat(32) },
prisma: makeFakePrisma(),
store: makeFakeStore() as never,
logger: noopLogger,
});
expect(typeof deps.loadPlugin).toBe("function");
expect(typeof deps.loadCapabilityGrants).toBe("function");
expect(typeof deps.loadBinary).toBe("function");
expect(typeof deps.verifySignature).toBe("function");
expect(typeof deps.consumeRateLimit).toBe("function");
expect(typeof deps.invokeInSandbox).toBe("function");
expect(typeof deps.recordAudit).toBe("function");
expect(typeof deps.recordOutcomeForCrashTracking).toBe("function");
expect(Array.isArray(deps.trustedPublicKeysHex)).toBe(true);
expect(deps.trustedPublicKeysHex).toEqual(["aa".repeat(32)]);
expect(deps.logger).toBe(noopLogger);
});
test("loadCapabilityGrants returns tenant plugin grants for runtime enforcement", async () => {
const prisma = makeFakePrisma();
prisma.tenantPluginCapabilityGrant.findMany.mockResolvedValue([
{ capabilityName: "log", scopeJson: null },
{ capabilityName: "http.outbound", scopeJson: { allowedHosts: ["api.example.com"] } },
]);
const deps = bootstrapPluginInvokeDeps({
env: {},
prisma,
store: makeFakeStore() as never,
logger: noopLogger,
});
await expect(deps.loadCapabilityGrants({ tenantId: "tenant-1", pluginId: "de.toolctrl.test" })).resolves.toEqual([
{ capabilityName: "log", scopeJson: null },
{ capabilityName: "http.outbound", scopeJson: { allowedHosts: ["api.example.com"] } },
]);
expect(prisma.tenantPluginCapabilityGrant.findMany).toHaveBeenCalledWith({
where: { tenantId: "tenant-1", pluginId: "de.toolctrl.test" },
select: { capabilityName: true, scopeJson: true },
});
});
test("loadPlugin returns null when the row is missing", async () => {
const prisma = makeFakePrisma();
const deps = bootstrapPluginInvokeDeps({
env: {},
prisma,
store: makeFakeStore() as never,
logger: noopLogger,
});
const row = await deps.loadPlugin({ tenantId: "t", pluginId: "p", version: "1.0.0" });
expect(row).toBeNull();
expect(prisma.plugin.findUnique).toHaveBeenCalledWith({
where: { tenantId_pluginId: { tenantId: "t", pluginId: "p" } },
});
});
test("loadPlugin returns null when the stored row's version does not match", async () => {
const prisma = makeFakePrisma();
prisma.plugin.findUnique = vi.fn(async () => ({
tenantId: "t",
pluginId: "p",
version: "2.0.0",
manifest: {},
wasmSha256: "x",
signaturePublicKey: "y",
enabled: true,
}));
const deps = bootstrapPluginInvokeDeps({
env: {},
prisma,
store: makeFakeStore() as never,
logger: noopLogger,
});
expect(await deps.loadPlugin({ tenantId: "t", pluginId: "p", version: "1.0.0" })).toBeNull();
});
test("loadPlugin returns parsed rows only when the stored manifest is valid", async () => {
const prisma = makeFakePrisma();
const manifest = manifestWithCapabilities([{ name: "log" }]);
prisma.plugin.findUnique = vi.fn(async () => ({
tenantId: "t",
pluginId: "de.toolctrl.test",
version: "1.0.0",
manifest,
wasmSha256: "0".repeat(64),
signaturePublicKey: "0".repeat(64),
enabled: true,
}));
const deps = bootstrapPluginInvokeDeps({
env: {},
prisma,
store: makeFakeStore() as never,
logger: noopLogger,
});
await expect(deps.loadPlugin({ tenantId: "t", pluginId: "de.toolctrl.test", version: "1.0.0" })).resolves.toMatchObject({
tenantId: "t",
pluginId: "de.toolctrl.test",
manifest,
enabled: true,
});
});
test("loadPlugin returns null and warns when the stored manifest is invalid", async () => {
const prisma = makeFakePrisma();
prisma.plugin.findUnique = vi.fn(async () => ({
tenantId: "t",
pluginId: "de.toolctrl.test",
version: "1.0.0",
manifest: { hooks: [{ name: "run", export: "run" }] },
wasmSha256: "0".repeat(64),
signaturePublicKey: "0".repeat(64),
enabled: true,
}));
const logger = { info: vi.fn(), warn: vi.fn(), error: vi.fn() };
const deps = bootstrapPluginInvokeDeps({
env: {},
prisma,
store: makeFakeStore() as never,
logger,
});
await expect(deps.loadPlugin({ tenantId: "t", pluginId: "de.toolctrl.test", version: "1.0.0" })).resolves.toBeNull();
expect(logger.warn).toHaveBeenCalledWith(
expect.objectContaining({
tenantId: "t",
pluginId: "de.toolctrl.test",
version: "1.0.0",
}),
"plugin.invoke.stored_manifest_invalid"
);
});
test("loadBinary returns null on ENOENT instead of throwing", async () => {
const deps = bootstrapPluginInvokeDeps({
env: { PLUGIN_BINARY_BASE_PATH: "/tmp/does-not-exist-for-tests" },
prisma: makeFakePrisma(),
store: makeFakeStore() as never,
logger: noopLogger,
});
const buf = await deps.loadBinary({ pluginId: "ghost", version: "1.0.0" });
expect(buf).toBeNull();
});
test("loadBinary refuses plugin paths that escape the configured base directory", async () => {
const logger = { info: vi.fn(), warn: vi.fn(), error: vi.fn() };
const deps = bootstrapPluginInvokeDeps({
env: { PLUGIN_BINARY_BASE_PATH: "/tmp/toolctrl-plugins" },
prisma: makeFakePrisma(),
store: makeFakeStore() as never,
logger,
});
const buf = await deps.loadBinary({ pluginId: "../outside", version: "../../1.0.0" });
expect(buf).toBeNull();
expect(logger.warn).toHaveBeenCalledWith(
{ pluginId: "../outside", version: "../../1.0.0" },
"plugin.invoke.binary_path_escape_blocked"
);
});
test("consumeRateLimit allows the first N calls then rejects", async () => {
const store = makeFakeStore();
const deps = bootstrapPluginInvokeDeps({
env: { PLUGIN_RATE_LIMIT_PER_MINUTE: "3", PLUGIN_RATE_LIMIT_WINDOW_SECONDS: "60" },
prisma: makeFakePrisma(),
store: store as never,
logger: noopLogger,
});
const first = await deps.consumeRateLimit({ tenantId: "t", pluginId: "p" });
const second = await deps.consumeRateLimit({ tenantId: "t", pluginId: "p" });
const third = await deps.consumeRateLimit({ tenantId: "t", pluginId: "p" });
const fourth = await deps.consumeRateLimit({ tenantId: "t", pluginId: "p" });
expect([first.allowed, second.allowed, third.allowed, fourth.allowed]).toEqual([true, true, true, false]);
// First hit must seed the TTL so the window slides from the first hit.
expect(store.expire).toHaveBeenCalledTimes(1);
});
test("invokeInSandbox wires log and audit host imports through declared capabilities", async () => {
const logger = { info: vi.fn(), warn: vi.fn(), error: vi.fn() };
const prisma = makeFakePrisma();
const deps = bootstrapPluginInvokeDeps({
env: {},
prisma,
store: makeFakeStore() as never,
logger,
});
const capabilities = [{ name: "log" as const }, { name: "audit" as const }];
const result = await deps.invokeInSandbox({
pluginInvocationId: "inv-1",
tenantId: "tenant-1",
pluginId: "de.toolctrl.test",
wasmBuffer: HOST_IMPORT_WASM,
manifest: manifestWithCapabilities(capabilities),
hookName: "run",
payload: 7,
capabilityGrants: capabilities.map((capability) => ({ capabilityName: capability.name, scopeJson: null })),
});
expect(result).toMatchObject({ ok: true, output: 7 });
expect(prisma.outboxEvent.create).toHaveBeenCalledWith({
data: expect.objectContaining({
tenantId: "tenant-1",
eventType: "audit.event.create",
dispatchKey: "plugin-host-audit:inv-1:0",
priority: 20,
payload: expect.objectContaining({
tenantId: "tenant-1",
actorId: "de.toolctrl.test",
actorType: "plugin",
action: "plugin.audit.recorded",
targetType: "plugin",
targetId: "de.toolctrl.test",
requestId: "inv-1",
sourceModule: "worker.plugin-runtime",
outcome: "success",
dataSensitivity: "internal",
regulatoryScope: ["soc2"],
metadata: expect.objectContaining({
pluginId: "de.toolctrl.test",
pluginManifestId: "de.toolctrl.test",
pluginInvocationId: "inv-1",
version: "1.0.0",
hookName: "run",
hostBinding: "audit",
sequence: 0,
value: { value: 7 },
}),
}),
}),
});
expect(prisma.outboxEvent.create).toHaveBeenCalledWith({
data: expect.objectContaining({
tenantId: "tenant-1",
eventType: "plugin.capability-use.batch",
dispatchKey: expect.stringMatching(/^plugin-capability-use:/),
priority: 10,
}),
});
const outboxArgs = prisma.outboxEvent.create.mock.calls
.map((call) => call[0] as { data: { eventType: string; payload: { entries?: Array<{ capability: string; outcome: string }> } } })
.find((call) => call.data.eventType === "plugin.capability-use.batch");
expect(outboxArgs).toBeDefined();
expect(outboxArgs?.data.payload.entries).toHaveLength(2);
expect(outboxArgs?.data.payload.entries?.map((entry) => [entry.capability, entry.outcome])).toEqual([
["log", "success"],
["audit", "success"],
]);
expect(logger.info).toHaveBeenCalledWith(
expect.objectContaining({
pluginId: "de.toolctrl.test",
value: { value: 7 },
version: "1.0.0",
}),
"plugin.invoke.host.log"
);
expect(logger.info).toHaveBeenCalledWith(
expect.objectContaining({
pluginId: "de.toolctrl.test",
value: { value: 7 },
version: "1.0.0",
}),
"plugin.invoke.host.audit"
);
expect(logger.info).toHaveBeenCalledWith(
expect.objectContaining({
tenantId: "tenant-1",
pluginId: "de.toolctrl.test",
pluginInvocationId: "inv-1",
sequence: 0,
}),
"plugin.invoke.host.audit_enqueued"
);
});
test("invokeInSandbox fails closed when capability-use audit cannot be enqueued", async () => {
const logger = { info: vi.fn(), warn: vi.fn(), error: vi.fn() };
const prisma = makeFakePrisma();
prisma.outboxEvent.create.mockRejectedValueOnce(new Error("outbox down"));
const deps = bootstrapPluginInvokeDeps({
env: {},
prisma,
store: makeFakeStore() as never,
logger,
});
const capabilities = [{ name: "log" as const }];
await expect(
deps.invokeInSandbox({
pluginInvocationId: "inv-audit-down",
tenantId: "tenant-1",
pluginId: "de.toolctrl.test",
wasmBuffer: HOST_IMPORT_WASM,
manifest: manifestWithCapabilities(capabilities),
hookName: "run",
payload: 7,
capabilityGrants: capabilities.map((capability) => ({ capabilityName: capability.name, scopeJson: null })),
})
).rejects.toThrow("outbox down");
expect(logger.error).toHaveBeenCalledWith(
expect.objectContaining({
pluginInvocationId: "inv-audit-down",
tenantId: "tenant-1",
pluginId: "de.toolctrl.test",
entryCount: expect.any(Number),
}),
"plugin capability-use audit flush failed"
);
});
test("invokeInSandbox fails closed when capability-use audit entries are truncated", async () => {
const logger = { info: vi.fn(), warn: vi.fn(), error: vi.fn() };
const prisma = makeFakePrisma();
const deps = bootstrapPluginInvokeDeps({
env: { PLUGIN_CAPABILITY_AUDIT_MAX_ENTRIES: "1" },
prisma,
store: makeFakeStore() as never,
logger,
});
const capabilities = [{ name: "log" as const }, { name: "audit" as const }];
await expect(
deps.invokeInSandbox({
pluginInvocationId: "inv-audit-truncated",
tenantId: "tenant-1",
pluginId: "de.toolctrl.test",
wasmBuffer: HOST_IMPORT_WASM,
manifest: manifestWithCapabilities(capabilities),
hookName: "run",
payload: 7,
capabilityGrants: capabilities.map((capability) => ({ capabilityName: capability.name, scopeJson: null })),
})
).rejects.toThrow("Plugin capability-use audit dropped 1 entries after maxEntries=1");
expect(prisma.outboxEvent.create).not.toHaveBeenCalledWith(
expect.objectContaining({
data: expect.objectContaining({
type: "plugin.capability-use.batch",
}),
})
);
});
test("invokeInSandbox treats duplicate capability-use dispatch keys as already enqueued", async () => {
const logger = { info: vi.fn(), warn: vi.fn(), error: vi.fn() };
const prisma = makeFakePrisma();
prisma.outboxEvent.create
.mockResolvedValueOnce({})
.mockRejectedValueOnce(Object.assign(new Error("Unique constraint failed"), { code: "P2002" }));
prisma.outboxEvent.findUnique.mockResolvedValueOnce({
id: "outbox-existing",
dispatchKey: "plugin-capability-use:inv-duplicate",
});
const deps = bootstrapPluginInvokeDeps({
env: {},
prisma,
store: makeFakeStore() as never,
logger,
});
const capabilities = [{ name: "log" as const }, { name: "audit" as const }];
const result = await deps.invokeInSandbox({
pluginInvocationId: "inv-duplicate",
tenantId: "tenant-1",
pluginId: "de.toolctrl.test",
wasmBuffer: HOST_IMPORT_WASM,
manifest: manifestWithCapabilities(capabilities),
hookName: "run",
payload: 7,
capabilityGrants: capabilities.map((capability) => ({ capabilityName: capability.name, scopeJson: null })),
});
expect(result).toMatchObject({ ok: true, output: 7 });
expect(prisma.outboxEvent.findUnique).toHaveBeenCalledWith({
where: { dispatchKey: "plugin-capability-use:inv-duplicate" },
});
expect(logger.error).not.toHaveBeenCalledWith(
expect.objectContaining({
pluginInvocationId: "inv-duplicate",
}),
"plugin capability-use audit flush failed"
);
expect(logger.info).toHaveBeenCalledWith(
expect.objectContaining({
pluginInvocationId: "inv-duplicate",
entryCount: 2,
}),
"plugin.invoke.capability_use_enqueued"
);
});
test("invokeInSandbox treats duplicate host-audit dispatch keys as already enqueued", async () => {
const logger = { info: vi.fn(), warn: vi.fn(), error: vi.fn() };
const prisma = makeFakePrisma();
prisma.outboxEvent.create.mockRejectedValueOnce(Object.assign(new Error("Unique constraint failed"), { code: "P2002" }));
prisma.outboxEvent.findUnique.mockResolvedValueOnce({
id: "outbox-existing",
dispatchKey: "plugin-host-audit:inv-host-audit-duplicate:0",
});
const deps = bootstrapPluginInvokeDeps({
env: {},
prisma,
store: makeFakeStore() as never,
logger,
});
const capabilities = [{ name: "log" as const }, { name: "audit" as const }];
const result = await deps.invokeInSandbox({
pluginInvocationId: "inv-host-audit-duplicate",
tenantId: "tenant-1",
pluginId: "de.toolctrl.test",
wasmBuffer: HOST_IMPORT_WASM,
manifest: manifestWithCapabilities(capabilities),
hookName: "run",
payload: 7,
capabilityGrants: capabilities.map((capability) => ({ capabilityName: capability.name, scopeJson: null })),
});
expect(result).toMatchObject({ ok: true, output: 7 });
expect(prisma.outboxEvent.findUnique).toHaveBeenCalledWith({
where: { dispatchKey: "plugin-host-audit:inv-host-audit-duplicate:0" },
});
expect(logger.info).toHaveBeenCalledWith(
expect.objectContaining({
pluginInvocationId: "inv-host-audit-duplicate",
sequence: 0,
}),
"plugin.invoke.host.audit_enqueued"
);
expect(prisma.outboxEvent.create).toHaveBeenCalledWith({
data: expect.objectContaining({
eventType: "plugin.capability-use.batch",
dispatchKey: "plugin-capability-use:inv-host-audit-duplicate",
}),
});
});
test("invokeInSandbox denies undeclared host imports through the gateway", async () => {
const logger = { info: vi.fn(), warn: vi.fn(), error: vi.fn() };
const prisma = makeFakePrisma();
const deps = bootstrapPluginInvokeDeps({
env: {},
prisma,
store: makeFakeStore() as never,
logger,
});
const capabilities = [{ name: "log" as const }];
const result = await deps.invokeInSandbox({
pluginInvocationId: "inv-2",
tenantId: "tenant-1",
pluginId: "de.toolctrl.test",
wasmBuffer: HOST_IMPORT_WASM,
manifest: manifestWithCapabilities(capabilities),
hookName: "run",
payload: 7,
capabilityGrants: capabilities.map((capability) => ({ capabilityName: capability.name, scopeJson: null })),
});
expect(result.ok).toBe(false);
expect(prisma.outboxEvent.create).toHaveBeenCalledWith({
data: expect.objectContaining({
tenantId: "tenant-1",
eventType: "plugin.capability-use.batch",
}),
});
const outboxArgs = prisma.outboxEvent.create.mock.calls[0]?.[0] as { data: { payload: { entries: Array<{ capability: string; outcome: string; errorCode?: string }> } } };
expect(outboxArgs.data.payload.entries).toEqual([
expect.objectContaining({
capability: "log",
outcome: "success",
}),
expect.objectContaining({
capability: "audit",
outcome: "denied",
errorCode: "not_declared_in_manifest",
}),
]);
if (!result.ok) {
expect(result.reason).toBe("trap");
expect(result.message).toContain("capability denied: audit");
}
expect(logger.warn).toHaveBeenCalledWith(
expect.objectContaining({
capability: "audit",
reason: "not_declared_in_manifest",
}),
"plugin.invoke.capability_denied"
);
});
test("invokeInSandbox denies manifest-declared host imports without tenant grants", async () => {
const logger = { info: vi.fn(), warn: vi.fn(), error: vi.fn() };
const prisma = makeFakePrisma();
const deps = bootstrapPluginInvokeDeps({
env: {},
prisma,
store: makeFakeStore() as never,
logger,
});
const capabilities = [{ name: "log" as const }, { name: "audit" as const }];
const result = await deps.invokeInSandbox({
pluginInvocationId: "inv-missing-grant",
tenantId: "tenant-1",
pluginId: "de.toolctrl.test",
wasmBuffer: HOST_IMPORT_WASM,
manifest: manifestWithCapabilities(capabilities),
hookName: "run",
payload: 7,
capabilityGrants: [{ capabilityName: "log", scopeJson: null }],
});
expect(result.ok).toBe(false);
const outboxArgs = prisma.outboxEvent.create.mock.calls[0]?.[0] as { data: { payload: { entries: Array<{ capability: string; outcome: string; errorCode?: string }> } } };
expect(outboxArgs.data.payload.entries).toEqual([
expect.objectContaining({
capability: "log",
outcome: "success",
}),
expect.objectContaining({
capability: "audit",
outcome: "denied",
errorCode: "not_granted_for_tenant",
}),
]);
if (!result.ok) {
expect(result.reason).toBe("trap");
expect(result.message).toContain("capability denied: audit");
}
expect(logger.warn).toHaveBeenCalledWith(
expect.objectContaining({
capability: "audit",
reason: "not_granted_for_tenant",
}),
"plugin.invoke.capability_denied"
);
});
test("invokeInSandbox enforces PLUGIN_MAX_WALL_CLOCK_MS", async () => {
const logger = { info: vi.fn(), warn: vi.fn(), error: vi.fn() };
const prisma = makeFakePrisma();
const deps = bootstrapPluginInvokeDeps({
env: { PLUGIN_MAX_WALL_CLOCK_MS: "50" },
prisma,
store: makeFakeStore() as never,
logger,
});
const result = await deps.invokeInSandbox({
pluginInvocationId: "inv-timeout",
tenantId: "tenant-1",
pluginId: "de.toolctrl.test",
wasmBuffer: INFINITE_LOOP_WASM,
manifest: manifestWithCapabilities([]),
hookName: "run",
payload: undefined,
capabilityGrants: [],
});
expect(result).toMatchObject({
ok: false,
reason: "timeout",
message: "plugin sandbox wall-clock timeout",
});
expect(prisma.outboxEvent.create).not.toHaveBeenCalled();
}, 5_000);
test("recordAudit success path logs at info level, failure at error", async () => {
const logger = { info: vi.fn(), warn: vi.fn(), error: vi.fn() };
const prisma = makeFakePrisma();
const deps = bootstrapPluginInvokeDeps({
env: {},
prisma,
store: makeFakeStore() as never,
logger,
});
await deps.recordAudit({
tenantId: "t",
pluginId: "p",
version: "1.0.0",
hookName: "tenant.created",
pluginInvocationId: "inv-success",
outcome: { ok: true, output: 1, fuelUsed: 0, durationMs: 5, pluginInvocationId: "inv-success" },
});
expect(prisma.pluginInvocation.create).toHaveBeenCalledWith({
data: expect.objectContaining({
id: "inv-success",
outcome: "success",
failureReason: null,
fuelUsed: 0,
}),
});
expect(logger.info).toHaveBeenCalledWith(expect.any(Object), "plugin.invoke.audit.success");
await deps.recordAudit({
tenantId: "t",
pluginId: "p",
version: "1.0.0",
hookName: "tenant.created",
pluginInvocationId: "inv-failure",
outcome: { ok: false, reason: "rate_limit_exceeded", durationMs: 1, pluginInvocationId: "inv-failure" },
});
expect(prisma.pluginInvocation.create).toHaveBeenLastCalledWith({
data: expect.objectContaining({
id: "inv-failure",
outcome: "failure",
failureReason: "rate_limit_exceeded",
fuelUsed: null,
}),
});
expect(logger.error).toHaveBeenCalledWith(expect.any(Object), "plugin.invoke.audit.failure");
});
test("recordOutcomeForCrashTracking records success vs failure on the tracker", async () => {
const store = makeFakeStore();
const deps = bootstrapPluginInvokeDeps({
env: {},
prisma: makeFakePrisma(),
store: store as never,
logger: noopLogger,
});
await deps.recordOutcomeForCrashTracking!({
tenantId: "t",
pluginId: "p",
outcome: { ok: false, reason: "sandbox_failed", durationMs: 0, pluginInvocationId: "inv-failure" },
});
expect(store.incr).toHaveBeenCalled();
await deps.recordOutcomeForCrashTracking!({
tenantId: "t",
pluginId: "p",
outcome: { ok: true, output: undefined, fuelUsed: 0, durationMs: 0, pluginInvocationId: "inv-success" },
});
// Success decrement reads + writes the counter.
expect(store.get).toHaveBeenCalled();
});
test("recordOutcomeForCrashTracking persists plugin auto-disable when the threshold is crossed", async () => {
const prisma = makeFakePrisma();
const transaction = vi.fn<NonNullable<PluginInvokeBootstrapPrismaSurface["$transaction"]>>(async (fn) => fn(prisma));
const prismaWithTransaction = { ...prisma, $transaction: transaction };
const deps = bootstrapPluginInvokeDeps({
env: { PLUGIN_AUTO_DISABLE_THRESHOLD: "2" },
prisma: prismaWithTransaction,
store: makeFakeStore() as never,
logger: noopLogger,
});
await deps.recordOutcomeForCrashTracking!({
tenantId: "tenant-1",
pluginId: "plugin-1",
outcome: { ok: false, reason: "sandbox_failed", durationMs: 0, pluginInvocationId: "inv-1" },
});
expect(prisma.plugin.update).not.toHaveBeenCalled();
await deps.recordOutcomeForCrashTracking!({
tenantId: "tenant-1",
pluginId: "plugin-1",
outcome: { ok: false, reason: "sandbox_failed", durationMs: 0, pluginInvocationId: "inv-2" },
});
expect(prisma.plugin.update).toHaveBeenCalledWith({
where: { tenantId_pluginId: { tenantId: "tenant-1", pluginId: "plugin-1" } },
data: { enabled: false },
});
expect(prisma.outboxEvent.create).toHaveBeenCalledWith({
data: expect.objectContaining({
tenantId: "tenant-1",
eventType: "audit.event.create",
dispatchKey: "plugin-auto-disable-audit:tenant-1:plugin-1:inv-2",
priority: 20,
payload: expect.objectContaining({
tenantId: "tenant-1",
actorType: "system",
action: "plugin.auto_disabled",
targetType: "plugin",
targetId: "plugin-1",
sourceModule: "worker.plugin-runtime",
outcome: "blocked",
metadata: expect.objectContaining({
pluginId: "plugin-1",
failureCount: 2,
reason: "sandbox_failed",
pluginInvocationId: "inv-2",
trigger: "crash-threshold",
}),
}),
}),
});
expect(transaction).toHaveBeenCalledOnce();
});
test("recordOutcomeForCrashTracking treats duplicate auto-disable audit dispatch keys as already enqueued", async () => {
const prisma = makeFakePrisma();
prisma.outboxEvent.create.mockRejectedValueOnce(Object.assign(new Error("Unique constraint failed"), { code: "P2002" }));
prisma.outboxEvent.findUnique.mockResolvedValueOnce({
id: "outbox-existing",
dispatchKey: "plugin-auto-disable-audit:tenant-1:plugin-1:inv-1",
});
const logger = { info: vi.fn(), warn: vi.fn(), error: vi.fn() };
const deps = bootstrapPluginInvokeDeps({
env: { PLUGIN_AUTO_DISABLE_THRESHOLD: "1" },
prisma,
store: makeFakeStore() as never,
logger,
});
await deps.recordOutcomeForCrashTracking!({
tenantId: "tenant-1",
pluginId: "plugin-1",
outcome: { ok: false, reason: "sandbox_failed", durationMs: 0, pluginInvocationId: "inv-1" },
});
expect(prisma.plugin.update).toHaveBeenCalledWith({
where: { tenantId_pluginId: { tenantId: "tenant-1", pluginId: "plugin-1" } },
data: { enabled: false },
});
expect(prisma.outboxEvent.findUnique).toHaveBeenCalledWith({
where: { dispatchKey: "plugin-auto-disable-audit:tenant-1:plugin-1:inv-1" },
});
expect(logger.info).toHaveBeenCalledWith(
expect.objectContaining({
tenantId: "tenant-1",
pluginId: "plugin-1",
failureCount: 1,
reason: "sandbox_failed",
}),
"plugin.crash.auto_disable_audit_enqueued"
);
});
test("recordOutcomeForCrashTracking uses invocation-scoped auto-disable audit keys after re-enable", async () => {
const prisma = makeFakePrisma();
const deps = bootstrapPluginInvokeDeps({
env: { PLUGIN_AUTO_DISABLE_THRESHOLD: "1" },
prisma,
store: makeFakeStore() as never,
logger: noopLogger,
});
await deps.recordOutcomeForCrashTracking!({
tenantId: "tenant-1",
pluginId: "plugin-1",
outcome: { ok: false, reason: "sandbox_failed", durationMs: 0, pluginInvocationId: "inv-first-disable" },
});
await deps.recordOutcomeForCrashTracking!({
tenantId: "tenant-1",
pluginId: "plugin-1",
outcome: { ok: false, reason: "sandbox_failed", durationMs: 0, pluginInvocationId: "inv-second-disable" },
});
const dispatchKeys = prisma.outboxEvent.create.mock.calls.map((call) => {
const args = call[0] as { data?: { dispatchKey?: string } };
return args.data?.dispatchKey;
});
expect(dispatchKeys).toEqual([
"plugin-auto-disable-audit:tenant-1:plugin-1:inv-first-disable",
"plugin-auto-disable-audit:tenant-1:plugin-1:inv-second-disable",
]);
});
});