Skip to content

fix: evaluate tool_result_persist hooks lazily to avoid race condition - #17930

Closed
TheArkifaneVashtorr wants to merge 1 commit into
openclaw:mainfrom
TheArkifaneVashtorr:fix/tool-result-persist-hook-race-condition
Closed

TheArkifaneVashtorr wants to merge 1 commit into
openclaw:mainfrom
TheArkifaneVashtorr:fix/tool-result-persist-hook-race-condition

Conversation

@TheArkifaneVashtorr

@TheArkifaneVashtorr TheArkifaneVashtorr commented Feb 16, 2026 •

Copy link
Copy Markdown

Problem

guardSessionManager() in session-tool-result-guard-wrapper.ts captures hookRunner?.hasHooks("tool_result_persist") eagerly at setup time as a ternary condition. If plugins register their tool_result_persist hooks after this function runs (which is the common case since plugin loading is async), the check permanently returns false and the transform callback is set to undefined.

This means tool_result_persist hooks registered via api.on("tool_result_persist", ...) never fire, even though they appear as registered in the hook runner and openclaw hooks check shows them as ready.

Root Cause

// BEFORE: eager capture — hookRunner is evaluated once
const hookRunner = getGlobalHookRunner();
const transform = hookRunner?.hasHooks("tool_result_persist")
  ? (message, meta) => { /* ... */ }
  : undefined;  // <-- permanently undefined if plugins load after this

Fix

Move getGlobalHookRunner() and hasHooks() inside the callback so they are evaluated lazily on each tool result:

// AFTER: lazy evaluation — checks on every tool result
const transform = (message, meta) => {
  const hookRunner = getGlobalHookRunner();
  if (!hookRunner?.hasHooks("tool_result_persist")) return undefined;
  return hookRunner.runToolResultPersist(/* ... */)?.message ?? message;
};

Impact

  • Before: Any plugin using api.on("tool_result_persist", ...) has its handler silently ignored
  • After: tool_result_persist hooks fire correctly regardless of plugin load order

Testing

Verified on OpenClaw 2026.2.3-1 (Linux):

  1. Created a plugin registering a tool_result_persist hook via api.on()
  2. With hooks.internal.enabled: true, the hook was registered but never called
  3. After applying this patch, the hook fires on every tool result

Related

Notes

This is a one-file, 23-line change with no behavioral difference when no tool_result_persist hooks are registered (the hasHooks() check still gates execution, just lazily).

Greptile Summary

This PR fixes a race condition in guardSessionManager() where the tool_result_persist hook availability was checked eagerly at setup time. Since plugin loading is async, hooks registered after guard installation were permanently missed. The fix moves getGlobalHookRunner() and hasHooks() inside the transform callback so they are evaluated lazily on each tool result. The PR also cleans up dead before_message_write hook wiring that referenced a non-existent hook type.

  • Lazy evaluation of getGlobalHookRunner() and hasHooks("tool_result_persist") ensures plugins loaded after guardSessionManager() are correctly detected
  • Removed dead beforeMessageWrite hook code — the before_message_write hook type does not exist in the current codebase and installSessionToolResultGuard does not accept a beforeMessageWriteHook option
  • A previously flagged issue regarding return undefined on the no-hooks path (line 37) still needs to be addressed before merging — see existing thread

Confidence Score: 2/5

  • This PR should not be merged until the previously flagged return undefined issue is resolved — it will silently drop tool result messages when no hooks are registered.
  • The core idea (lazy hook evaluation) is correct and well-motivated. However, the existing thread identified that the transform function now always exists (never undefined), causing persistToolResult to always invoke it. When no hooks are registered, return undefined on line 37 propagates as undefined through originalAppend(), replacing the tool result message with undefined. This is a functional regression for the common no-hooks case. The cleanup of dead beforeMessageWrite code is fine.
  • src/agents/session-tool-result-guard-wrapper.ts — the return undefined on line 37 needs to be changed to return message to preserve tool results when no hooks are registered

Last reviewed commit: 1639ae3

guardSessionManager() captures hookRunner?.hasHooks('tool_result_persist')
eagerly at setup time. If plugins register their tool_result_persist hooks
after this function runs (which is common since plugin loading is async),
the check permanently returns false and the transform is set to undefined.

This means tool_result_persist hooks registered via api.on() never fire,
even though they appear as registered in the hook runner.

Fix: move getGlobalHookRunner() and hasHooks() inside the callback so
they are evaluated on each tool result, not once at guard installation.

Closes #11229
@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: XS labels Feb 16, 2026

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 file reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

// oxlint-disable-next-line typescript/no-explicit-any
const transform = (message: any, meta: { toolCallId?: string; toolName?: string; isSynthetic?: boolean }) => {
const hookRunner = getGlobalHookRunner();
if (!hookRunner?.hasHooks("tool_result_persist")) return undefined;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning undefined drops tool results when no hooks are registered

When no tool_result_persist hooks exist, this line returns undefined. However, the consumer in session-tool-result-guard.ts (persistToolResult) checks whether the transform function is truthy — and since transform is now always a function (never undefined), it always calls transformer(message, meta) and uses the return value directly. When that return value is undefined, the tool result message is replaced with undefined and passed to originalAppend(undefined as never).

Before this PR: transform was undefined → persistToolResult fell through to return message (correct).
After this PR: transform is always a function → persistToolResult calls it → gets undefined back → persists undefined instead of the original message.

The fix is to return message instead of undefined when no hooks are registered:

Suggested change
if (!hookRunner?.hasHooks("tool_result_persist")) return undefined;
if (!hookRunner?.hasHooks("tool_result_persist")) return message;
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/agents/session-tool-result-guard-wrapper.ts
Line: 37:37

Comment:
**Returning `undefined` drops tool results when no hooks are registered**

When no `tool_result_persist` hooks exist, this line returns `undefined`. However, the consumer in `session-tool-result-guard.ts` (`persistToolResult`) checks whether the transform *function* is truthy — and since `transform` is now always a function (never `undefined`), it always calls `transformer(message, meta)` and uses the return value directly. When that return value is `undefined`, the tool result message is replaced with `undefined` and passed to `originalAppend(undefined as never)`.

**Before this PR**: `transform` was `undefined` → `persistToolResult` fell through to `return message` (correct).
**After this PR**: `transform` is always a function → `persistToolResult` calls it → gets `undefined` back → persists `undefined` instead of the original message.

The fix is to return `message` instead of `undefined` when no hooks are registered:

```suggestion
    if (!hookRunner?.hasHooks("tool_result_persist")) return message;
```

How can I resolve this? If you propose a fix, please make it concise.

@steipete steipete closed this Feb 16, 2026
@steipete steipete reopened this Feb 17, 2026
@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 openclaw-barnacle Bot added stale Marked as stale due to inactivity and removed stale Marked as stale due to inactivity labels Feb 22, 2026
@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 openclaw-barnacle Bot added the stale Marked as stale due to inactivity label Mar 14, 2026
@openclaw-barnacle

Copy link
Copy Markdown

Closing due to inactivity.
If you believe this PR should be revived, post in #pr-thunderdome-dangerzone 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

agents Agent runtime and tooling size: XS stale Marked as stale due to inactivity

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants