File size: 34,065 Bytes
4c76b0d | 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 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 | // Session action contract tests cover plugin session action metadata and execution contracts.
import { expectDefined } from "@openclaw/normalization-core";
import {
createPluginRegistryFixture,
registerTestPlugin,
} from "openclaw/plugin-sdk/plugin-test-contracts";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import {
ADMIN_SCOPE,
APPROVALS_SCOPE,
PAIRING_SCOPE,
QUESTIONS_SCOPE,
READ_SCOPE,
TALK_SCOPE,
TALK_SECRETS_SCOPE,
WRITE_SCOPE,
type OperatorScope,
} from "../../gateway/operator-scopes.js";
import { handleGatewayRequest } from "../../gateway/server-methods.js";
import { pluginHostHookHandlers } from "../../gateway/server-methods/plugin-host-hooks.js";
import type { GatewayClient, RespondFn } from "../../gateway/server-methods/types.js";
import { onAgentEvent, resetAgentEventsForTest } from "../../infra/agent-events.js";
import type { PluginSessionActionContext } from "../host-hooks.js";
import { createEmptyPluginRegistry } from "../registry-empty.js";
import { createPluginRegistry } from "../registry.js";
import {
captureActivePluginRegistrySnapshot,
rollbackStagedPluginRegistry,
setActivePluginRegistry,
stageActivePluginRegistry,
} from "../runtime.js";
import { createPluginRecord } from "../status.test-fixtures.js";
import type { OpenClawPluginApi } from "../types.js";
const MAIN_SESSION_KEY = "agent:main:main";
type HookResponse = { ok: boolean; payload?: unknown; error?: unknown };
function sessionActionBody(
pluginId: string,
actionId: string,
extra?: Record<string, unknown>,
): Record<string, unknown> {
return {
pluginId,
actionId,
...extra,
};
}
async function callPluginSessionActionForTest(params: {
body: Record<string, unknown>;
scopes?: string[];
}): Promise<HookResponse> {
let response: HookResponse | undefined;
const respond: RespondFn = (ok, payload, error) => {
response = { ok, payload, error };
};
await expectDefined(
pluginHostHookHandlers["plugins.sessionAction"],
'pluginHostHookHandlers["plugins.sessionAction"] test invariant',
)({
req: { id: "test", type: "req", method: "plugins.sessionAction", params: params.body },
params: params.body,
client: {
connId: "test-client",
connect: { scopes: params.scopes ?? [WRITE_SCOPE] },
} as GatewayClient,
isWebchatConnect: () => false,
respond,
context: { getRuntimeConfig: () => ({}) } as never,
});
return response ?? { ok: false, error: new Error("handler did not respond") };
}
async function callRegisteredSessionActionForTest(params: {
pluginId: string;
actionId: string;
extra?: Record<string, unknown>;
scopes?: string[];
}): Promise<HookResponse> {
return callPluginSessionActionForTest({
body: sessionActionBody(params.pluginId, params.actionId, params.extra),
...(params.scopes ? { scopes: params.scopes } : {}),
});
}
async function callPluginSessionActionThroughGatewayForTest(params: {
body: Record<string, unknown>;
scopes?: string[];
}): Promise<HookResponse> {
let response: HookResponse | undefined;
const respond: RespondFn = (ok, payload, error) => {
response = { ok, payload, error };
};
await handleGatewayRequest({
req: { id: "test", type: "req", method: "plugins.sessionAction", params: params.body },
respond,
client: {
connId: "test-client",
connect: {
role: "operator",
scopes: params.scopes ?? [],
},
} as GatewayClient,
isWebchatConnect: () => false,
context: {
logGateway: {
warn() {},
},
getRuntimeConfig: () => ({}),
} as unknown as Parameters<typeof handleGatewayRequest>[0]["context"],
});
return response ?? { ok: false, error: new Error("handler did not respond") };
}
async function callRegisteredSessionActionThroughGatewayForTest(params: {
pluginId: string;
actionId: string;
extra?: Record<string, unknown>;
scopes?: string[];
}): Promise<HookResponse> {
return callPluginSessionActionThroughGatewayForTest({
body: sessionActionBody(params.pluginId, params.actionId, params.extra),
...(params.scopes ? { scopes: params.scopes } : {}),
});
}
function requireHookError(response: HookResponse): { code?: unknown; message?: unknown } {
expect(response.ok).toBe(false);
const error = response.error as { code?: unknown; message?: unknown } | undefined;
if (!error) {
throw new Error("expected hook error");
}
return error;
}
function requireObservedEvent(
observed: unknown[],
index: number,
): { runId?: unknown; sessionKey?: unknown; stream?: unknown; data?: Record<string, unknown> } {
const event = observed[index] as
| { runId?: unknown; sessionKey?: unknown; stream?: unknown; data?: Record<string, unknown> }
| undefined;
if (!event) {
throw new Error(`expected observed event #${index + 1}`);
}
return event;
}
function registerActionFixture(params: {
id: string;
name?: string;
register: (api: OpenClawPluginApi) => void;
}) {
const { config, registry } = createPluginRegistryFixture();
registerTestPlugin({
registry,
config,
record: createPluginRecord({
id: params.id,
name: params.name ?? params.id,
}),
register: params.register,
});
return { config, registry };
}
describe("plugin session actions", () => {
afterEach(() => {
setActivePluginRegistry(createEmptyPluginRegistry());
resetAgentEventsForTest();
});
it("initializes and registers typed session actions", () => {
expect(createEmptyPluginRegistry().sessionActions).toEqual([]);
const { registry } = registerActionFixture({
id: "session-action-fixture",
name: "Session Action Fixture",
register(api) {
api.registerSessionAction({
id: "approve",
description: "Approve the current workflow",
requiredScopes: [APPROVALS_SCOPE],
handler: () => ({ ok: true, result: { accepted: true } }),
});
},
});
expect(registry.registry.sessionActions).toHaveLength(1);
const actionEntry = registry.registry.sessionActions[0];
expect(actionEntry?.pluginId).toBe("session-action-fixture");
expect(actionEntry?.pluginName).toBe("Session Action Fixture");
expect(actionEntry?.action.id).toBe("approve");
expect(actionEntry?.action.description).toBe("Approve the current workflow");
expect(actionEntry?.action.requiredScopes).toEqual([APPROVALS_SCOPE]);
});
it("rejects invalid or duplicate session action registrations", () => {
const { registry } = registerActionFixture({
id: "invalid-session-actions",
name: "Invalid Session Actions",
register(api) {
for (const action of [
{ id: "dup" },
{ id: "dup" },
{ id: "bad-scope", requiredScopes: ["not-a-scope"] as never },
{ id: "bad-schema-shape", schema: "not-an-object" as never },
{ id: "bad-schema-compile", schema: { type: "not-a-json-schema-type" } as never },
{
id: "bad-schema-keyword",
schema: {
type: "object",
properties: { id: { type: "string" } },
required: "id",
} as never,
},
{
id: "bad-schema-ref",
schema: { $ref: "#/$defs/Missing" } as never,
},
{ id: "" },
]) {
api.registerSessionAction({
...action,
handler: () => ({ ok: true }),
});
}
},
});
expect(registry.registry.sessionActions.map((entry) => entry.action.id)).toEqual(["dup"]);
const diagnosticMessages = registry.registry.diagnostics?.map((diagnostic) => {
expect(diagnostic.pluginId).toBe("invalid-session-actions");
return diagnostic.message;
});
expect(diagnosticMessages).toHaveLength(7);
expect(diagnosticMessages).toContain("session action already registered: dup");
expect(diagnosticMessages).toContain(
"session action requiredScopes contains unknown operator scope: not-a-scope",
);
expect(diagnosticMessages).toContain(
"session action schema must be a JSON schema object or boolean: bad-schema-shape",
);
expect(
diagnosticMessages?.some((message) =>
message.includes("session action schema is not valid JSON Schema: bad-schema-compile"),
),
).toBe(true);
expect(
diagnosticMessages?.some((message) =>
message.includes("session action schema is not valid JSON Schema: bad-schema-keyword"),
),
).toBe(true);
expect(
diagnosticMessages?.some((message) =>
message.includes("session action schema is not valid JSON Schema: bad-schema-ref"),
),
).toBe(true);
expect(diagnosticMessages).toContain(
"session action registration requires id, handler, and valid optional fields",
);
});
it("validates payload schemas and typed action results", async () => {
const callSchemaAction = (
actionId: string,
extra?: Record<string, unknown>,
): Promise<{ ok: boolean; payload?: unknown; error?: unknown }> =>
callRegisteredSessionActionForTest({
pluginId: "schema-action-fixture",
actionId,
...(extra ? { extra } : {}),
});
const handlerCalls: unknown[] = [];
const { registry } = registerActionFixture({
id: "schema-action-fixture",
name: "Schema Action Fixture",
register(api) {
api.registerSessionAction({
id: "approve",
schema: {
type: "object",
additionalProperties: false,
required: ["version"],
properties: {
version: { type: "string" },
},
},
handler: ({ payload, sessionKey, client }) => {
handlerCalls.push({ payload, sessionKey, scopes: client?.scopes ?? [] });
return {
result: { accepted: true, ...(sessionKey ? { sessionKey } : {}) },
continueAgent: true,
reply: { text: "approved" },
};
},
});
api.registerSessionAction({
id: "typed-error",
handler: () => ({
ok: false,
error: "needs operator input",
code: "needs_input",
details: { field: "version" },
}),
});
api.registerSessionAction({
id: "allow-any",
schema: true,
handler: ({ payload }) => ({ result: { payload: payload ?? null } }),
});
api.registerSessionAction({
id: "deny-all",
schema: false,
handler: () => ({ result: { unreachable: true } }),
});
},
});
setActivePluginRegistry(registry.registry);
const rejected = await callPluginSessionActionForTest({
body: sessionActionBody("schema-action-fixture", "approve", { payload: { version: 1 } }),
});
const rejectedError = requireHookError(rejected);
expect(rejectedError.code).toBe("INVALID_REQUEST");
expect(String(rejectedError.message)).toContain(
"plugin session action payload does not match schema",
);
expect(handlerCalls).toEqual([]);
await expect(
callSchemaAction("approve", {
sessionKey: MAIN_SESSION_KEY,
payload: { version: "2026.05.01" },
}),
).resolves.toEqual({
ok: true,
payload: {
ok: true,
result: { accepted: true, sessionKey: MAIN_SESSION_KEY },
continueAgent: true,
reply: { text: "approved" },
},
error: undefined,
});
expect(handlerCalls).toEqual([
{
payload: { version: "2026.05.01" },
sessionKey: MAIN_SESSION_KEY,
scopes: [WRITE_SCOPE],
},
]);
await expect(callSchemaAction("typed-error")).resolves.toEqual({
ok: true,
payload: {
ok: false,
error: "needs operator input",
code: "needs_input",
details: {
field: "version",
},
},
error: undefined,
});
await expect(
callSchemaAction("allow-any", { payload: { any: ["json", true] } }),
).resolves.toEqual({
ok: true,
payload: {
ok: true,
result: { payload: { any: ["json", true] } },
},
error: undefined,
});
const denyAll = await callSchemaAction("deny-all", { payload: { rejected: true } });
expect(requireHookError(denyAll).code).toBe("INVALID_REQUEST");
});
it("validates plugin session action results before returning gateway payloads", async () => {
const callValidationAction = (
actionId: string,
): Promise<{ ok: boolean; payload?: unknown; error?: unknown }> =>
callRegisteredSessionActionForTest({
pluginId: "session-action-validation-fixture",
actionId,
});
const { registry } = registerActionFixture({
id: "session-action-validation-fixture",
name: "Session Action Validation Fixture",
register(api) {
const handlers = {
"bad-result": () => ({ result: 1n as never }),
"bad-reply": () => ({ reply: { text: "ok", extra: () => undefined } as never }),
"primitive-result": () => "not-an-object" as never,
"typed-error": () => ({
ok: false,
error: "needs operator input",
code: "needs_input",
details: { field: "version" },
}),
"bad-ok": () =>
({
ok: "false",
error: "must not masquerade as success",
}) as never,
"error-shaped-success": () =>
({
error: "must declare ok false",
}) as never,
"bad-error-details": () => ({
ok: false,
error: "bad details",
details: { value: 1n } as never,
}),
"bad-continue-agent": () => ({ continueAgent: "yes" as never }),
"mixed-branch-fields": () =>
({
ok: false,
error: "stop",
continueAgent: true,
result: { leaked: true },
}) as never,
"unknown-success-field": () =>
({
result: { accepted: true },
extra: "unexpected",
}) as never,
"throws-secret": () => {
throw new Error("fixture action failed");
},
};
for (const [id, handler] of Object.entries(handlers)) {
api.registerSessionAction({ id, handler: handler as never });
}
},
});
setActivePluginRegistry(registry.registry);
const expectValidationError = async (
actionId: string,
message: { exact: string } | { includes: string },
) => {
const response = await callValidationAction(actionId);
const error = requireHookError(response);
expect(error.code).toBe("INVALID_REQUEST");
if ("exact" in message) {
expect(error.message).toBe(message.exact);
} else {
expect(String(error.message)).toContain(message.includes);
}
};
await expectValidationError("bad-result", {
exact: "plugin session action result must be JSON-compatible",
});
await expectValidationError("bad-reply", {
exact: "plugin session action reply must be JSON-compatible",
});
const primitiveResult = await callValidationAction("primitive-result");
const primitiveResultError = requireHookError(primitiveResult);
expect(primitiveResultError.code).toBe("INVALID_REQUEST");
expect(primitiveResultError.message).toBe("plugin session action result must be an object");
await expect(callValidationAction("typed-error")).resolves.toEqual({
ok: true,
payload: {
ok: false,
error: "needs operator input",
code: "needs_input",
details: {
field: "version",
},
},
error: undefined,
});
await expectValidationError("bad-ok", { includes: "/ok: must be boolean" });
await expectValidationError("error-shaped-success", {
includes: "unexpected property 'error'",
});
await expectValidationError("bad-error-details", {
exact: "plugin session action details must be JSON-compatible",
});
await expectValidationError("bad-continue-agent", {
includes: "/continueAgent: must be boolean",
});
await expectValidationError("mixed-branch-fields", {
includes: "unexpected property 'continueAgent'",
});
await expectValidationError("unknown-success-field", {
includes: "unexpected property 'extra'",
});
const throwsSecret = await callValidationAction("throws-secret");
const throwsSecretError = requireHookError(throwsSecret);
expect(throwsSecretError.code).toBe("UNAVAILABLE");
expect(throwsSecretError.message).toBe("plugin session action failed");
});
describe("authorizes session actions through the gateway by action-declared scopes", () => {
const pluginId = "scope-action-fixture";
const actionScopes = {
approve: [APPROVALS_SCOPE],
view: [READ_SCOPE],
talk: [TALK_SCOPE],
"talk-approve": [TALK_SCOPE, APPROVALS_SCOPE],
"talk-secrets": [TALK_SECRETS_SCOPE],
admin: [ADMIN_SCOPE],
pairing: [PAIRING_SCOPE],
questions: [QUESTIONS_SCOPE],
"default-write": undefined,
} satisfies Record<string, OperatorScope[] | undefined>;
const handler = vi.fn(({ sessionKey }: PluginSessionActionContext) => ({
result: { accepted: true, ...(sessionKey ? { sessionKey } : {}) },
continueAgent: true,
}));
beforeEach(() => {
handler.mockClear();
const { registry } = registerActionFixture({
id: pluginId,
register(api) {
for (const [id, requiredScopes] of Object.entries(actionScopes)) {
api.registerSessionAction({ id, requiredScopes, handler });
}
},
});
setActivePluginRegistry(registry.registry);
});
it.each<
[
name: string,
actionId: keyof typeof actionScopes,
scopes: OperatorScope[],
missingScope?: OperatorScope,
]
>([
["approvals admits a custom approval action", "approve", [APPROVALS_SCOPE]],
["read cannot approve", "approve", [READ_SCOPE], APPROVALS_SCOPE],
["write cannot approve", "approve", [WRITE_SCOPE], APPROVALS_SCOPE],
["read admits a read action", "view", [READ_SCOPE]],
["write satisfies read", "view", [WRITE_SCOPE]],
["talk admits a talk action", "talk", [TALK_SCOPE]],
["write satisfies talk", "talk", [WRITE_SCOPE]],
["admin admits a talk action", "talk", [ADMIN_SCOPE]],
["read cannot talk", "talk", [READ_SCOPE], TALK_SCOPE],
["no scopes cannot talk", "talk", [], TALK_SCOPE],
[
"write reports missing approvals after satisfying talk",
"talk-approve",
[WRITE_SCOPE],
APPROVALS_SCOPE,
],
[
"write and approvals satisfy talk and approvals",
"talk-approve",
[WRITE_SCOPE, APPROVALS_SCOPE],
],
[
"talk and approvals satisfy both requirements",
"talk-approve",
[TALK_SCOPE, APPROVALS_SCOPE],
],
["admin satisfies both requirements", "talk-approve", [ADMIN_SCOPE]],
["write cannot read talk secrets", "talk-secrets", [WRITE_SCOPE], TALK_SECRETS_SCOPE],
["write cannot administer", "admin", [WRITE_SCOPE], ADMIN_SCOPE],
["write cannot pair", "pairing", [WRITE_SCOPE], PAIRING_SCOPE],
["write cannot answer questions", "questions", [WRITE_SCOPE], QUESTIONS_SCOPE],
["write admits an action with default scopes", "default-write", [WRITE_SCOPE]],
["talk cannot perform a default-write action", "default-write", [TALK_SCOPE], WRITE_SCOPE],
["read cannot perform a default-write action", "default-write", [READ_SCOPE], WRITE_SCOPE],
["no scopes cannot perform a default-write action", "default-write", [], WRITE_SCOPE],
["admin admits an action with default scopes", "default-write", [ADMIN_SCOPE]],
])("%s", async (_name, actionId, scopes, missingScope) => {
const response = await callRegisteredSessionActionThroughGatewayForTest({
pluginId,
actionId,
scopes,
});
const expectedResponse: HookResponse = missingScope
? {
ok: false,
payload: undefined,
error: {
code: "FORBIDDEN",
message: `missing scope: ${missingScope}`,
details: {
code: "MISSING_SCOPE",
missingScope,
requiredScopes: actionScopes[actionId] ?? [WRITE_SCOPE],
},
},
}
: {
ok: true,
payload: { ok: true, result: { accepted: true }, continueAgent: true },
error: undefined,
};
expect({ response, handlerCalls: handler.mock.calls.length }).toEqual({
response: expectedResponse,
handlerCalls: missingScope ? 0 : 1,
});
if (!missingScope) {
expect(handler).toHaveBeenCalledWith({
pluginId,
actionId,
client: { connId: "test-client", scopes },
});
}
});
it("normalizes session keys and rejects invalid params for approval-only callers", async () => {
await expect(
callRegisteredSessionActionThroughGatewayForTest({
pluginId,
actionId: "approve",
extra: { sessionKey: ` ${MAIN_SESSION_KEY} ` },
scopes: [APPROVALS_SCOPE],
}),
).resolves.toEqual({
ok: true,
payload: {
ok: true,
result: { accepted: true, sessionKey: MAIN_SESSION_KEY },
continueAgent: true,
},
error: undefined,
});
expect(handler).toHaveBeenCalledWith({
pluginId,
actionId: "approve",
sessionKey: MAIN_SESSION_KEY,
agentId: "main",
client: { connId: "test-client", scopes: [APPROVALS_SCOPE] },
});
const blankPluginId = await callPluginSessionActionThroughGatewayForTest({
body: { pluginId: " ", actionId: "approve" },
scopes: [APPROVALS_SCOPE],
});
expect(requireHookError(blankPluginId)).toEqual({
code: "INVALID_REQUEST",
message: "plugins.sessionAction pluginId and actionId must be non-empty",
});
expect(handler).toHaveBeenCalledTimes(1);
});
});
it("passes a defensive copy of client scopes to session action handlers", async () => {
const registry = createEmptyPluginRegistry();
let response: { ok: boolean; payload?: unknown; error?: unknown } | undefined;
let handlerScopes: string[] | undefined;
const originalScopes = [READ_SCOPE];
registry.sessionActions = [
{
pluginId: "scope-copy-fixture",
pluginName: "Scope Copy Fixture",
source: "test",
action: {
id: "mutate",
requiredScopes: [READ_SCOPE],
handler: ({ client }) => {
handlerScopes = client?.scopes;
client?.scopes.push(APPROVALS_SCOPE);
return { result: { ok: true } };
},
},
},
];
registry.plugins = [createPluginRecord({ id: "scope-copy-fixture" })];
setActivePluginRegistry(registry);
await expectDefined(
pluginHostHookHandlers["plugins.sessionAction"],
'pluginHostHookHandlers["plugins.sessionAction"] test invariant',
)({
req: {
id: "scope-copy",
type: "req",
method: "plugins.sessionAction",
params: { pluginId: "scope-copy-fixture", actionId: "mutate" },
},
params: { pluginId: "scope-copy-fixture", actionId: "mutate" },
client: {
connId: "scope-copy-client",
connect: { scopes: originalScopes },
} as GatewayClient,
isWebchatConnect: () => false,
respond: (ok, payload, error) => {
response = { ok, payload, error };
},
context: { getRuntimeConfig: () => ({}) } as never,
});
expect(response).toEqual({
ok: true,
payload: { ok: true, result: { ok: true } },
error: undefined,
});
expect(handlerScopes).toEqual([READ_SCOPE, APPROVALS_SCOPE]);
expect(handlerScopes).not.toBe(originalScopes);
expect(originalScopes).toEqual([READ_SCOPE]);
});
it("does not dispatch session actions for plugins that are not loaded", async () => {
const handler = vi.fn(() => ({ result: { stale: true } }));
const registry = createEmptyPluginRegistry();
registry.sessionActions = [
{
pluginId: "failed-action-plugin",
pluginName: "Failed Action Plugin",
source: "test",
action: {
id: "stale",
requiredScopes: [READ_SCOPE],
handler,
},
},
];
registry.plugins = [
createPluginRecord({
id: "failed-action-plugin",
name: "Failed Action Plugin",
status: "error",
}),
];
setActivePluginRegistry(registry);
const staleAction = await callPluginSessionActionThroughGatewayForTest({
body: {
pluginId: "failed-action-plugin",
actionId: "stale",
},
scopes: [READ_SCOPE],
});
const staleActionError = requireHookError(staleAction);
expect(staleActionError.code).toBe("UNAVAILABLE");
expect(staleActionError.message).toBe(
"unknown plugin session action: failed-action-plugin/stale",
);
expect(handler).not.toHaveBeenCalled();
});
it("emits plugin-attributed agent events through the plugin API", () => {
const observed: unknown[] = [];
const unsubscribe = onAgentEvent((event) => observed.push(event));
const { config, registry } = createPluginRegistryFixture();
let bundledApi: OpenClawPluginApi | undefined;
let workspaceApi: OpenClawPluginApi | undefined;
registerTestPlugin({
registry,
config,
record: createPluginRecord({
id: "event-plugin",
name: "Event Plugin",
origin: "bundled",
}),
register(api) {
bundledApi = api;
},
});
registerTestPlugin({
registry,
config,
record: createPluginRecord({
id: "workspace-event-plugin",
name: "Workspace Event Plugin",
origin: "workspace",
}),
register(api) {
workspaceApi = api;
},
});
setActivePluginRegistry(registry.registry);
try {
expect(
bundledApi?.agent?.events.emitAgentEvent({
runId: "run-emit",
sessionKey: " agent:main:main ",
stream: "approval",
data: { state: "queued" },
}),
).toEqual({ emitted: true, stream: "approval" });
expect(
bundledApi?.agent?.events.emitAgentEvent({
runId: "run-emit",
stream: "lifecycle",
data: { phase: "start" },
}),
).toEqual({
emitted: false,
reason: "lifecycle start requires a finite startedAt timestamp",
});
expect(
bundledApi?.agent?.events.emitAgentEvent({
runId: "run-emit",
stream: "lifecycle",
data: { phase: "start", startedAt: 1_234 },
}),
).toEqual({ emitted: true, stream: "lifecycle" });
expect(
workspaceApi?.emitAgentEvent({
runId: "run-emit",
stream: "lifecycle",
data: { phase: "end" },
}),
).toEqual({ emitted: false, reason: "stream lifecycle is reserved for bundled plugins" });
expect(
workspaceApi?.emitAgentEvent({
runId: "run-emit",
stream: "assistant",
data: { text: "spoofed assistant output" },
}),
).toEqual({ emitted: false, reason: "stream assistant is reserved for bundled plugins" });
expect(
workspaceApi?.emitAgentEvent({
runId: "run-emit",
stream: "other-plugin.workflow",
data: { state: "queued" },
}),
).toEqual({
emitted: false,
reason: "stream other-plugin.workflow must be scoped to plugin workspace-event-plugin",
});
expect(
workspaceApi?.emitAgentEvent({
runId: "run-emit",
stream: "workspace-event-plugin.workflow",
data: { state: "queued" },
}),
).toEqual({ emitted: true, stream: "workspace-event-plugin.workflow" });
expect(
bundledApi?.emitAgentEvent({
runId: "run-emit",
stream: "approval",
data: 1n as never,
}),
).toEqual({ emitted: false, reason: "event data must be JSON-compatible" });
} finally {
unsubscribe();
}
expect(observed).toHaveLength(3);
const bundledEvent = requireObservedEvent(observed, 0);
expect(bundledEvent.runId).toBe("run-emit");
expect(bundledEvent.sessionKey).toBe("agent:main:main");
expect(bundledEvent.stream).toBe("approval");
expect(bundledEvent.data).toEqual({
state: "queued",
pluginId: "event-plugin",
pluginName: "Event Plugin",
});
const lifecycleEvent = requireObservedEvent(observed, 1);
expect(lifecycleEvent.stream).toBe("lifecycle");
expect(lifecycleEvent.data).toEqual({
phase: "start",
startedAt: 1_234,
pluginId: "event-plugin",
pluginName: "Event Plugin",
});
const workspaceEvent = requireObservedEvent(observed, 2);
expect(workspaceEvent.runId).toBe("run-emit");
expect(workspaceEvent.sessionKey).toBeUndefined();
expect(workspaceEvent.stream).toBe("workspace-event-plugin.workflow");
expect(workspaceEvent.data).toEqual({
state: "queued",
pluginId: "workspace-event-plugin",
pluginName: "Workspace Event Plugin",
});
});
it("blocks agent events from stale and non-activating plugin API closures", () => {
const observed: unknown[] = [];
const unsubscribe = onAgentEvent((event) => observed.push(event));
const { config, registry } = createPluginRegistryFixture();
let capturedApi: OpenClawPluginApi | undefined;
registerTestPlugin({
registry,
config,
record: createPluginRecord({
id: "stale-event-plugin",
name: "Stale Event Plugin",
origin: "bundled",
}),
register(api) {
capturedApi = api;
},
});
setActivePluginRegistry(registry.registry);
setActivePluginRegistry(createEmptyPluginRegistry());
try {
expect(
capturedApi?.emitAgentEvent({
runId: "stale-run",
stream: "approval",
data: { stale: true },
}),
).toEqual({ emitted: false, reason: "plugin is not loaded" });
const neverActiveRegistry = createPluginRegistry({
logger: {
info() {},
warn() {},
error() {},
debug() {},
},
runtime: {} as never,
});
let neverActiveApi: OpenClawPluginApi | undefined;
registerTestPlugin({
registry: neverActiveRegistry,
config,
record: createPluginRecord({
id: "never-active-event-plugin",
name: "Never Active Event Plugin",
origin: "bundled",
}),
register(api) {
neverActiveApi = api;
},
});
expect(
neverActiveApi?.emitAgentEvent({
runId: "never-active-run",
stream: "approval",
data: { inactive: true },
}),
).toEqual({ emitted: false, reason: "plugin is not loaded" });
const inactiveRegistry = createPluginRegistry({
logger: {
info() {},
warn() {},
error() {},
debug() {},
},
runtime: {} as never,
activateGlobalSideEffects: false,
});
let inactiveApi: OpenClawPluginApi | undefined;
registerTestPlugin({
registry: inactiveRegistry,
config,
record: createPluginRecord({
id: "inactive-event-plugin",
name: "Inactive Event Plugin",
origin: "bundled",
}),
register(api) {
inactiveApi = api;
},
});
expect(
inactiveApi?.emitAgentEvent({
runId: "inactive-run",
stream: "approval",
data: { inactive: true },
}),
).toEqual({ emitted: false, reason: "global side effects disabled" });
} finally {
unsubscribe();
}
expect(observed).toEqual([]);
});
it("keeps agent event delivery after a staged registry rollback", () => {
const observed: unknown[] = [];
const unsubscribe = onAgentEvent((event) => observed.push(event));
const { config, registry } = createPluginRegistryFixture();
let capturedApi: OpenClawPluginApi | undefined;
registerTestPlugin({
registry,
config,
record: createPluginRecord({
id: "reactivated-event-plugin",
name: "Reactivated Event Plugin",
origin: "bundled",
}),
register(api) {
capturedApi = api;
},
});
setActivePluginRegistry(registry.registry);
const snapshot = captureActivePluginRegistrySnapshot();
stageActivePluginRegistry(createEmptyPluginRegistry(), null, "default");
rollbackStagedPluginRegistry(snapshot);
try {
expect(
capturedApi?.emitAgentEvent({
runId: "reactivated-run",
stream: "approval",
data: { active: true },
}),
).toEqual({ emitted: true, stream: "approval" });
} finally {
unsubscribe();
}
expect(observed).toHaveLength(1);
const reactivatedEvent = requireObservedEvent(observed, 0);
expect(reactivatedEvent.runId).toBe("reactivated-run");
expect(reactivatedEvent.sessionKey).toBeUndefined();
expect(reactivatedEvent.stream).toBe("approval");
expect(reactivatedEvent.data).toEqual({
active: true,
pluginId: "reactivated-event-plugin",
pluginName: "Reactivated Event Plugin",
});
});
});
|