Skip to content

Commit f6511f7

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 ff20dd7 commit f6511f7

3 files changed

Lines changed: 29 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: 22 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,9 @@ export function buildEmbeddedExtensionFactories(params: {
179188
modelId: string;
180189
model: ProviderRuntimeModel | undefined;
181190
runId?: string;
191+
agentId?: string;
192+
sessionId?: string;
193+
sessionKey?: string;
182194
}): ExtensionFactory[] {
183195
const factories: ExtensionFactory[] = [];
184196
if (resolveEffectiveCompactionMode(params.cfg) === "safeguard") {
@@ -209,6 +221,14 @@ export function buildEmbeddedExtensionFactories(params: {
209221
if (pruningFactory) {
210222
factories.push(pruningFactory);
211223
}
212-
factories.push(buildAgentToolResultMiddlewareFactory(params.sessionManager, params.runId));
224+
factories.push(
225+
buildAgentToolResultMiddlewareFactory(
226+
params.sessionManager,
227+
params.agentId,
228+
params.sessionId,
229+
params.sessionKey,
230+
params.runId,
231+
),
232+
);
213233
return factories;
214234
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ export async function prepareEmbeddedAttemptAgentSession(input: {
8080
modelId: attempt.modelId,
8181
model: attempt.model,
8282
runId: attempt.runId,
83+
agentId: attempt.agentId ?? input.sessionAgentId,
84+
sessionKey: attempt.sessionKey,
85+
sessionId: attempt.sessionId,
8386
});
8487
const resourceLoader = createEmbeddedAgentResourceLoader({
8588
cwd: input.effectiveCwd,

0 commit comments

Comments
 (0)