Skip to content

Add Codex native hook relay memory guard - #97350

Closed
safetybanana wants to merge 2 commits into
openclaw:mainfrom
safetybanana:codex-native-hook-memory-guard
Closed

safetybanana wants to merge 2 commits into
openclaw:mainfrom
safetybanana:codex-native-hook-memory-guard

Conversation

@safetybanana

@safetybanana safetybanana commented Jun 28, 2026 •

Copy link
Copy Markdown

Summary

  • Add opt-in plugins.entries.codex.config.appServer.nativeHookRelay.memoryGuard
  • Gate Codex native hook relay registration on available memory, process RSS, and active guarded relay count
  • Clear native hook config for guarded turns under pressure instead of registering hook commands
  • Apply the guard to both main Codex app-server turns and /btw side-question turns
  • Document the config and add manifest schema/UI hints

What Problem This Solves

On memory-constrained Gateway hosts, multiple Codex native hook relay helper processes can exist concurrently and each can consume hundreds of MiB. OpenClaw already has a disabled native hook relay config path; this patch makes relay installation conditional so low-memory hosts can degrade per turn instead of compounding pressure.

The guard is opt-in, so default behavior is unchanged.

Behavior

When memoryGuard.enabled is true, OpenClaw checks:

  • cgroup memory (memory.current/memory.max) when bounded
  • /proc/meminfo MemAvailable fallback
  • Gateway process RSS
  • guarded active relay count

If a configured threshold is exceeded, OpenClaw sends features.hooks=false plus empty native hook arrays for that turn. The model call can continue, but Codex-native hook relay behavior is not installed for that turn.

Example:

{
  plugins: {
    entries: {
      codex: {
        config: {
          appServer: {
            nativeHookRelay: {
              memoryGuard: {
                enabled: true,
                minAvailableMemoryMb: 1024,
                maxProcessRssMb: 1536,
                maxActiveRelays: 1,
              },
            },
          },
        },
      },
    },
  },
}

Tests

  • node scripts/run-vitest.mjs run extensions/codex/src/app-server/native-hook-relay.test.ts --maxWorkers=1
  • node scripts/run-vitest.mjs run extensions/codex/src/app-server/config.test.ts --maxWorkers=1
  • node scripts/run-vitest.mjs run extensions/codex/src/app-server/run-attempt.native-hook-relay.test.ts --maxWorkers=1
  • git diff --check HEAD~1 HEAD

Evidence

Local focused validation on the source branch:

  • native-hook-relay.test.ts: 13 tests passed
  • config.test.ts: 109 tests passed
  • run-attempt.native-hook-relay.test.ts: 19 tests passed
  • git diff --check HEAD~1 HEAD: clean

The implementation uses the existing disabled relay overlay (features.hooks=false, empty hook arrays), so guarded degradation reuses the already-supported clearing path instead of introducing a new Codex config shape.

Notes

Raw pnpm exec vitest was not used for final validation because it spent several minutes in bundler startup on this host. The repository test wrapper was used instead.

@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation extensions: codex size: L triage: blank-template Candidate: PR template appears mostly untouched. triage: needs-pr-context Candidate: external PR body lacks required problem context or evidence. labels Jun 28, 2026
@clawsweeper

clawsweeper Bot commented Jun 28, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs real behavior proof before merge. Reviewed June 28, 2026, 5:18 AM ET / 09:18 UTC.

Summary
The PR adds an opt-in Codex native hook relay memory guard with config, docs, manifest metadata, and focused tests.

PR surface: Source +410, Tests +235, Docs +38. Total +683 across 10 files.

Reproducibility: yes. for source-level reproduction of the current relay pressure class: current main installs synchronous Codex command hooks, and upstream Codex spawns selected handlers as child processes; the PR's improved behavior is not proven in a real pressure run.

Review metrics: 1 noteworthy metric.

  • Codex config surface added: 1 object, 4 options added. The new appServer.nativeHookRelay.memoryGuard settings become supported operator-facing plugin config and need compatibility and degraded-mode review.

