Skip to content

Commit 7a9c49f

Browse files
committed
fix(agents): preserve code mode hook context
1 parent fb70de8 commit 7a9c49f

6 files changed

Lines changed: 64 additions & 4 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Docs: https://docs.openclaw.ai
3434

3535
### Fixes
3636

37+
- Agents/code mode: preserve agent, session, run, and channel context in `before_tool_call` hooks for top-level `exec`/`wait` dispatches. Fixes #83387.
3738
- Replies: keep final payload delivery after live preview updates so channels can finalize or send the completed answer instead of losing preview-only drafts. (#83468)
3839
- Providers/Xiaomi: replay MiMo Anthropic-compatible `reasoning_content` as provider-required thinking blocks even when OpenClaw thinking is disabled, fixing follow-up tool turns for `mimo-v2-flash`. Fixes #83407. Thanks @Xgenious7.
3940
- Agents/exec approvals: forward approval-runtime credentials on agent-owned Gateway approval calls so approved async commands complete through the existing runtime path instead of stalling on unauthenticated follow-up calls. Thanks @IWhatsskill, @Patrick-Erichsen, and @jesse-merhi.

‎src/agents/pi-embedded-runner/compact.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,6 +1061,15 @@ async function compactEmbeddedPiSessionDirectOnce(
10611061
const { customTools } = splitSdkTools({
10621062
tools: effectiveTools,
10631063
sandboxEnabled: !!sandbox?.enabled,
1064+
toolHookContext: {
1065+
agentId: sessionAgentId,
1066+
config: params.config,
1067+
cwd: effectiveWorkspace,
1068+
sessionKey: sandboxSessionKey,
1069+
sessionId: params.sessionId,
1070+
runId: params.runId,
1071+
channelId: params.currentChannelId,
1072+
},
10641073
});
10651074
// Pi treats `tools` as a name allowlist during session creation. Pass the
10661075
// exact OpenClaw-managed registrations so custom tools survive startup.

‎src/agents/pi-embedded-runner/run/attempt.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2179,6 +2179,7 @@ export async function runEmbeddedAttempt(
21792179
const { customTools } = splitSdkTools({
21802180
tools: effectiveTools,
21812181
sandboxEnabled: !!sandbox?.enabled,
2182+
toolHookContext: catalogToolHookContext,
21822183
});
21832184

21842185
// Add client tools (OpenResponses hosted tools) to customTools.
Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
import type { AgentTool } from "@earendil-works/pi-agent-core";
22
import { toToolDefinitions } from "../pi-tool-definition-adapter.js";
3+
import type { HookContext } from "../pi-tools.before-tool-call.js";
34

45
// We always pass tools via `customTools` so our policy filtering, sandbox integration,
56
// and extended toolset remain consistent across providers.
67
type AnyAgentTool = AgentTool;
78

8-
export function splitSdkTools(options: { tools: AnyAgentTool[]; sandboxEnabled: boolean }): {
9+
export function splitSdkTools(options: {
10+
tools: AnyAgentTool[];
11+
sandboxEnabled: boolean;
12+
toolHookContext?: HookContext;
13+
}): {
914
customTools: ReturnType<typeof toToolDefinitions>;
1015
} {
11-
const { tools } = options;
16+
const { tools, toolHookContext } = options;
1217
return {
13-
customTools: toToolDefinitions(tools),
18+
customTools: toToolDefinitions(tools, toolHookContext),
1419
};
1520
}

‎src/agents/pi-tool-definition-adapter.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,10 @@ export function isClientToolNameConflictError(err: unknown): err is Error {
223223
return err instanceof Error && err.message.startsWith(CLIENT_TOOL_NAME_CONFLICT_PREFIX);
224224
}
225225

226-
export function toToolDefinitions(tools: AnyAgentTool[]): ToolDefinition[] {
226+
export function toToolDefinitions(
227+
tools: AnyAgentTool[],
228+
hookContext?: HookContext,
229+
): ToolDefinition[] {
227230
return tools.map((tool) => {
228231
const name = tool.name || "tool";
229232
const normalizedName = normalizeToolName(name);
@@ -242,6 +245,7 @@ export function toToolDefinitions(tools: AnyAgentTool[]): ToolDefinition[] {
242245
toolName: name,
243246
params,
244247
toolCallId,
248+
ctx: hookContext,
245249
});
246250
if (hookOutcome.blocked) {
247251
if (hookOutcome.kind === "veto") {

‎src/agents/pi-tools.before-tool-call.integration.e2e.test.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,46 @@ describe("before_tool_call hook deduplication (#15502)", () => {
313313
expect(beforeToolCallHook).toHaveBeenCalledTimes(1);
314314
});
315315

316+
it("passes hook context for unwrapped tool definitions", async () => {
317+
const execute = vi.fn().mockResolvedValue({ content: [], details: { ok: true } });
318+
const baseTool = { name: "exec", execute, description: "exec", parameters: {} } as any;
319+
const [def] = toToolDefinitions([baseTool], {
320+
agentId: "code-agent",
321+
sessionKey: "agent:code-agent:main",
322+
sessionId: "session-code",
323+
runId: "run-code",
324+
channelId: "channel-code",
325+
});
326+
const extensionContext = {} as Parameters<typeof def.execute>[4];
327+
328+
await def.execute(
329+
"call-code-exec",
330+
{ code: "echo hi" },
331+
undefined,
332+
undefined,
333+
extensionContext,
334+
);
335+
336+
expect(beforeToolCallHook).toHaveBeenCalledTimes(1);
337+
expect(beforeToolCallHook).toHaveBeenCalledWith(
338+
{
339+
toolName: "exec",
340+
params: { code: "echo hi" },
341+
runId: "run-code",
342+
toolCallId: "call-code-exec",
343+
},
344+
{
345+
toolName: "exec",
346+
agentId: "code-agent",
347+
sessionKey: "agent:code-agent:main",
348+
sessionId: "session-code",
349+
runId: "run-code",
350+
toolCallId: "call-code-exec",
351+
channelId: "channel-code",
352+
},
353+
);
354+
});
355+
316356
it("preserves the hook marker when abort wrapping a hooked tool", () => {
317357
const execute = vi.fn().mockResolvedValue({ content: [], details: { ok: true } });
318358
const baseTool = { name: "Bash", execute, description: "bash", parameters: {} } as any;

0 commit comments

Comments
 (0)