Skip to content

Commit a5c7346

Browse files
author
Hirokazu (Life OS)
committed
Preserve Codex hooks on relay memory guard denial
1 parent 5978de8 commit a5c7346

6 files changed

Lines changed: 132 additions & 12 deletions

File tree

‎extensions/codex/src/app-server/native-hook-relay.test.ts‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ import {
77
attachCodexNativeHookRelayAdmissionRelease,
88
buildCodexNativeHookRelayConfig,
99
buildCodexNativeHookRelayDisabledConfig,
10+
buildCodexNativeHookRelaySuppressedConfig,
1011
resetCodexNativeHookRelayAdmissionsForTests,
1112
resolveCodexNativeHookRelayCommandTimeoutMs,
1213
resolveCodexNativeHookRelayUnregisterGraceMs,
1314
} from "./native-hook-relay.js";
15+
import { mergeCodexThreadConfigs } from "./plugin-thread-config.js";
1416

1517
describe("Codex native hook relay config", () => {
1618
it("builds deterministic Codex config overrides with command hooks", () => {
@@ -300,6 +302,59 @@ describe("Codex native hook relay config", () => {
300302
});
301303
});
302304

305+
it("builds scoped suppression config without disabling unrelated hooks", () => {
306+
expect(buildCodexNativeHookRelaySuppressedConfig()).toEqual({
307+
"hooks.state": {
308+
"/<session-flags>/config.toml:pre_tool_use:0:0": { enabled: false },
309+
"<session-flags>/config.toml:pre_tool_use:0:0": { enabled: false },
310+
"/<session-flags>/config.toml:post_tool_use:0:0": { enabled: false },
311+
"<session-flags>/config.toml:post_tool_use:0:0": { enabled: false },
312+
"/<session-flags>/config.toml:permission_request:0:0": { enabled: false },
313+
"<session-flags>/config.toml:permission_request:0:0": { enabled: false },
314+
"/<session-flags>/config.toml:stop:0:0": { enabled: false },
315+
"<session-flags>/config.toml:stop:0:0": { enabled: false },
316+
},
317+
});
318+
});
319+
320+
it("preserves existing Codex hooks when suppression config is merged last", () => {
321+
expect(
322+
mergeCodexThreadConfigs(
323+
{
324+
"features.hooks": true,
325+
"hooks.PreToolUse": [
326+
{
327+
hooks: [
328+
{
329+
type: "command",
330+
command: "echo user-pre-tool-use",
331+
timeout: 5,
332+
},
333+
],
334+
},
335+
],
336+
},
337+
buildCodexNativeHookRelaySuppressedConfig(),
338+
),
339+
).toMatchObject({
340+
"features.hooks": true,
341+
"hooks.PreToolUse": [
342+
{
343+
hooks: [
344+
{
345+
type: "command",
346+
command: "echo user-pre-tool-use",
347+
timeout: 5,
348+
},
349+
],
350+
},
351+
],
352+
"hooks.state": {
353+
"/<session-flags>/config.toml:pre_tool_use:0:0": { enabled: false },
354+
},
355+
});
356+
});
357+
303358
it("caps oversized native hook cleanup grace before scheduling", () => {
304359
expect(resolveCodexNativeHookRelayUnregisterGraceMs(Number.MAX_SAFE_INTEGER)).toBe(
305360
MAX_TIMER_TIMEOUT_MS,

‎extensions/codex/src/app-server/native-hook-relay.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,21 @@ export function buildCodexNativeHookRelayDisabledConfig(): JsonObject {
445445
};
446446
}
447447

448+
/** Builds a scoped overlay for automatic relay suppression without disabling other hooks. */
449+
export function buildCodexNativeHookRelaySuppressedConfig(): JsonObject {
450+
const hookState: JsonObject = {};
451+
for (const event of CODEX_NATIVE_HOOK_RELAY_EVENTS) {
452+
for (const sourcePath of CODEX_SESSION_FLAGS_HOOK_SOURCE_PATHS) {
453+
hookState[`${sourcePath}:${CODEX_HOOK_KEY_LABEL_BY_NATIVE_EVENT[event]}:0:0`] = {
454+
enabled: false,
455+
} satisfies JsonValue;
456+
}
457+
}
458+
return {
459+
"hooks.state": hookState,
460+
};
461+
}
462+
448463
function normalizeHookTimeoutSec(value: number | undefined): number {
449464
return typeof value === "number" && Number.isFinite(value) && value > 0 ? Math.ceil(value) : 5;
450465
}

‎extensions/codex/src/app-server/run-attempt.native-hook-relay.test.ts‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,7 @@ describe("runCodexAppServerAttempt native hook relay", () => {
716716
expect(startConfig?.["hooks.Stop"]).toEqual([]);
717717
});
718718

719-
it("sends clearing Codex native hook config when memory guard denies relay admission", async () => {
719+
it("suppresses only OpenClaw relay hook state when memory guard denies relay admission", async () => {
720720
const sessionFile = path.join(tempDir, "session.jsonl");
721721
const workspaceDir = path.join(tempDir, "workspace");
722722
const harness = createStartedThreadHarness();
@@ -739,11 +739,17 @@ describe("runCodexAppServerAttempt native hook relay", () => {
739739
const startRequest = harness.requests.find((request) => request.method === "thread/start");
740740
const startConfig = (startRequest?.params as { config?: Record<string, unknown> } | undefined)
741741
?.config;
742-
expect(startConfig?.["features.hooks"]).toBe(false);
743-
expect(startConfig?.["hooks.PreToolUse"]).toEqual([]);
744-
expect(startConfig?.["hooks.PostToolUse"]).toEqual([]);
745-
expect(startConfig?.["hooks.PermissionRequest"]).toEqual([]);
746-
expect(startConfig?.["hooks.Stop"]).toEqual([]);
742+
expect(startConfig?.["features.hooks"]).not.toBe(false);
743+
expect(startConfig).not.toHaveProperty("hooks.PreToolUse");
744+
expect(startConfig).not.toHaveProperty("hooks.PostToolUse");
745+
expect(startConfig).not.toHaveProperty("hooks.PermissionRequest");
746+
expect(startConfig).not.toHaveProperty("hooks.Stop");
747+
expect(startConfig?.["hooks.state"]).toMatchObject({
748+
"/<session-flags>/config.toml:pre_tool_use:0:0": { enabled: false },
749+
"/<session-flags>/config.toml:post_tool_use:0:0": { enabled: false },
750+
"/<session-flags>/config.toml:permission_request:0:0": { enabled: false },
751+
"/<session-flags>/config.toml:stop:0:0": { enabled: false },
752+
});
747753
});
748754

749755
it("cleans up native hook relay state when turn/start fails", async () => {

‎extensions/codex/src/app-server/run-attempt.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ import {
200200
buildCodexNativeHookRelayDisabledConfig,
201201
buildCodexNativeHookRelayConfig,
202202
buildCodexNativeHookRelayId,
203+
buildCodexNativeHookRelaySuppressedConfig,
203204
clearPendingCodexNativeHookRelayUnregistersForTests,
204205
CODEX_NATIVE_HOOK_RELAY_TTL_GRACE_MS,
205206
createCodexNativeHookRelay,
@@ -1488,9 +1489,11 @@ export async function runCodexAppServerAttempt(
14881489
events: nativeHookRelayEvents,
14891490
hookTimeoutSec: options.nativeHookRelay?.hookTimeoutSec,
14901491
})
1491-
: options.nativeHookRelay?.enabled === false || nativeHookRelayDisabledByMemoryGuard
1492-
? buildCodexNativeHookRelayDisabledConfig()
1493-
: undefined,
1492+
: nativeHookRelayDisabledByMemoryGuard
1493+
? buildCodexNativeHookRelaySuppressedConfig()
1494+
: options.nativeHookRelay?.enabled === false
1495+
? buildCodexNativeHookRelayDisabledConfig()
1496+
: undefined,
14941497
nativeHookRelayGeneration: nativeHookRelay?.generation,
14951498
};
14961499
};

‎extensions/codex/src/app-server/side-question.test.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,6 +1135,44 @@ describe("runCodexAppServerSideQuestion", () => {
11351135
expect(config).not.toHaveProperty("hooks.state");
11361136
});
11371137

1138+
it("suppresses only OpenClaw relay hook state when side-thread memory guard denies relay admission", async () => {
1139+
const client = createFakeClient();
1140+
getSharedCodexAppServerClientMock.mockResolvedValue(client);
1141+
1142+
await expect(
1143+
runCodexAppServerSideQuestion(sideParams(), {
1144+
nativeHookRelay: {
1145+
enabled: true,
1146+
memoryGuard: {
1147+
enabled: true,
1148+
minAvailableMemoryMb: 1024,
1149+
getAvailableMemoryBytesForTests: () => 512 * 1024 * 1024,
1150+
memoryUsageForTests: () => ({ rss: 128 * 1024 * 1024 }),
1151+
},
1152+
},
1153+
}),
1154+
).resolves.toEqual({ text: "Side answer." });
1155+
1156+
const forkParams = mockCall(client.request)[1] as Record<string, unknown> | undefined;
1157+
const config = forkParams?.config as Record<string, unknown> | undefined;
1158+
expect(config).toMatchObject({
1159+
"features.code_mode": true,
1160+
"features.code_mode_only": false,
1161+
"features.apply_patch_streaming_events": true,
1162+
});
1163+
expect(config?.["features.hooks"]).not.toBe(false);
1164+
expect(config).not.toHaveProperty("hooks.PreToolUse");
1165+
expect(config).not.toHaveProperty("hooks.PostToolUse");
1166+
expect(config).not.toHaveProperty("hooks.PermissionRequest");
1167+
expect(config).not.toHaveProperty("hooks.Stop");
1168+
expect(config?.["hooks.state"]).toMatchObject({
1169+
"/<session-flags>/config.toml:pre_tool_use:0:0": { enabled: false },
1170+
"/<session-flags>/config.toml:post_tool_use:0:0": { enabled: false },
1171+
"/<session-flags>/config.toml:permission_request:0:0": { enabled: false },
1172+
"/<session-flags>/config.toml:stop:0:0": { enabled: false },
1173+
});
1174+
});
1175+
11381176
it("passes Codex code-mode-only opt-in to side-thread forks", async () => {
11391177
const client = createFakeClient();
11401178
getSharedCodexAppServerClientMock.mockResolvedValue(client);

‎extensions/codex/src/app-server/side-question.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import {
5151
attachCodexNativeHookRelayAdmissionRelease,
5252
buildCodexNativeHookRelayConfig,
5353
buildCodexNativeHookRelayDisabledConfig,
54+
buildCodexNativeHookRelaySuppressedConfig,
5455
CODEX_NATIVE_HOOK_RELAY_EVENTS,
5556
type CodexNativeHookRelayAdmission,
5657
type CodexNativeHookRelayMemoryGuardConfig,
@@ -443,9 +444,11 @@ export async function runCodexAppServerSideQuestion(
443444
hookTimeoutSec: options.nativeHookRelay?.hookTimeoutSec,
444445
clearOmittedEvents: true,
445446
})
446-
: options.nativeHookRelay?.enabled === false || nativeHookRelayDisabledByMemoryGuard
447-
? buildCodexNativeHookRelayDisabledConfig()
448-
: undefined;
447+
: nativeHookRelayDisabledByMemoryGuard
448+
? buildCodexNativeHookRelaySuppressedConfig()
449+
: options.nativeHookRelay?.enabled === false
450+
? buildCodexNativeHookRelayDisabledConfig()
451+
: undefined;
449452
const runtimeThreadConfig = buildCodexRuntimeThreadConfig(webSearchPlan.threadConfig, {
450453
nativeCodeModeEnabled: nativeToolSurfaceEnabled,
451454
nativeCodeModeOnlyEnabled: appServer.codeModeOnly,

0 commit comments

Comments
 (0)