Merge readiness
Overall: 🧂 unranked krab
Proof: 🧂 unranked krab
Patch quality: 🧂 unranked krab
Result: blocked until real behavior proof from a real setup is added.

Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch.

Rank-up moves:

  • [P1] Fix the stale relay reference and add a resume-failure plus memoryGuard-denial regression test.
  • [P1] Add redacted terminal/log or artifact proof from a real Linux gateway pressure run showing memoryGuard denial and health/status responsiveness.
  • Get maintainer sign-off on the new Codex memoryGuard config contract and hook-suppression degraded mode.

Proof guidance:

  • [P1] Needs real behavior proof before merge: The PR body provides focused tests and diff checks, but no after-fix real gateway memory-pressure proof showing denial, bounded subprocess behavior, and responsive health/status calls with private details redacted. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.

Risk before merge

  • [P1] A resume-failure then guarded-start path can emit native hook relay config from an unregistered stale relay instead of the intended suppression config.
  • [P1] The new memoryGuard config is operator-facing Codex plugin surface with degraded-mode semantics that suppress OpenClaw session-flag hooks under pressure.
  • [P1] The PR has only focused test output, not real Linux gateway pressure proof showing denial decisions, bounded subprocess behavior, and health/status responsiveness.

Maintainer options:

  1. Fix guarded fallback and require pressure proof (recommended)
    Clear the stale relay reference before admission, add a resume-fallback denial test, and require redacted Linux gateway pressure proof plus maintainer config/security sign-off before merge.
  2. Accept as an opt-in emergency valve
    Maintainers could intentionally accept the new operator config and degraded hook behavior after manually validating the fixed path, but they would own the remaining proof and compatibility risk.
  3. Defer to relay redesign or fix(codex): omit idle PreToolUse hook relays #94515
    Pause or close this branch if maintainers prefer solving Codex PreToolUse native hook relay spawns CPU-bound openclaw-hooks processes and stalls gateway RPC #91009 through idle PreToolUse omission, a lightweight relay, or an OS-level concurrency design instead of adding memoryGuard config surface.

Next step before merge

  • [P1] Human review is needed because the external PR must supply real pressure proof and maintainers must approve the new config/security contract after the stale-relay defect is fixed.

Security
Needs attention: The diff changes security-sensitive native hook execution under pressure and has a stale-relay bug that can defeat guarded suppression.

Review findings

  • [P1] Clear stale relay before guarded fallback starts — extensions/codex/src/app-server/run-attempt.ts:1486-1493
Review details

Best possible solution:

Fix the stale-relay fallback defect, add regression coverage for resume-failure plus denied guarded start, then require redacted real gateway pressure proof and maintainer approval of the config/security contract before merge.

Do we have a high-confidence way to reproduce the issue?

Yes for source-level reproduction of the current relay pressure class: current main installs synchronous Codex command hooks, and upstream Codex spawns selected handlers as child processes; the PR's improved behavior is not proven in a real pressure run.

Is this the best way to solve the issue?

No for the current patch: memoryGuard is a plausible opt-in mitigation, but the stale relay bug must be fixed and maintainers still need to choose whether this config surface is the right long-term boundary versus #94515 or a lighter relay design.

Full review comments:

  • [P1] Clear stale relay before guarded fallback starts — extensions/codex/src/app-server/run-attempt.ts:1486-1493
    This return path still prefers a truthy nativeHookRelay over the new memory-guard suppression. If a resume attempt creates a relay, thread/resume fails, and the fallback thread/start is denied by memoryGuard, the old relay object remains set after unregister() and this branch emits hook config from that stale relay instead of buildCodexNativeHookRelaySuppressedConfig(). Under pressure that can reinstall stale native hook commands exactly when the guard is supposed to suppress them.
    Confidence: 0.89

Overall correctness: patch is incorrect
Overall confidence: 0.88

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against d17b970bb50d.

Label changes

