Skip to content

Commit 56a5d7e

Browse files
authored
fix(codex): defer report-mode plugin approvals
Route Codex app-server report-mode PreToolUse plugin approval requirements through the matching app-server approval request instead of failing closed. Shares duplicate in-flight approvals, preserves block/rewrite fail-closed behavior, and keeps generic plugin allow-always scoped to one Codex request. Supersedes #86978; thanks @clawSean for the original docs clarification.
1 parent 44dc29f commit 56a5d7e

10 files changed

Lines changed: 511 additions & 21 deletions
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
7039b60f2cea732a90db633328952faaddd919f0d098b303b29d554e64184073 plugin-sdk-api-baseline.json
2-
1a78f4df81562af070c5379c6369a8bea9c704f985b5382a463364757b26db0d plugin-sdk-api-baseline.jsonl
1+
91cb45dc1e8aaa3dac9a2c1d3c98c8ff22112e41c305de17f30d0d4420635ee4 plugin-sdk-api-baseline.json
2+
3aa4802ffcb68c4f15e367030994eae10e73b55b5f14c8e23d4e9467fae325fe plugin-sdk-api-baseline.jsonl

‎docs/plugins/codex-harness-runtime.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,16 @@ OpenClaw can mirror selected events, but it cannot rewrite the native Codex
9999
thread unless Codex exposes that operation through app-server or native hook
100100
callbacks.
101101

102+
Codex app-server report-mode `PreToolUse` events defer plugin approval requests
103+
to the matching app-server approval. If an OpenClaw `before_tool_call` hook
104+
returns `requireApproval` while the native payload sets report approval mode
105+
(`openclaw_approval_mode` is `"report"`), the native hook relay records the
106+
plugin approval requirement and returns no native decision. When Codex sends the
107+
app-server approval request for the same tool use, OpenClaw opens the plugin
108+
approval prompt and maps the decision back to Codex. Codex `PermissionRequest`
109+
events are a separate approval path and can still route through OpenClaw
110+
approvals when the runtime is configured for that bridge.
111+
102112
Codex app-server item notifications also provide async `after_tool_call`
103113
observations for native tool completions that are not already covered by the
104114
native `PostToolUse` relay. These observations are for telemetry and plugin

