Repository navigation
fix: evaluate tool_result_persist hooks lazily to avoid race condition - #17930
TheArkifaneVashtorr wants to merge 1 commit into
Conversation
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
| // 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; |
There was a problem hiding this 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:
| 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.|
This pull request has been automatically marked as stale due to inactivity. |
|
This pull request has been automatically marked as stale due to inactivity. |
|
Closing due to inactivity. |
Problem
guardSessionManager()insession-tool-result-guard-wrapper.tscaptureshookRunner?.hasHooks("tool_result_persist")eagerly at setup time as a ternary condition. If plugins register theirtool_result_persisthooks after this function runs (which is the common case since plugin loading is async), the check permanently returnsfalseand the transform callback is set toundefined.This means
tool_result_persisthooks registered viaapi.on("tool_result_persist", ...)never fire, even though they appear as registered in the hook runner andopenclaw hooks checkshows them as ready.Root Cause
Fix
Move
getGlobalHookRunner()andhasHooks()inside the callback so they are evaluated lazily on each tool result:Impact
api.on("tool_result_persist", ...)has its handler silently ignoredtool_result_persisthooks fire correctly regardless of plugin load orderTesting
Verified on OpenClaw 2026.2.3-1 (Linux):
tool_result_persisthook viaapi.on()hooks.internal.enabled: true, the hook was registered but never calledRelated
before_tool_call,before_agent_start) — worth auditingNotes
This is a one-file, 23-line change with no behavioral difference when no
tool_result_persisthooks are registered (thehasHooks()check still gates execution, just lazily).Greptile Summary
This PR fixes a race condition in
guardSessionManager()where thetool_result_persisthook availability was checked eagerly at setup time. Since plugin loading is async, hooks registered after guard installation were permanently missed. The fix movesgetGlobalHookRunner()andhasHooks()inside the transform callback so they are evaluated lazily on each tool result. The PR also cleans up deadbefore_message_writehook wiring that referenced a non-existent hook type.getGlobalHookRunner()andhasHooks("tool_result_persist")ensures plugins loaded afterguardSessionManager()are correctly detectedbeforeMessageWritehook code — thebefore_message_writehook type does not exist in the current codebase andinstallSessionToolResultGuarddoes not accept abeforeMessageWriteHookoptionreturn undefinedon the no-hooks path (line 37) still needs to be addressed before merging — see existing threadConfidence Score: 2/5
return undefinedissue is resolved — it will silently drop tool result messages when no hooks are registered.undefined), causingpersistToolResultto always invoke it. When no hooks are registered,return undefinedon line 37 propagates asundefinedthroughoriginalAppend(), replacing the tool result message withundefined. This is a functional regression for the common no-hooks case. The cleanup of deadbeforeMessageWritecode is fine.src/agents/session-tool-result-guard-wrapper.ts— thereturn undefinedon line 37 needs to be changed toreturn messageto preserve tool results when no hooks are registeredLast reviewed commit: 1639ae3