Skip to content

fix(ollama): normalize prefixed tool-call names before Ollama dispatch - #74499

Closed
afurm wants to merge 2 commits into
openclaw:mainfrom
afurm:af/normalize-ollama-tool-call-names
Closed

afurm wants to merge 2 commits into
openclaw:mainfrom
afurm:af/normalize-ollama-tool-call-names

Conversation

@afurm

@afurm afurm commented Apr 29, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Problem:
    • Ollama adapter was sending/handling tool call names like functions.exec or tools.exec without normalization, so tool matching could fail in downstream dispatch.
  • Why it matters:
    • Tool calls from assistant/history/round-trip data could be rejected or misrouted, causing real tool execution failures in tool-using chats.
  • What changed:
    • Added normalizeOllamaToolCallName in extensions/ollama/src/stream.ts.
    • Applied normalization when converting assistant content to Ollama tool calls (convertToOllamaMessages path via extractToolCalls).
    • Applied normalization when rebuilding assistant messages from Ollama responses (buildAssistantMessage) so SDK-facing tool calls stay canonical.
    • Added two regression tests in extensions/ollama/src/stream-runtime.test.ts for both directions of normalization.
  • What did NOT change (scope boundary):
    • No schema/API changes, no provider-level behavior changes outside Ollama extension, no model/provider config changes, no security surface changes.

Change Type

  • Bug fix
  • Feature
  • Refactor required for the fix
  • Docs
  • Security hardening
  • Chore/infra

Scope

  • Gateway / orchestration
  • Skills / tool execution
  • Auth / tokens
  • Memory / storage
  • Integrations
  • API / contracts
  • UI / DX
  • CI/CD / infra

Linked Issue/PR

Root Cause

  • Root cause:
    • Tool names coming in as legacy/provider-prefixed forms (functions.*, tools.*) were passed through unchanged in Ollama-adapter conversion paths.
  • Missing detection / guardrail:
    • No shared normalization guard existed in the Ollama request/response conversion boundary for tool-call names.
  • Contributing context (if known):
    • Observed during investigation of tool-call dispatch failures across Ollama/Kimi-style prefixed tool names.

Regression Test Plan

  • Coverage level that should have caught this:
    • Unit test
    • Seam / integration test
    • End-to-end test
    • Existing coverage already sufficient
  • Target test or file:
    • extensions/ollama/src/stream-runtime.test.ts
  • Scenario the test should lock in:
    • functions.exec / tools.exec tool-call names are normalized to exec in both outgoing Ollama messages and incoming tool-call reconstruction.
  • Why this is the smallest reliable guardrail:
    • It directly verifies the exact normalization boundary where the defect appears.
  • Existing test that already covers this (if any):
    • N/A (new regression coverage added)
  • If no new test is added, why not:
    • N/A

User-visible / Behavior Changes

  • Tool-call names with functions.* / tools.* prefixes now normalize to canonical names during Ollama conversion.
  • No user-facing UI changes.
  • No defaults/configuration changes.

Diagram

Before:
[user assistant toolCall (functions.exec)] -> [Ollama request conversion sends "functions.exec"] -> [tool lookup mismatch / no-op execution]

After:
[user assistant toolCall (functions.exec)] -> [normalize name -> "exec"] -> [Ollama conversion and SDK tool-call reconstruction use "exec"] -> [tool executes normally]

Security Impact

  • New permissions/capabilities? (No)
  • Secrets/tokens handling changed? (No)
  • New/changed network calls? (No)
  • Command/tool execution surface changed? (No changes in behavior, only name normalization)
  • Data access scope changed? (No)
  • If any Yes, explain risk + mitigation:
    • N/A

Repro + Verification

Environment

  • OS: macOS
  • Runtime/container: Node.js (repo runtime)
  • Model/provider: Ollama / Kimi/GLM-compatible endpoints
  • Integration/channel (if any): Tool execution flow in OpenClaw assistant runtime (Ollama extension)
  • Relevant config (redacted): standard local config

Steps

  1. Trigger assistant tool-call with prefixed names (functions.exec, tools.exec) from historical/Anthropic-compatible tool-call payloads.
  2. Route through Ollama stream path and check emitted Ollama payloads/tool-call reconstruction.
  3. Run focused tests for extensions/ollama/src/stream-runtime.test.ts (planned).

Expected

  • Prefixed tool names are converted to canonical names before Ollama interaction.

Actual

  • Implemented and covered by unit tests.

Evidence

  • Failing test/log before + passing after
  • Trace/log snippets
  • Screenshot/recording
  • Perf numbers (if relevant)

Human Verification (required)

  • Verified scenarios:
    • Read-through code-path inspection for outbound (convertToOllamaMessages) and inbound (buildAssistantMessage) tool-call conversions.
    • Added/validated focused regression tests for both sides.
  • Edge cases checked:
    • empty/standard tool names, prefixed names.
  • What you did not verify:
    • Full live runtime smoke run in a running OpenClaw instance (not executed in this pass).

Review Conversations

  • I replied to or resolved every bot review conversation I addressed in this PR.
  • I left unresolved only the conversations that still need reviewer or maintainer judgment.

Compatibility / Migration

  • Backward compatible? (Yes)
  • Config/env changes? (No)
  • Migration needed? (No)
  • If yes, exact upgrade steps:
    • N/A