Label justifications:

  • P1: The PR targets a real Codex relay pressure failure that can stall gateway RPCs and cause OOM or restart-loop availability failures.
  • merge-risk: 🚨 compatibility: The diff adds supported Codex plugin config keys and changes the native hook overlay emitted when those keys are enabled.
  • merge-risk: 🚨 security-boundary: Guarded denial suppresses OpenClaw session-flag native hooks, which can affect tool policy and finalization behavior for that turn.
  • merge-risk: 🚨 availability: The mitigation is intended to protect gateway availability, but the current patch can defeat suppression via a stale relay and lacks real pressure proof.
  • rating: 🧂 unranked krab: Overall readiness is 🧂 unranked krab; proof is 🧂 unranked krab and patch quality is 🧂 unranked krab.
  • status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask. Needs real behavior proof before merge: The PR body provides focused tests and diff checks, but no after-fix real gateway memory-pressure proof showing denial, bounded subprocess behavior, and responsive health/status calls with private details redacted. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.
Evidence reviewed

PR surface:

Source +410, Tests +235, Docs +38. Total +683 across 10 files.

View PR surface stats
Area Files Added Removed Net
Source 5 457 47 +410
Tests 4 238 3 +235
Docs 1 38 0 +38
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 10 733 50 +683

Security concerns:

  • [high] Stale relay can defeat guarded suppression — extensions/codex/src/app-server/run-attempt.ts:1486
    After resume failure, the attempt path can keep an unregistered relay object truthy and emit native hook config instead of suppression on a denied guarded start, which can re-enable stale hook commands during pressure.
    Confidence: 0.89
  • [medium] Hook suppression needs maintainer security sign-off — extensions/codex/src/app-server/native-hook-relay.ts:448
    Admission denial disables OpenClaw session-flag hooks for the turn, so native tool policy, diagnostics, or finalization hooks may be skipped exactly during memory pressure events.
    Confidence: 0.83

Acceptance criteria:

  • [P1] node scripts/run-vitest.mjs run extensions/codex/src/app-server/run-attempt.native-hook-relay.test.ts --maxWorkers=1.
  • [P1] node scripts/run-vitest.mjs run extensions/codex/src/app-server/native-hook-relay.test.ts --maxWorkers=1.
  • [P1] node scripts/run-vitest.mjs run extensions/codex/src/app-server/side-question.test.ts --maxWorkers=1.
  • [P1] git diff --check HEAD~1 HEAD.

What I checked:

  • PR state and proof surface: this PR is open at head a5c7346, changes 10 files with +733/-50, and its body reports focused tests plus git diff checks but no real after-fix gateway pressure run. (a5c7346e0f29)
  • Stale relay bug in PR head: The PR head unregisters an existing nativeHookRelay, then on denied admission can still prefer the truthy stale nativeHookRelay branch when building configPatch instead of the suppressed config branch. (extensions/codex/src/app-server/run-attempt.ts:1486, a5c7346e0f29)
  • Resume fallback caller path: startOrResumeThread calls buildFinalConfigPatch for resume, catches non-connection resume failure, clears the binding, then calls buildFinalConfigPatch again for start, so the same closure can see a relay from a failed resume before a guarded start. (extensions/codex/src/app-server/thread-lifecycle.ts:750, d17b970bb50d)
  • Current main relay behavior: Current main still enables Codex native hook relay from the harness and builds synchronous command hooks without memory/RSS/admission gating. (extensions/codex/harness.ts:75, d17b970bb50d)
  • PR adds operator-facing config surface: The PR adds appServer.nativeHookRelay.memoryGuard with enabled, minAvailableMemoryMb, maxProcessRssMb, and maxActiveRelays in runtime config, manifest schema, UI metadata, and docs. (extensions/codex/src/app-server/config.ts:121, a5c7346e0f29)
  • Suppression contract: The PR implements guarded degradation by writing hooks.state disables for OpenClaw session-flag hooks, while docs warn native hook behavior for Codex-owned native tools and finalization is not installed for that turn. (extensions/codex/src/app-server/native-hook-relay.ts:448, a5c7346e0f29)

