Skip to content

Commit d30e4e2

Browse files
committed
fix(agents): pass agent/session context to OpenClaw tool result middleware
The OpenClaw runtime constructed the AgentToolResultMiddlewareRunner with only { runtime: 'openclaw' }, omitting agentId, sessionId, sessionKey, and runId. This differed from the Codex runtime which passes the full toolResultHookContext. Add the missing fields so middleware can attribute and scope OpenClaw events at parity with Codex. Fixes #106910
1 parent b89f6df commit d30e4e2

3 files changed

Lines changed: 27 additions & 2 deletions

File tree

‎src/agents/embedded-agent-runner/extensions.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ function buildSafeguardFactories(cfg: OpenClawConfig, workspaceDir?: string) {
3535
provider: "anthropic",
3636
modelId: "claude-sonnet-4-20250514",
3737
model,
38+
agentId: "main",
39+
sessionKey: "agent:main:main",
3840
});
3941

4042
return { factories, sessionManager };
@@ -141,6 +143,8 @@ describe("buildEmbeddedExtensionFactories", () => {
141143
provider: "litellm",
142144
modelId: "claude-sonnet-4-6",
143145
model: { api: "anthropic-messages", contextWindow: 200_000 } as Model,
146+
agentId: "main",
147+
sessionKey: "agent:main:main",
144148
});
145149

146150
expect(factories).toContain(contextPruningExtension);

‎src/agents/embedded-agent-runner/extensions.ts‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,18 @@ function snapshotToolSendReceipt(details: unknown): unknown {
5252

5353
function buildAgentToolResultMiddlewareFactory(
5454
sessionManager: SessionManager,
55+
agentId?: string,
56+
sessionId?: string,
57+
sessionKey?: string,
5558
runId?: string,
5659
): ExtensionFactory {
57-
const runner = createAgentToolResultMiddlewareRunner({ runtime: "openclaw" });
60+
const runner = createAgentToolResultMiddlewareRunner({
61+
runtime: "openclaw",
62+
...(agentId ? { agentId } : {}),
63+
...(sessionId ? { sessionId } : {}),
64+
...(sessionKey ? { sessionKey } : {}),
65+
...(runId ? { runId } : {}),
66+
});
5867
return (agent) => {
5968
agent.on("tool_result", async (rawEvent: unknown, ctx: { cwd?: string }) => {
6069
const event = recordFromUnknown(rawEvent) as AgentToolResultEvent;
@@ -179,6 +188,8 @@ export function buildEmbeddedExtensionFactories(params: {
179188
modelId: string;
180189
model: ProviderRuntimeModel | undefined;
181190
runId?: string;
191+
agentId?: string;
192+
sessionKey?: string;
182193
}): ExtensionFactory[] {
183194
const factories: ExtensionFactory[] = [];
184195
if (resolveEffectiveCompactionMode(params.cfg) === "safeguard") {
@@ -212,6 +223,14 @@ export function buildEmbeddedExtensionFactories(params: {
212223
if (pruningFactory) {
213224
factories.push(pruningFactory);
214225
}
215-
factories.push(buildAgentToolResultMiddlewareFactory(params.sessionManager, params.runId));
226+
factories.push(
227+
buildAgentToolResultMiddlewareFactory(
228+
params.sessionManager,
229+
params.agentId,
230+
undefined,
231+
params.sessionKey,
232+
params.runId,
233+
),
234+
);
216235
return factories;
217236
}

‎src/agents/embedded-agent-runner/run/attempt-session.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ export async function prepareEmbeddedAttemptAgentSession(input: {
8383
modelId: attempt.modelId,
8484
model: attempt.model,
8585
runId: attempt.runId,
86+
agentId: attempt.agentId ?? input.sessionAgentId,
87+
sessionKey: attempt.sessionKey,
8688
});
8789
const resourceLoader = createEmbeddedAgentResourceLoader({
8890
cwd: input.effectiveCwd,

0 commit comments

Comments
 (0)