Risks and Mitigations

  • Risk:
    • Rare edge case where a tool name intentionally starts with functions or tools as a literal prefix without separator.
  • Mitigation:
    • Prefix stripping only applies when followed by ., /, _, or - and only in Ollama conversion paths; normal non-prefixed names are unchanged.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4b3fc266cc

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread extensions/ollama/src/stream.ts Resolved
@clawsweeper

clawsweeper Bot commented Apr 29, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: found issues before merge.

What this changes:

The PR adds Ollama-local normalization for functions.*/tools.* tool-call names in stream conversion and assistant-message reconstruction, plus focused regression tests for prefixed and non-prefixed names.

Maintainer follow-up before merge:

This is an active contributor PR with a concrete one-line correctness finding; the right next action is author or maintainer amendment on the PR branch, not an automated replacement branch.

Security review:

Security review cleared: The diff only changes Ollama stream name normalization and tests; it does not add dependencies, workflows, secrets handling, permissions, downloads, or lifecycle scripts.

Review findings:

  • [P2] Consume the prefix separator when normalizing tool names — extensions/ollama/src/stream.ts:598-599
Review details

Best possible solution:

Amend the Ollama-local normalizer to consume the prefix separator while preserving names such as functionshell, keep the new adapter-boundary tests, and then run the focused Ollama and runner normalization tests before deciding whether this PR fully resolves #74487.

Full review comments:

  • [P2] Consume the prefix separator when normalizing tool names — extensions/ollama/src/stream.ts:598-599
    The lookahead verifies that functions or tools is followed by a separator, but the separator is not removed. As written, functions.exec normalizes to .exec and tools/exec normalizes to /exec, so the added tests expecting exec fail and dispatch still misses canonical tool names. Consume the delimiter with the prefix or strip it immediately after matching.
    Confidence: 0.97

Overall correctness: patch is incorrect
Overall confidence: 0.91

Acceptance criteria:

  • pnpm test extensions/ollama/src/stream-runtime.test.ts src/agents/pi-embedded-runner/run/attempt.test.ts
  • pnpm exec oxfmt --check --threads=1 extensions/ollama/src/stream.ts extensions/ollama/src/stream-runtime.test.ts

What I checked:

  • PR normalizer leaves delimiter behind: The branch changes the prefix regex to look ahead for a separator without consuming it; direct evaluation maps functions.exec to .exec and tools/exec to /exec, contradicting the new tests and intended dispatch name. (extensions/ollama/src/stream.ts:598, 576603a946c4)
  • Current main has no Ollama-local tool-name normalization: On current main, extractToolCalls forwards part.name directly and buildAssistantMessage forwards toolCall.function.name directly, so the branch is targeting a real uncovered Ollama boundary rather than an already-implemented local helper. (extensions/ollama/src/stream.ts:769, 6e5a703dd2db)
  • Existing runner normalization overlaps but is not the same boundary: Current main already has runner-side normalization for allowed tool names, with tests for functions.read, functions.write, and tools/exec; the PR adds provider adapter conversion coverage before/after Ollama serialization. (src/agents/pi-embedded-runner/run/attempt.test.ts:883, 6e5a703dd2db)
  • Transcript hygiene docs place shared repair in the runner: The transcript hygiene reference says provider-specific transcript sanitization is centralized in the embedded runner, which explains the overlap with existing generic normalization and why an Ollama adapter-specific change should stay narrowly justified by the conversion boundary. Public docs: docs/reference/transcript-hygiene.md. (docs/reference/transcript-hygiene.md:23, 6e5a703dd2db)

Likely related people:

  • steipete: Recent history and patch metadata show repeated maintenance in the Ollama stream and regression-test files, including abort handling and tool-call argument parsing near this code path. (role: recent maintainer; confidence: high; commits: 9d03cd15a9a0, a31342ab6b57, acfa9877b32a; files: extensions/ollama/src/stream.ts, extensions/ollama/src/stream-runtime.test.ts)
  • vincentkoc: Authored the merged runner-side normalization work for malformed and provider-prefixed tool-call names before dispatch, which overlaps with the behavior this PR is adapting at the Ollama boundary. (role: introduced related behavior; confidence: medium; commits: 69a6c0a9dd88; files: src/agents/pi-embedded-runner/run/attempt.tool-call-normalization.ts, src/agents/pi-embedded-runner/run/attempt.test.ts)

Remaining risk / open question:

  • The linked Telegram/Ollama report may also involve channel-session tool registration or runtime context, so delimiter-correct normalization may be necessary but not sufficient for the whole user-visible failure.

Codex review notes: model gpt-5.5, reasoning high; reviewed against 6e5a703dd2db.

@steipete

Copy link
Copy Markdown
Contributor

Thanks @afurm. I landed the maintainer version on main in 89f8716 and am closing this PR as superseded.

The landed patch keeps your Ollama adapter-boundary fix, but consumes the prefix separator as ClawSweeper noted, and adds extra guard coverage for non-prefixed names so we do not rewrite functionshell, tooling, or bare tools. It also includes live Ollama/Kimi verification through a real openclaw agent --local tool call.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Ollama Kimi tool call fails with "Tool exec not found" despite /tools showing exec

2 participants