Skip to content

Custom OpenAI-compatible provider profiles are silently routed through the OpenRouter runtime, inheriting its orphaned-tool-output drop bug #908

Closed

Description

@uzunenes

Summary

Any provider configured in config.toml as type = "open-ai-compatible" (or a named profile) — e.g. a self-hosted LM Studio/Ollama/vLLM/SGLang endpoint — is routed through ActiveProvider::OpenRouter (crates/jcode-base/src/provider/selection.rs:29-31), not the native OpenAI provider. This means it inherits the OpenRouter runtime's build_chat_messages logic, including a bug where orphaned tool-call outputs (a ToolResult whose matching ToolUse was pruned, typically by compaction) are silently dropped instead of recovered.

Routing chain (verified)

  • crates/jcode-base/src/provider/selection.rs:29-31: OpenAiCompatibleProfile(_) | NamedProfile(_) => ActiveProvider::OpenRouter.
  • crates/jcode-base/src/provider/openrouter.rs:2-9,73: OpenRouter runtime is documented as the generic transport that "Direct OpenAI-compatible endpoints reuse".
  • src/cli/startup.rs:164-190: register_openrouter_factory maps compatible/named profiles to OpenRouterProvider.
  • crates/jcode-provider-openrouter-runtime/src/openrouter_provider_impl.rs:97: calls jcode_provider_openrouter::request::build_chat_messages.
  • crates/jcode-base/src/provider_catalog.rs:573-632: writes the custom base_url into JCODE_OPENROUTER_API_BASE.
  • Only the native built-in openai-api provider (Responses API, /responses) uses the OpenAI runtime's orphan-recovery path (crates/jcode-provider-openai/src/request.rs:338-357).

The underlying bug

In crates/jcode-provider-openrouter/src/request.rs:303-305, leftover pending_tool_results with no matching ToolUse are folded into skipped_results and discarded (log: [openrouter] Filtered {} orphaned tool result(s)... at line 315) — the content is never re-encoded into any message. By contrast, crates/jcode-provider-openai/src/request.rs:74-98,338-357 recovers the same situation as a user message: "[Recovered orphaned tool output: {}]\n{}".

Reproduction

A minimal test against build_chat_messages in jcode-provider-openrouter, feeding a single orphaned ToolResult{tool_use_id: "call_orphan", content: "orphan result"}, produces an empty message list — the content exists nowhere in the output request. The equivalent OpenAI-provider test recovers it correctly.

Impact

Because compaction (jcode-base/src/compaction.rs) can prune an assistant ToolUse while leaving its paired ToolResult, any long session against a self-hosted/custom OpenAI-compatible endpoint can silently lose tool-result content mid-task. The agent then appears to stall or end its turn prematurely with no memory of the tool result it just requested, requiring the user to manually say "continue." This affects every open-ai-compatible/named-profile user, not just people who deliberately choose openrouter.ai.

Suggested fix

In crates/jcode-provider-openrouter/src/request.rs, at line ~303, re-encode leftover pending_tool_results as {"role":"user","content":"[Recovered orphaned tool output: ...]"} messages (OpenAI chat-completions format) instead of discarding them into skipped_results, mirroring orphan_tool_output_to_user_message() from the OpenAI provider. Leave the separate duplicate-output drop at lines 507-513 unchanged, as that path is already correct.

Checked for duplicates

Searched issues/PRs for 'orphaned tool output', 'orphan', 're-ordering', 'dropped tool output', 'compatible profile' — no existing report covers this exact routing + drop combination.

Activity

  1. added
    bug
    priority: high
    triage: reproducible
    autonomous: clear
    on Aug 13, 2026
  2. 1jehuang commented on Aug 13, 2026

    @1jehuang
    Owner

    I reproduced the orphaned-tool-output drop and added recovery matching the OpenAI runtime, with regression coverage. The full OpenRouter suite passes (28 tests). Fix: #918.

    --- — Jcode agent (automated triage), on behalf of @1jehuang

  3. added
    triage: fixed-pending-release
    on Aug 13, 2026
  4. added a commit that references this issue on Aug 13, 2026
    2c0c350
  5. added a commit that references this issue on Aug 15, 2026
    a8e0afd
  6. added a commit that references this issue on Aug 28, 2026
    877e57f
  7. added 5 commits that reference this issue on Aug 30, 2026
    7f9b092
    9343b8b
    5d9edab
    35b8ef8
    78fe634
  8. added a commit that references this issue on Sep 18, 2026
    90a9910
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    autonomous: clearbugpriority: hightriage: fixed-pending-releasetriage: reproducible

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions