openclaw - 💡(How to fix) Fix [Feature]: Add `before_tool` hook for mandatory pre-execution checks [1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
openclaw/openclaw#60065Fetched 2026-04-08 02:36:52
View on GitHub
Comments
0
Participants
1
Timeline
1
Reactions
0
Author
Participants
Timeline (top)
labeled ×1

Add a before_tool hook that fires before tool execution, allowing agents to enforce mandatory pre-checks (e.g., safety assessment, backup) before system-changing operations.

Error Message

Relying on agents to call a Safety Agent before actions is error-prone. Post-execution hooks can't prevent undesired changes. Prohibiting risky tools globally would overly constrain functionality. There's no native, systematic way to enforce such a check per tool invocation.

Root Cause

Add a before_tool hook that fires before tool execution, allowing agents to enforce mandatory pre-checks (e.g., safety assessment, backup) before system-changing operations.

Fix Action

Fix / Workaround

Our workaround of using a separate Safety Agent (with sessions_spawn) is fragile, as it relies on correct agent behavior rather than enforcing policy at the framework level.

{
  "hooks": {
    "internal": {
      "entries": {
        "before-tool": {
          "enabled": true,
          "match": {
            "tools": ["write", "edit", "apply_patch", "gateway", "cron"]
          },
          "action": "spawn-agent",
          "agentId": "safety"
        }
      }
    }
  }
}

This expands the hooks system, enabling agents and policies to intercept and control potentially dangerous operations in a programmatic, auditable way.

Workaround example config, direct agent calls to safety checks, logs of missed calls. Consider the value of pre-commit hooks in git, or admission controllers in Kubernetes as prior art.

Code Example

{
  "hooks": {
    "internal": {
      "entries": {
        "before-tool": {
          "enabled": true,
          "match": {
            "tools": ["write", "edit", "apply_patch", "gateway", "cron"]
          },
          "action": "spawn-agent",
          "agentId": "safety"
        }
      }
    }
  }
}
RAW_BUFFERClick to expand / collapse

Summary

Add a before_tool hook that fires before tool execution, allowing agents to enforce mandatory pre-checks (e.g., safety assessment, backup) before system-changing operations.

Problem to solve

There is currently no way to intercept tool execution before it happens. The current hook system supports only post-execution or startup triggers (such as boot-md, bootstrap-extra-files, command-logger, session-memory). As a result, it is not possible to guarantee mandatory pre-checks before potentially dangerous or impactful operations, which reduces system safety and composability.

Our workaround of using a separate Safety Agent (with sessions_spawn) is fragile, as it relies on correct agent behavior rather than enforcing policy at the framework level.

Proposed solution

Introduce a before_tool hook in the hooks configuration schema. This hook should trigger before any tool is executed and allow matching on specific tool types/names. If matched, it must execute a configurable action (e.g., spawn a Safety Agent, enforce policy, block, prompt for confirmation).

Proposed interface (in JSON):

{
  "hooks": {
    "internal": {
      "entries": {
        "before-tool": {
          "enabled": true,
          "match": {
            "tools": ["write", "edit", "apply_patch", "gateway", "cron"]
          },
          "action": "spawn-agent",
          "agentId": "safety"
        }
      }
    }
  }
}

This expands the hooks system, enabling agents and policies to intercept and control potentially dangerous operations in a programmatic, auditable way.

Alternatives considered

Relying on agents to call a Safety Agent before actions is error-prone. Post-execution hooks can't prevent undesired changes. Prohibiting risky tools globally would overly constrain functionality. There's no native, systematic way to enforce such a check per tool invocation.

Impact

Affected: All platform users who run agents with system-modifying capabilities or sensitive operations. Severity: High in environments with multiple contributors or where system state integrity is crucial. Frequency: Whenever system-altering tools are invoked. Consequence: Without a mandatory pre-check, critical safety measures can be bypassed leading to outages or misconfiguration.

Evidence/examples

Workaround example config, direct agent calls to safety checks, logs of missed calls. Consider the value of pre-commit hooks in git, or admission controllers in Kubernetes as prior art.

Additional information

This should remain backward compatible with the existing hook schema. Consider opt-in with "enabled": true to allow gradual rollout.

extent analysis

TL;DR

Implement a before_tool hook in the hooks configuration schema to enforce mandatory pre-checks before tool execution.

Guidance

  • Introduce a before_tool hook that triggers before any tool is executed, allowing matching on specific tool types/names.
  • Define a configurable action (e.g., spawn a Safety Agent, enforce policy, block, prompt for confirmation) to be executed when a match is found.
  • Ensure the new hook is backward compatible with the existing hook schema, considering an opt-in approach with "enabled": true for gradual rollout.
  • Test the before_tool hook with various tool types and actions to verify its effectiveness in enforcing mandatory pre-checks.

Example

{
  "hooks": {
    "internal": {
      "entries": {
        "before-tool": {
          "enabled": true,
          "match": {
            "tools": ["write", "edit", "apply_patch", "gateway", "cron"]
          },
          "action": "spawn-agent",
          "agentId": "safety"
        }
      }
    }
  }
}

Notes

The proposed solution requires careful consideration of the hook's configuration and behavior to ensure it effectively enforces mandatory pre-checks without introducing unnecessary constraints or complexity.

Recommendation

Apply the proposed workaround by introducing the before_tool hook, as it provides a systematic way to enforce mandatory pre-checks before tool execution, improving system safety and composability.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

openclaw - 💡(How to fix) Fix [Feature]: Add `before_tool` hook for mandatory pre-execution checks [1 participants]