Likely related people:

  • pashpashpash: Authored the merged Codex native hook bridge foundation that this PR modifies. (role: feature introducer; confidence: high; commits: 7a958d920c88; files: extensions/codex/src/app-server/native-hook-relay.ts, src/agents/harness/native-hook-relay.ts, src/cli/native-hook-relay-cli.ts)
  • joshavant: Recently changed native hook relay lifetime and native tool policy behavior in the affected relay path. (role: recent process-lifecycle contributor; confidence: high; commits: 14b1ebd640c6, e57b137aef41; files: extensions/codex/src/app-server/native-hook-relay.ts, src/agents/harness/native-hook-relay.ts)
  • steipete: Authored no-policy native hook fallback and multiple adjacent relay cleanup changes that define current behavior. (role: adjacent contributor and merger; confidence: high; commits: fb64546d9e6f, 3d0dc15904e2, 61cf22f14711; files: extensions/codex/src/app-server/native-hook-relay.ts, src/agents/harness/native-hook-relay.ts)
  • evgyur: Contributed the prior idle Codex hook relay subprocess reduction in the same pressure class. (role: adjacent idle-relay fix author; confidence: medium; commits: 592aae3696da; files: extensions/codex/src/app-server/native-hook-relay.ts)
  • Kaspre: Introduced side-question native hook coverage, and this PR extends memoryGuard into that sibling path. (role: side-question contributor; confidence: medium; commits: 69a0c925b890; files: extensions/codex/src/app-server/side-question.ts, extensions/codex/src/app-server/native-hook-relay.ts)
What the crustacean ranks mean
  • 🦀 challenger crab: rare, exceptional readiness with strong proof, clean implementation, and convincing validation.
  • 🦞 diamond lobster: very strong readiness with only minor maintainer review expected.
  • 🐚 platinum hermit: good normal PR, likely mergeable with ordinary maintainer review.
  • 🦐 gold shrimp: useful signal, but proof or patch confidence is still limited.
  • 🦪 silver shellfish: thin signal; proof, validation, or implementation needs work.
  • 🧂 unranked krab: not merge-ready because proof is missing/unusable or there are serious correctness or safety concerns.
  • 🌊 off-meta tidepool: rating does not apply to this item.

Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

How this review workflow works
  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

@clawsweeper clawsweeper Bot added rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. P1 High-priority user-facing bug, regression, or broken workflow. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. merge-risk: 🚨 security-boundary 🚨 May affect sandboxing, authorization, credentials, or sensitive data. labels Jun 28, 2026
@openclaw-barnacle openclaw-barnacle Bot removed the triage: needs-pr-context Candidate: external PR body lacks required problem context or evidence. label Jun 28, 2026
@safetybanana

Copy link
Copy Markdown
Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jun 28, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event repository_dispatch).
Result: the existing ClawSweeper review comment will be edited in place when the review finishes.

@clawsweeper clawsweeper Bot added the merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. label Jun 28, 2026
@safetybanana

Copy link
Copy Markdown
Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jun 28, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event repository_dispatch).
Result: the existing ClawSweeper review comment will be edited in place when the review finishes.

@safetybanana

Copy link
Copy Markdown
Author

@clawsweeper re-review

@openclaw-barnacle

Copy link
Copy Markdown

This pull request has been automatically marked as stale due to inactivity.
Please add updates or it will be closed.

@openclaw-barnacle

Copy link
Copy Markdown

Closing due to inactivity.
If you believe this PR should be revived, post in #clawtributors on Discord to talk to a maintainer.
That channel is the escape hatch for high-quality PRs that get auto-closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Improvements or additions to documentation extensions: codex merge-risk: 🚨 availability 🚨 May cause crashes, hangs, restart loops, stalls, or process outages. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. merge-risk: 🚨 security-boundary 🚨 May affect sandboxing, authorization, credentials, or sensitive data. P1 High-priority user-facing bug, regression, or broken workflow. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. size: L stale Marked as stale due to inactivity status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. triage: blank-template Candidate: PR template appears mostly untouched.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant