fix(hooks): propagate ephemeral sessionId through embedded tool contexts (#32273)

* fix(plugins): expose ephemeral sessionId in tool contexts for per-conversation isolation

The plugin tool context (`OpenClawPluginToolContext`) and tool hook
context (`PluginHookToolContext`) only provided `sessionKey`, which
is a durable channel identifier that survives /new and /reset.
Plugins like mem0 that need per-conversation isolation (e.g. mapping
Mem0 `run_id`) had no way to distinguish between conversations,
causing session-scoped memories to persist unbounded across resets.

Add `sessionId` (ephemeral UUID regenerated on /new and /reset) to:
- `OpenClawPluginToolContext` (factory context for plugin tools)
- `PluginHookToolContext` (before_tool_call / after_tool_call hooks)
- Internal `HookContext` for tool call wrappers

Thread the value from the run attempt through createOpenClawCodingTools
→ createOpenClawTools → resolvePluginTools and through the tool hook
wrapper.

Closes #31253

Made-with: Cursor

* fix(agents): propagate embedded sessionId through tool hook context

* test(hooks): cover sessionId in embedded tool hook contexts

* docs(changelog): add sessionId hook context follow-up note

* test(hooks): avoid toolCallId collision in after_tool_call e2e

---------

Co-authored-by: SidQin-cyber <sidqin0410@gmail.com>
This commit is contained in:
Vincent Koc
2026-03-02 15:11:51 -08:00
committed by GitHub
parent 3b3e47e15d
commit 0954b6bf5f
13 changed files with 56 additions and 5 deletions

View File

@@ -370,6 +370,7 @@ export async function compactEmbeddedPiSessionDirect(
messageProvider: params.messageChannel ?? params.messageProvider,
agentAccountId: params.agentAccountId,
sessionKey: sandboxSessionKey,
sessionId: params.sessionId,
groupId: params.groupId,
groupChannel: params.groupChannel,
groupSpace: params.groupSpace,

View File

@@ -585,6 +585,7 @@ export async function runEmbeddedAttempt(
senderE164: params.senderE164,
senderIsOwner: params.senderIsOwner,
sessionKey: sandboxSessionKey,
sessionId: params.sessionId,
agentDir,
workspaceDir: effectiveWorkspace,
config: params.config,
@@ -858,7 +859,8 @@ export async function runEmbeddedAttempt(
},
{
agentId: sessionAgentId,
sessionKey: params.sessionKey,
sessionKey: sandboxSessionKey,
sessionId: params.sessionId,
loopDetection: clientToolLoopDetection,
},
)
@@ -1186,6 +1188,7 @@ export async function runEmbeddedAttempt(
enforceFinalTag: params.enforceFinalTag,
config: params.config,
sessionKey: sandboxSessionKey,
sessionId: params.sessionId,
agentId: sessionAgentId,
});