Skip to content

Commit 0aaf753

Browse files
authored
matrix: add exec approval reaction shortcuts (#60931)
Merged via squash. Prepared head SHA: a34e824 Co-authored-by: gumadeiras <[email protected]> Co-authored-by: gumadeiras <[email protected]> Reviewed-by: @gumadeiras
1 parent 934641d commit 0aaf753

14 files changed

Lines changed: 1252 additions & 28 deletions

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ Docs: https://docs.openclaw.ai
176176
- Windows/restart: fall back to the installed Startup-entry launcher when the scheduled task was never registered, so `/restart` can relaunch the gateway on Windows setups where `schtasks` install fell back during onboarding. (#58943) Thanks @imechZhangLY.
177177
- Agents/Claude CLI: persist routed Claude session bindings, rotate them on `/new` and `/reset`, and keep live Claude CLI model switches moving across the configured Claude family so resumed sessions follow the real active thread and model. Thanks @vincentkoc.
178178
- Providers/Anthropic: when Claude CLI auth becomes the default, write a real `claude-cli` auth profile so local and gateway agent runs can use Claude CLI immediately without missing-API-key failures. Thanks @vincentkoc.
179+
- Matrix/exec approvals: anchor seeded approval reactions to the primary Matrix prompt event, resolve them from event metadata instead of prompt text, and clean up chunked approval prompts correctly. (#60931) thanks @gumadeiras.
179180

180181
## 2026.4.2
181182

‎docs/channels/matrix.md‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,13 @@ Delivery rules:
679679
- `target: "channel"` sends the prompt back to the originating Matrix room or DM
680680
- `target: "both"` sends to approver DMs and the originating Matrix room or DM
681681

682-
Matrix uses text approval prompts today. Approvers resolve them with `/approve <id> allow-once`, `/approve <id> allow-always`, or `/approve <id> deny`.
682+
Matrix approval prompts seed reaction shortcuts on the primary approval message:
683+
684+
- `✅` = allow once
685+
- `❌` = deny
686+
- `♾️` = allow always when that decision is allowed by the effective exec policy
687+
688+
Approvers can react on that message or use the fallback slash commands: `/approve <id> allow-once`, `/approve <id> allow-always`, or `/approve <id> deny`.
683689

684690
Only resolved approvers can approve or deny. Channel delivery includes the command text, so only enable `channel` or `both` in trusted rooms.
685691

‎docs/tools/exec-approvals.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ host policy sources, and the effective result.
2424
If the companion app UI is **not available**, any request that requires a prompt is
2525
resolved by the **ask fallback** (default: deny).
2626

27+
Native chat approval clients can also expose channel-specific affordances on the
28+
pending approval message. For example, Matrix can seed reaction shortcuts on the
29+
approval prompt (`✅` allow once, `❌` deny, and `♾️` allow always when available)
30+
while still leaving the `/approve ...` commands in the message as a fallback.
31+
2732
## Where it applies
2833

2934
Exec approvals are enforced locally on the execution host:
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import { afterEach, describe, expect, it } from "vitest";
2+
import {
3+
buildMatrixApprovalReactionHint,
4+
clearMatrixApprovalReactionTargetsForTest,
5+
listMatrixApprovalReactionBindings,
6+
registerMatrixApprovalReactionTarget,
7+
resolveMatrixApprovalReactionTarget,
8+
unregisterMatrixApprovalReactionTarget,
9+
} from "./approval-reactions.js";
10+
11+
afterEach(() => {
12+
clearMatrixApprovalReactionTargetsForTest();
13+
});
14+
15+
describe("matrix approval reactions", () => {
16+
it("lists reactions in stable decision order", () => {
17+
expect(listMatrixApprovalReactionBindings(["allow-once", "deny", "allow-always"])).toEqual([
18+
{ decision: "allow-once", emoji: "✅", label: "Allow once" },
19+
{ decision: "allow-always", emoji: "♾️", label: "Allow always" },
20+
{ decision: "deny", emoji: "❌", label: "Deny" },
21+
]);
22+
});
23+
24+
it("builds a compact reaction hint", () => {
25+
expect(buildMatrixApprovalReactionHint(["allow-once", "deny"])).toBe(
26+
"React here: ✅ Allow once, ❌ Deny",
27+
);
28+
});
29+
30+
it("resolves a registered approval anchor event back to an approval decision", () => {
31+
registerMatrixApprovalReactionTarget({
32+
roomId: "!ops:example.org",
33+
eventId: "$approval-msg",
34+
approvalId: "req-123",
35+
allowedDecisions: ["allow-once", "allow-always", "deny"],
36+
});
37+
38+
expect(
39+
resolveMatrixApprovalReactionTarget({
40+
roomId: "!ops:example.org",
41+
eventId: "$approval-msg",
42+
reactionKey: "✅",
43+
}),
44+
).toEqual({
45+
approvalId: "req-123",
46+
decision: "allow-once",
47+
});
48+
expect(
49+
resolveMatrixApprovalReactionTarget({
50+
roomId: "!ops:example.org",
51+
eventId: "$approval-msg",
52+
reactionKey: "♾️",
53+
}),
54+
).toEqual({
55+
approvalId: "req-123",
56+
decision: "allow-always",
57+
});
58+
expect(
59+
resolveMatrixApprovalReactionTarget({
60+
roomId: "!ops:example.org",
61+
eventId: "$approval-msg",
62+
reactionKey: "❌",
63+
}),
64+
).toEqual({
65+
approvalId: "req-123",
66+
decision: "deny",
67+
});
68+
});
69+
70+
it("ignores reactions that are not allowed on the registered approval anchor event", () => {
71+
registerMatrixApprovalReactionTarget({
72+
roomId: "!ops:example.org",
73+
eventId: "$approval-msg",
74+
approvalId: "req-123",
75+
allowedDecisions: ["allow-once", "deny"],
76+
});
77+
78+
expect(
79+
resolveMatrixApprovalReactionTarget({
80+
roomId: "!ops:example.org",
81+
eventId: "$approval-msg",
82+
reactionKey: "♾️",
83+
}),
84+
).toBeNull();
85+
});
86+
87+
it("stops resolving reactions after the approval anchor event is unregistered", () => {
88+
registerMatrixApprovalReactionTarget({
89+
roomId: "!ops:example.org",
90+
eventId: "$approval-msg",
91+
approvalId: "req-123",
92+
allowedDecisions: ["allow-once", "allow-always", "deny"],
93+
});
94+
unregisterMatrixApprovalReactionTarget({
95+
roomId: "!ops:example.org",
96+
eventId: "$approval-msg",
97+
});
98+
99+
expect(
100+
resolveMatrixApprovalReactionTarget({
101+
roomId: "!ops:example.org",
102+
eventId: "$approval-msg",
103+
reactionKey: "✅",
104+
}),
105+
).toBeNull();
106+
});
107+
});
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
import type { ExecApprovalReplyDecision } from "openclaw/plugin-sdk/approval-runtime";
2+
3+
const MATRIX_APPROVAL_REACTION_META = {
4+
"allow-once": {
5+
emoji: "✅",
6+
label: "Allow once",
7+
},
8+
"allow-always": {
9+
emoji: "♾️",
10+
label: "Allow always",
11+
},
12+
deny: {
13+
emoji: "❌",
14+
label: "Deny",
15+
},
16+
} satisfies Record<ExecApprovalReplyDecision, { emoji: string; label: string }>;
17+
18+
const MATRIX_APPROVAL_REACTION_ORDER = [
19+
"allow-once",
20+
"allow-always",
21+
"deny",
22+
] as const satisfies readonly ExecApprovalReplyDecision[];
23+
24+
export type MatrixApprovalReactionBinding = {
25+
decision: ExecApprovalReplyDecision;
26+
emoji: string;
27+
label: string;
28+
};
29+
30+
export type MatrixApprovalReactionResolution = {
31+
approvalId: string;
32+
decision: ExecApprovalReplyDecision;
33+
};
34+
35+
type MatrixApprovalReactionTarget = {
36+
approvalId: string;
37+
allowedDecisions: readonly ExecApprovalReplyDecision[];
38+
};
39+
40+
const matrixApprovalReactionTargets = new Map<string, MatrixApprovalReactionTarget>();
41+
42+
function buildReactionTargetKey(roomId: string, eventId: string): string | null {
43+
const normalizedRoomId = roomId.trim();
44+
const normalizedEventId = eventId.trim();
45+
if (!normalizedRoomId || !normalizedEventId) {
46+
return null;
47+
}
48+
return `${normalizedRoomId}:${normalizedEventId}`;
49+
}
50+
51+
export function listMatrixApprovalReactionBindings(
52+
allowedDecisions: readonly ExecApprovalReplyDecision[],
53+
): MatrixApprovalReactionBinding[] {
54+
const allowed = new Set(allowedDecisions);
55+
return MATRIX_APPROVAL_REACTION_ORDER.filter((decision) => allowed.has(decision)).map(
56+
(decision) => ({
57+
decision,
58+
emoji: MATRIX_APPROVAL_REACTION_META[decision].emoji,
59+
label: MATRIX_APPROVAL_REACTION_META[decision].label,
60+
}),
61+
);
62+
}
63+
64+
export function buildMatrixApprovalReactionHint(
65+
allowedDecisions: readonly ExecApprovalReplyDecision[],
66+
): string | null {
67+
const bindings = listMatrixApprovalReactionBindings(allowedDecisions);
68+
if (bindings.length === 0) {
69+
return null;
70+
}
71+
return `React here: ${bindings.map((binding) => `${binding.emoji} ${binding.label}`).join(", ")}`;
72+
}
73+
74+
export function resolveMatrixApprovalReactionDecision(
75+
reactionKey: string,
76+
allowedDecisions: readonly ExecApprovalReplyDecision[],
77+
): ExecApprovalReplyDecision | null {
78+
const normalizedReaction = reactionKey.trim();
79+
if (!normalizedReaction) {
80+
return null;
81+
}
82+
const allowed = new Set(allowedDecisions);
83+
for (const decision of MATRIX_APPROVAL_REACTION_ORDER) {
84+
if (!allowed.has(decision)) {
85+
continue;
86+
}
87+
if (MATRIX_APPROVAL_REACTION_META[decision].emoji === normalizedReaction) {
88+
return decision;
89+
}
90+
}
91+
return null;
92+
}
93+
94+
export function registerMatrixApprovalReactionTarget(params: {
95+
roomId: string;
96+
eventId: string;
97+
approvalId: string;
98+
allowedDecisions: readonly ExecApprovalReplyDecision[];
99+
}): void {
100+
const key = buildReactionTargetKey(params.roomId, params.eventId);
101+
const approvalId = params.approvalId.trim();
102+
const allowedDecisions = Array.from(
103+
new Set(
104+
params.allowedDecisions.filter(
105+
(decision): decision is ExecApprovalReplyDecision =>
106+
decision === "allow-once" || decision === "allow-always" || decision === "deny",
107+
),
108+
),
109+
);
110+
if (!key || !approvalId || allowedDecisions.length === 0) {
111+
return;
112+
}
113+
matrixApprovalReactionTargets.set(key, {
114+
approvalId,
115+
allowedDecisions,
116+
});
117+
}
118+
119+
export function unregisterMatrixApprovalReactionTarget(params: {
120+
roomId: string;
121+
eventId: string;
122+
}): void {
123+
const key = buildReactionTargetKey(params.roomId, params.eventId);
124+
if (!key) {
125+
return;
126+
}
127+
matrixApprovalReactionTargets.delete(key);
128+
}
129+
130+
export function resolveMatrixApprovalReactionTarget(params: {
131+
roomId: string;
132+
eventId: string;
133+
reactionKey: string;
134+
}): MatrixApprovalReactionResolution | null {
135+
const key = buildReactionTargetKey(params.roomId, params.eventId);
136+
if (!key) {
137+
return null;
138+
}
139+
const target = matrixApprovalReactionTargets.get(key);
140+
if (!target) {
141+
return null;
142+
}
143+
const decision = resolveMatrixApprovalReactionDecision(
144+
params.reactionKey,
145+
target.allowedDecisions,
146+
);
147+
if (!decision) {
148+
return null;
149+
}
150+
return {
151+
approvalId: target.approvalId,
152+
decision,
153+
};
154+
}
155+
156+
export function clearMatrixApprovalReactionTargetsForTest(): void {
157+
matrixApprovalReactionTargets.clear();
158+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { beforeEach, describe, expect, it, vi } from "vitest";
2+
3+
const gatewayRuntimeHoisted = vi.hoisted(() => ({
4+
requestSpy: vi.fn(),
5+
startSpy: vi.fn(),
6+
stopSpy: vi.fn(),
7+
stopAndWaitSpy: vi.fn(async () => undefined),
8+
createClientSpy: vi.fn(),
9+
}));
10+
11+
vi.mock("openclaw/plugin-sdk/gateway-runtime", () => ({
12+
createOperatorApprovalsGatewayClient: gatewayRuntimeHoisted.createClientSpy,
13+
}));
14+
15+
describe("resolveMatrixExecApproval", () => {
16+
beforeEach(() => {
17+
gatewayRuntimeHoisted.requestSpy.mockReset();
18+
gatewayRuntimeHoisted.startSpy.mockReset();
19+
gatewayRuntimeHoisted.stopSpy.mockReset();
20+
gatewayRuntimeHoisted.stopAndWaitSpy.mockReset().mockResolvedValue(undefined);
21+
gatewayRuntimeHoisted.createClientSpy.mockReset().mockImplementation((opts) => ({
22+
start: () => {
23+
gatewayRuntimeHoisted.startSpy();
24+
opts.onHelloOk?.();
25+
},
26+
request: gatewayRuntimeHoisted.requestSpy,
27+
stop: gatewayRuntimeHoisted.stopSpy,
28+
stopAndWait: gatewayRuntimeHoisted.stopAndWaitSpy,
29+
}));
30+
});
31+
32+
it("submits exec approval resolutions through the gateway approvals client", async () => {
33+
const { resolveMatrixExecApproval } = await import("./exec-approval-resolver.js");
34+
35+
await resolveMatrixExecApproval({
36+
cfg: {} as never,
37+
approvalId: "req-123",
38+
decision: "allow-once",
39+
senderId: "@owner:example.org",
40+
});
41+
42+
expect(gatewayRuntimeHoisted.requestSpy).toHaveBeenCalledWith("exec.approval.resolve", {
43+
id: "req-123",
44+
decision: "allow-once",
45+
});
46+
});
47+
48+
it("recognizes structured approval-not-found errors", async () => {
49+
const { isApprovalNotFoundError } = await import("./exec-approval-resolver.js");
50+
const err = new Error("approval not found");
51+
(err as Error & { gatewayCode?: string; details?: { reason?: string } }).gatewayCode =
52+
"INVALID_REQUEST";
53+
(err as Error & { gatewayCode?: string; details?: { reason?: string } }).details = {
54+
reason: "APPROVAL_NOT_FOUND",
55+
};
56+
57+
expect(isApprovalNotFoundError(err)).toBe(true);
58+
});
59+
});

0 commit comments

Comments
 (0)