‎docs/plugins/hooks.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,8 @@ Hook guard behavior for typed lifecycle hooks:
211211
- `params` rewrites the tool parameters for execution.
212212
- `requireApproval` pauses the agent run and asks the user through plugin
213213
approvals. The `/approve` command can approve both exec and plugin approvals.
214+
In Codex app-server report-mode native `PreToolUse` relays, this is deferred
215+
to the matching app-server approval request; see [Codex harness runtime](/plugins/codex-harness-runtime#hook-boundaries).
214216
- A lower-priority `block: true` can still block after a higher-priority hook
215217
requested approval.
216218
- `onResolution` receives the resolved approval decision - `allow-once`,

‎extensions/codex/src/app-server/approval-bridge.test.ts‎

Lines changed: 92 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
callGatewayTool,
33
hasNativeHookRelayInvocation,
44
invokeNativeHookRelay,
5+
resolveNativeHookRelayDeferredToolApproval,
56
runBeforeToolCallHook,
67
type EmbeddedRunAttemptParams,
78
} from "openclaw/plugin-sdk/agent-harness-runtime";
@@ -13,6 +14,7 @@ vi.mock("openclaw/plugin-sdk/agent-harness-runtime", async (importOriginal) => (
1314
callGatewayTool: vi.fn(),
1415
hasNativeHookRelayInvocation: vi.fn(() => false),
1516
invokeNativeHookRelay: vi.fn(),
17+
resolveNativeHookRelayDeferredToolApproval: vi.fn(),
1618
runBeforeToolCallHook: vi.fn(async ({ params }: { params: unknown }) => ({
1719
blocked: false,
1820
params,
@@ -22,6 +24,9 @@ vi.mock("openclaw/plugin-sdk/agent-harness-runtime", async (importOriginal) => (
2224
const mockCallGatewayTool = vi.mocked(callGatewayTool);
2325
const mockHasNativeHookRelayInvocation = vi.mocked(hasNativeHookRelayInvocation);
2426
const mockInvokeNativeHookRelay = vi.mocked(invokeNativeHookRelay);
27+
const mockResolveNativeHookRelayDeferredToolApproval = vi.mocked(
28+
resolveNativeHookRelayDeferredToolApproval,
29+
);
2530
const mockRunBeforeToolCallHook = vi.mocked(runBeforeToolCallHook);
2631

2732
function requireRecord(value: unknown, label: string): Record<string, unknown> {
@@ -103,6 +108,8 @@ describe("Codex app-server approval bridge", () => {
103108
mockHasNativeHookRelayInvocation.mockReset();
104109
mockHasNativeHookRelayInvocation.mockReturnValue(false);
105110
mockInvokeNativeHookRelay.mockReset();
111+
mockResolveNativeHookRelayDeferredToolApproval.mockReset();
112+
mockResolveNativeHookRelayDeferredToolApproval.mockResolvedValue(undefined);
106113
mockRunBeforeToolCallHook.mockReset();
107114
mockRunBeforeToolCallHook.mockImplementation(async ({ params }) => ({
108115
blocked: false,
@@ -132,7 +139,7 @@ describe("Codex app-server approval bridge", () => {
132139
expect(mockRunBeforeToolCallHook).toHaveBeenCalledWith(
133140
expect.objectContaining({
134141
toolName: "exec",
135-
approvalMode: "report",
142+
approvalMode: "request",
136143
}),
137144
);
138145
findApprovalEvent(params, {
@@ -212,7 +219,7 @@ describe("Codex app-server approval bridge", () => {
212219
},
213220
},
214221
toolCallId: "cmd-1",
215-
approvalMode: "report",
222+
approvalMode: "request",
216223
signal: undefined,
217224
ctx: {
218225
agentId: "main",
@@ -385,6 +392,11 @@ describe("Codex app-server approval bridge", () => {
385392
expect(result).toEqual({ decision: "accept" });
386393
expect(mockRunBeforeToolCallHook).not.toHaveBeenCalled();
387394
expect(mockInvokeNativeHookRelay).toHaveBeenCalledTimes(1);
395+
expect(mockResolveNativeHookRelayDeferredToolApproval).toHaveBeenCalledWith({
396+
relayId: "relay-1",
397+
toolUseId: "cmd-native-relay-noop",
398+
signal: undefined,
399+
});
388400
expect(mockCallGatewayTool.mock.calls.map(([method]) => method)).toEqual([
389401
"plugin.approval.request",
390402
"plugin.approval.waitDecision",
@@ -432,12 +444,53 @@ describe("Codex app-server approval bridge", () => {
432444
event: "pre_tool_use",
433445
toolUseId: "cmd-native-relay-observed",
434446
});
447+
expect(mockResolveNativeHookRelayDeferredToolApproval).toHaveBeenCalledWith({
448+
relayId: "relay-1",
449+
toolUseId: "cmd-native-relay-observed",
450+
signal: undefined,
451+
});
435452
expect(mockCallGatewayTool.mock.calls.map(([method]) => method)).toEqual([
436453
"plugin.approval.request",
437454
"plugin.approval.waitDecision",
438455
]);
439456
});
440457

458+
it("accepts command approvals from deferred native PreToolUse plugin approvals", async () => {
459+
const params = createParams();
460+
mockHasNativeHookRelayInvocation.mockReturnValueOnce(true);
461+
mockResolveNativeHookRelayDeferredToolApproval.mockResolvedValueOnce({
462+
handled: true,
463+
outcome: "approved-once",
464+
});
465+
466+
const result = await handleCodexAppServerApprovalRequest({
467+
method: "item/commandExecution/requestApproval",
468+
requestParams: {
469+
threadId: "thread-1",
470+
turnId: "turn-1",
471+
itemId: "cmd-native-relay-deferred",
472+
command: "pnpm test extensions/codex/src/app-server",
473+
cwd: "/workspace",
474+
},
475+
paramsForRun: params,
476+
threadId: "thread-1",
477+
turnId: "turn-1",
478+
nativeHookRelay: {
479+
relayId: "relay-1",
480+
allowedEvents: ["pre_tool_use"],
481+
},
482+
});
483+
484+
expect(result).toEqual({ decision: "accept" });
485+
expect(mockRunBeforeToolCallHook).not.toHaveBeenCalled();
486+
expect(mockInvokeNativeHookRelay).not.toHaveBeenCalled();
487+
expect(mockCallGatewayTool).not.toHaveBeenCalled();
488+
findApprovalEvent(params, {
489+
status: "approved",
490+
message: "Codex app-server approval granted for this turn.",
491+
});
492+
});
493+
441494
it("fails closed when the native hook relay returns unreadable approval output", async () => {
442495
const params = createParams();
443496
mockInvokeNativeHookRelay.mockResolvedValueOnce({
@@ -676,6 +729,43 @@ describe("Codex app-server approval bridge", () => {
676729
});
677730
});
678731

732+
it("keeps OpenClaw plugin allow-always approvals scoped to one Codex request", async () => {
733+
const params = createParams();
734+
mockRunBeforeToolCallHook.mockResolvedValueOnce({
735+
blocked: false,
736+
params: {
737+
command: "pnpm test",
738+
approval: {
739+
threadId: "thread-1",
740+
turnId: "turn-1",
741+
itemId: "cmd-needs-approval",
742+
command: "pnpm test",
743+
},
744+
},
745+
approvalResolution: "allow-always",
746+
});
747+
748+
const result = await handleCodexAppServerApprovalRequest({
749+
method: "item/commandExecution/requestApproval",
750+
requestParams: {
751+
threadId: "thread-1",
752+
turnId: "turn-1",
753+
itemId: "cmd-needs-approval",
754+
command: "pnpm test",
755+
},
756+
paramsForRun: params,
757+
threadId: "thread-1",
758+
turnId: "turn-1",
759+
});
760+
761+
expect(result).toEqual({ decision: "accept" });
762+
expect(mockCallGatewayTool).not.toHaveBeenCalled();
763+
findApprovalEvent(params, {
764+
status: "approved",
765+
message: "Codex app-server approval granted for this turn.",
766+
});
767+
});
768+
679769
it("denies command approvals when OpenClaw tool policy requires approval", async () => {
680770
const params = createParams();
681771
mockRunBeforeToolCallHook.mockResolvedValueOnce({

‎extensions/codex/src/app-server/approval-bridge.ts‎

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
formatApprovalDisplayPath,
55
hasNativeHookRelayInvocation,
66
invokeNativeHookRelay,
7+
resolveNativeHookRelayDeferredToolApproval,
78
type EmbeddedRunAttemptParams,
89
type NativeHookRelayProcessResponse,
910
type NativeHookRelayRegistrationHandle,
@@ -101,6 +102,21 @@ export async function handleCodexAppServerApprovalRequest(params: {
101102
});
102103
return buildApprovalResponse(params.method, context.requestParams, "denied");
103104
}
105+
if (
106+
policyOutcome?.outcome === "approved-once" ||
107+
policyOutcome?.outcome === "approved-session"
108+
) {
109+
emitApprovalEvent(params.paramsForRun, {
110+
phase: "resolved",
111+
kind: context.kind,
112+
status: "approved",
113+
title: context.title,
114+
...context.eventDetails,
115+
...approvalEventScope(params.method, policyOutcome.outcome),
116+
message: approvalResolutionMessage(policyOutcome.outcome),
117+
});
118+
return buildApprovalResponse(params.method, context.requestParams, policyOutcome.outcome);
119+
}
104120
if (params.autoApprove === true) {
105121
emitApprovalEvent(params.paramsForRun, {
106122
phase: "resolved",
@@ -312,7 +328,10 @@ function buildApprovalContext(params: {
312328
}
313329

314330
type ApprovalContext = ReturnType<typeof buildApprovalContext>;
315-
type ApprovalPolicyOutcome = { outcome: "denied"; reason: string } | { outcome: "no-decision" };
331+
type ApprovalPolicyOutcome =
332+
| { outcome: "denied"; reason: string }
333+
| { outcome: "approved-once" | "approved-session" }
334+
| { outcome: "no-decision" };
316335

317336
async function runOpenClawToolPolicyForApprovalRequest(params: {
318337
method: string;
@@ -337,10 +356,17 @@ async function runOpenClawToolPolicyForApprovalRequest(params: {
337356
policyRequest,
338357
nativeHookRelay: params.nativeHookRelay,
339358
cwd,
359+
signal: params.signal,
340360
});
341361
if (nativeRelayOutcome?.blocked) {
342362
return { outcome: "denied", reason: nativeRelayOutcome.reason };
343363
}
364+
if (
365+
nativeRelayOutcome?.outcome === "approved-once" ||
366+
nativeRelayOutcome?.outcome === "approved-session"
367+
) {
368+
return { outcome: nativeRelayOutcome.outcome };
369+
}
344370
if (nativeRelayOutcome?.handled) {
345371
return { outcome: "no-decision" };
346372
}
@@ -355,7 +381,7 @@ async function runOpenClawToolPolicyForApprovalRequest(params: {
355381
toolName: policyRequest.toolName,
356382
params: policyRequest.params,
357383
...(params.context.itemId ? { toolCallId: params.context.itemId } : {}),
358-
approvalMode: "report",
384+
approvalMode: "request",
359385
signal: params.signal,
360386
ctx: {
361387
...(params.paramsForRun.agentId ? { agentId: params.paramsForRun.agentId } : {}),
@@ -377,6 +403,13 @@ async function runOpenClawToolPolicyForApprovalRequest(params: {
377403
"OpenClaw tool policy rewrote Codex app-server approval params; refusing original request.",
378404
};
379405
}
406+
if (outcome.approvalResolution) {
407+
return {
408+
// Generic plugin approval `allow-always` is plugin-owned durability, not
409+
// Codex session trust. Keep the app-server request scoped to this item.
410+
outcome: "approved-once",
411+
};
412+
}
380413
return undefined;
381414
}
382415

@@ -390,6 +423,7 @@ async function runNativeRelayToolPolicyForApprovalRequest(params: {
390423
"allowedEvents" | "generation" | "relayId"
391424
>;
392425
cwd?: string;
426+
signal?: AbortSignal;
393427
}): Promise<
394428
| {
395429
handled: true;
@@ -399,6 +433,7 @@ async function runNativeRelayToolPolicyForApprovalRequest(params: {
399433
| {
400434
handled: true;
401435
blocked?: false;
436+
outcome?: "approved-once" | "approved-session";
402437
}
403438
| undefined
404439
> {
@@ -426,6 +461,17 @@ async function runNativeRelayToolPolicyForApprovalRequest(params: {
426461
toolUseId: params.context.itemId,
427462
})
428463
) {
464+
const approvalOutcome = await resolveNativeHookRelayDeferredToolApproval({
465+
relayId: params.nativeHookRelay.relayId,
466+
toolUseId: params.context.itemId,
467+
signal: params.signal,
468+
});
469+
if (approvalOutcome?.outcome === "denied") {
470+
return { handled: true, blocked: true, reason: approvalOutcome.reason };
471+
}
472+
if (approvalOutcome?.outcome === "approved-once") {
473+
return { handled: true, outcome: approvalOutcome.outcome };
474+
}
429475
return { handled: true };
430476
}
431477
try {
@@ -441,6 +487,17 @@ async function runNativeRelayToolPolicyForApprovalRequest(params: {
441487
if (decision.blocked) {
442488
return { handled: true, blocked: true, reason: decision.reason };
443489
}
490+
const approvalOutcome = await resolveNativeHookRelayDeferredToolApproval({
491+
relayId: params.nativeHookRelay.relayId,
492+
toolUseId: params.context.itemId,
493+
signal: params.signal,
494+
});
495+
if (approvalOutcome?.outcome === "denied") {
496+
return { handled: true, blocked: true, reason: approvalOutcome.reason };
497+
}
498+
if (approvalOutcome?.outcome === "approved-once") {
499+
return { handled: true, outcome: approvalOutcome.outcome };
500+
}
444501
return { handled: true };
445502
} catch (error) {
446503
return {

‎src/agents/agent-tools.before-tool-call.embedded-mode.test.ts‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,37 @@ describe("runBeforeToolCallHook — embedded mode approvals", () => {
160160
expect(mockCallGatewayTool).not.toHaveBeenCalled();
161161
});
162162

163+
it("defers approval-required tools without opening an approval request", async () => {
164+
runBeforeToolCallMock.mockResolvedValue({
165+
requireApproval: {
166+
pluginId: "test-plugin",
167+
title: "Needs approval",
168+
description: "Review before running",
169+
severity: "info",
170+
},
171+
params: { adjusted: true },
172+
});
173+
174+
const result = await runBeforeToolCallHook({
175+
toolName: "exec",
176+
params: { command: "ls" },
177+
toolCallId: "call-defer",
178+
approvalMode: "defer",
179+
});
180+
181+
expect(result).toMatchObject({
182+
blocked: false,
183+
params: { command: "ls" },
184+
deferredApproval: {
185+
toolName: "exec",
186+
toolCallId: "call-defer",
187+
baseParams: { command: "ls" },
188+
overrideParams: { adjusted: true },
189+
},
190+
});
191+
expect(mockCallGatewayTool).not.toHaveBeenCalled();
192+
});
193+
163194
it("sends approval to gateway when NOT in embedded mode", async () => {
164195
setEmbeddedMode(false);
165196

@@ -263,7 +294,11 @@ describe("runBeforeToolCallHook — embedded mode approvals", () => {
263294
ctx: { agentId: "main", sessionKey: "main" },
264295
});
265296

266-
expect(result).toEqual({ blocked: false, params: { command: "deploy" } });
297+
expect(result).toEqual({
298+
blocked: false,
299+
params: { command: "deploy" },
300+
approvalResolution: PluginApprovalResolutions.ALLOW_ONCE,
301+
});
267302
const approvalCall = requireApprovalRequestCall("trusted policy approval request");
268303
expect(approvalCall.timeoutParams.timeoutMs).toBe(130_000);
269304
expect(approvalCall.request.pluginId).toBe("trusted-policy");

0 commit comments

Comments
 (0)