chore(deps): bump claude-agent-sdk to 0.1.58 with compat proxy enabled

Dev preview PR — combines the cli_path plumbing (#12741), the
in-process compat proxy (#12745), and the SDK bump in one branch so
we can dogfood the full upgrade end-to-end.

Changes:

* `claude-agent-sdk` -> 0.1.58 (bundled CLI 2.1.97).  Gets us all the
  blocked features:
    - `exclude_dynamic_sections` cross-user prompt cache hits
      (0.1.57) — directly amplifies #12725
    - `AssistantMessage.usage` per-turn token tracking (0.1.49) —
      cost attribution
    - `task_budget` (0.1.51) — per-task cost ceiling
    - `get_context_usage()` (0.1.52) — context window monitoring
    - MCP large-tool-result truncation fix (0.1.55)
    - MCP HTTP/SSE buffer leak fix (CLI 2.1.97) — known production
      memory creep
    - 429 retry exponential-backoff fix (CLI 2.1.97) — production
      rate-limit recovery
    - `--resume` cache miss regression fix (CLI 2.1.90)
    - SDK session quadratic-write fix (CLI 2.1.90)

* `ChatConfig.claude_agent_use_compat_proxy` default flipped from
  `False` -> `True`. The bundled CLI in 0.1.55+ injects the
  `context-management-2025-06-27` beta header which OpenRouter
  rejects (anthropics/claude-agent-sdk-python#789). The proxy strips
  it transparently. Disable explicitly only if you've pinned to a
  CLI version in `_KNOWN_GOOD_BUNDLED_CLI_VERSIONS_DIRECT`.

* `sdk_compat_test.py` pin assertion split into two known-good sets:
    - `_KNOWN_GOOD_BUNDLED_CLI_VERSIONS_DIRECT` — works without the
      proxy ({"2.1.63", "2.1.70"})
    - `_KNOWN_GOOD_BUNDLED_CLI_VERSIONS_VIA_PROXY` — works only with
      the compat proxy enabled ({"2.1.97"})
  The test now requires `claude_agent_use_compat_proxy=True` for
  proxy-only versions, so disabling the proxy on a fresh checkout
  with this PR fails fast with a clear error.

Operational rollout (when ready to ship beyond dev preview):

1. Merge #12741 (plumbing + reproduction test)
2. Merge #12745 (proxy module — opt-in default off)
3. Merge this PR (bumps SDK + flips default to on)
4. Watch production for the existing reproduction test running
   continuously as a regression guard
5. If anything goes wrong: revert this PR (proxy becomes opt-in
   again, SDK back to whichever version is in the previous merge)

Dev preview usage: deploy this branch with no env-var changes —
the proxy is on by default. The reproduction test will continue
to pass against the bundled CLI 2.1.97 when (and only when) the
proxy successfully strips the forbidden patterns.
This commit is contained in:
majdyz
2026-04-11 07:59:49 +00:00
parent 428ed39a1a
commit 5cf60587ef
4 changed files with 91 additions and 30 deletions

View File

@@ -187,21 +187,26 @@ class ChatConfig(BaseSettings):
"(same pattern as `api_key` / `base_url`).",
)
claude_agent_use_compat_proxy: bool = Field(
default=False,
default=True,
description="Run the in-process OpenRouter compatibility proxy "
"(`backend.copilot.sdk.openrouter_compat_proxy`) in front of the "
"Claude Code CLI. The proxy strips `tool_reference` content "
"blocks and the `context-management-2025-06-27` beta header / "
"field from outgoing requests so newer SDK / CLI versions stop "
"tripping OpenRouter's stricter validation. Orthogonal to "
"`claude_agent_cli_path` — the override picks the binary, the "
"proxy rewrites whatever the binary sends. Reads from "
"`CHAT_CLAUDE_AGENT_USE_COMPAT_PROXY` or the unprefixed "
"`CLAUDE_AGENT_USE_COMPAT_PROXY` environment variable (same "
"pattern as `claude_agent_cli_path`). Only takes effect when "
"the session has an Anthropic-compatible upstream to forward "
"to — direct-Anthropic sessions skip the proxy entirely to "
"avoid silently re-routing through OpenRouter.",
"tripping OpenRouter's stricter validation. Defaults to True "
"because the bundled CLI in `claude-agent-sdk >= 0.1.55` requires "
"the proxy. Orthogonal to `claude_agent_cli_path` — the override "
"picks the binary, the proxy rewrites whatever the binary sends. "
"Disable explicitly only if you've pinned `claude-agent-sdk` to "
"a version whose bundled CLI is in "
"`_KNOWN_GOOD_BUNDLED_CLI_VERSIONS_DIRECT` (2.1.63 or 2.1.70). "
"Reads from `CHAT_CLAUDE_AGENT_USE_COMPAT_PROXY` or the "
"unprefixed `CLAUDE_AGENT_USE_COMPAT_PROXY` environment "
"variable (same pattern as `claude_agent_cli_path`). Only "
"takes effect when the session has an Anthropic-compatible "
"upstream to forward to — direct-Anthropic sessions skip the "
"proxy entirely to avoid silently re-routing through "
"OpenRouter.",
)
use_openrouter: bool = Field(
default=True,

View File

@@ -232,27 +232,82 @@ def test_sdk_exports_hook_event_type(hook_event: str):
# version, so the SDK Python API surface and the CLI binary version can
# be picked independently.
# CLI versions verified to work against OpenRouter from production
# traffic. When upstream lands a fix and we can confirm a newer version
# works, add it to this set rather than blanket-removing the assertion.
_KNOWN_GOOD_BUNDLED_CLI_VERSIONS: frozenset[str] = frozenset({"2.1.63"})
# CLI versions verified to work against OpenRouter directly (no compat
# proxy required) — bisected via the reproduction test in
# `cli_openrouter_compat_test.py`. Bundled CLI versions outside this
# set are still allowed but ONLY when the compat proxy is enabled (see
# the second known-good set below + the test below).
_KNOWN_GOOD_BUNDLED_CLI_VERSIONS_DIRECT: frozenset[str] = frozenset(
{
"2.1.63", # claude-agent-sdk 0.1.45 — original pin from PR #12294.
"2.1.70", # claude-agent-sdk 0.1.47 — first version with the
# tool_reference proxy detection fix; bisect-verified
# OpenRouter-safe in #12742.
}
)
# CLI versions verified to work against OpenRouter ONLY when the
# in-process `openrouter_compat_proxy` is enabled (which strips the
# `tool_reference` content blocks and `context-management-2025-06-27`
# beta from outgoing requests). Without the proxy these CLI versions
# trip OpenRouter's stricter validation and return 400.
_KNOWN_GOOD_BUNDLED_CLI_VERSIONS_VIA_PROXY: frozenset[str] = frozenset(
{
"2.1.97", # claude-agent-sdk 0.1.58 — needs `claude_agent_use_compat_proxy=True`
# due to the upstream regression in
# anthropics/claude-agent-sdk-python#789.
}
)
# Aggregate set used by the assertion below — the test allows EITHER
# a directly-known-good CLI OR a proxy-known-good CLI when the proxy
# is enabled in the active config.
_KNOWN_GOOD_BUNDLED_CLI_VERSIONS: frozenset[str] = (
_KNOWN_GOOD_BUNDLED_CLI_VERSIONS_DIRECT | _KNOWN_GOOD_BUNDLED_CLI_VERSIONS_VIA_PROXY
)
def test_bundled_cli_version_is_known_good_against_openrouter():
"""Pin the bundled CLI version so accidental SDK bumps cause a loud,
fast failure with a pointer to the OpenRouter compatibility issue."""
fast failure with a pointer to the OpenRouter compatibility issue.
A CLI version that's only safe via the compat proxy is allowed only
when ``ChatConfig.claude_agent_use_compat_proxy`` is enabled.
"""
from claude_agent_sdk._cli_version import __cli_version__
assert __cli_version__ in _KNOWN_GOOD_BUNDLED_CLI_VERSIONS, (
from backend.copilot.config import ChatConfig
cfg = ChatConfig()
proxy_enabled = cfg.claude_agent_use_compat_proxy
if __cli_version__ in _KNOWN_GOOD_BUNDLED_CLI_VERSIONS_DIRECT:
return # safe with or without the proxy
if __cli_version__ in _KNOWN_GOOD_BUNDLED_CLI_VERSIONS_VIA_PROXY:
assert proxy_enabled, (
f"Bundled Claude Code CLI version {__cli_version__!r} is only "
"OpenRouter-safe when `claude_agent_use_compat_proxy` is "
"enabled, but the active ChatConfig has the proxy disabled. "
"Either set `COPILOT__CLAUDE_AGENT_USE_COMPAT_PROXY=true` or "
"downgrade `claude-agent-sdk` to a version whose bundled CLI "
f"is in {sorted(_KNOWN_GOOD_BUNDLED_CLI_VERSIONS_DIRECT)!r}. "
"See https://github.com/anthropics/claude-agent-sdk-python/issues/789."
)
return
raise AssertionError(
f"Bundled Claude Code CLI version is {__cli_version__!r}, which is "
f"not in the OpenRouter-known-good set "
f"{sorted(_KNOWN_GOOD_BUNDLED_CLI_VERSIONS)!r}. "
f"not in any OpenRouter-known-good set "
f"({sorted(_KNOWN_GOOD_BUNDLED_CLI_VERSIONS)!r}). "
"If you intentionally bumped `claude-agent-sdk`, verify the new "
"bundled CLI works with OpenRouter against the reproduction test "
"in `cli_openrouter_compat_test.py`, then add the new CLI version "
"to `_KNOWN_GOOD_BUNDLED_CLI_VERSIONS`. If you cannot make the "
"bundled CLI work, set `claude_agent_cli_path` to a known-good "
"binary instead and skip the bundled one. See "
"to either `_KNOWN_GOOD_BUNDLED_CLI_VERSIONS_DIRECT` (works "
"without the proxy) or `_KNOWN_GOOD_BUNDLED_CLI_VERSIONS_VIA_PROXY` "
"(works only with `claude_agent_use_compat_proxy=true`). If you "
"cannot make the bundled CLI work either way, set "
"`claude_agent_cli_path` to a known-good binary instead. See "
"https://github.com/anthropics/claude-agent-sdk-python/issues/789 "
"and https://github.com/Significant-Gravitas/AutoGPT/pull/12294."
)

View File

@@ -1,4 +1,4 @@
# This file is automatically @generated by Poetry 2.2.1 and should not be changed by hand.
# This file is automatically @generated by Poetry 2.1.4 and should not be changed by hand.
[[package]]
name = "agentmail"
@@ -909,17 +909,18 @@ files = [
[[package]]
name = "claude-agent-sdk"
version = "0.1.45"
version = "0.1.58"
description = "Python SDK for Claude Code"
optional = false
python-versions = ">=3.10"
groups = ["main"]
files = [
{file = "claude_agent_sdk-0.1.45-py3-none-macosx_11_0_arm64.whl", hash = "sha256:26a5cc60c3a394f5b814f6b2f67650819cbcd38c405bbdc11582b3e097b3a770"},
{file = "claude_agent_sdk-0.1.45-py3-none-manylinux_2_17_aarch64.whl", hash = "sha256:decc741b53e0b2c10a64fd84c15acca1102077d9f99941c54905172cd95160c9"},
{file = "claude_agent_sdk-0.1.45-py3-none-manylinux_2_17_x86_64.whl", hash = "sha256:7d48dcf4178c704e4ccbf3f1f4ebf20b3de3f03d0592086c1f3abd16b8ca441e"},
{file = "claude_agent_sdk-0.1.45-py3-none-win_amd64.whl", hash = "sha256:d1cf34995109c513d8daabcae7208edc260b553b53462a9ac06a7c40e240a288"},
{file = "claude_agent_sdk-0.1.45.tar.gz", hash = "sha256:97c1e981431b5af1e08c34731906ab8d4a58fe0774a04df0ea9587dcabc85151"},
{file = "claude_agent_sdk-0.1.58-py3-none-macosx_11_0_arm64.whl", hash = "sha256:69197950809754c4f06bba8261f2d99c3f9605b6cc1c13d3409d0eb82fb4ee64"},
{file = "claude_agent_sdk-0.1.58-py3-none-macosx_11_0_x86_64.whl", hash = "sha256:75d60883fc5e2070bccd8d9b19505fe16af8e049120c03821e9dc8c826cca434"},
{file = "claude_agent_sdk-0.1.58-py3-none-manylinux_2_17_aarch64.whl", hash = "sha256:7bf4eb0f00ec944a7b63eb94788f120dfb0460c348a525235c7d6641805acc1d"},
{file = "claude_agent_sdk-0.1.58-py3-none-manylinux_2_17_x86_64.whl", hash = "sha256:650d298a3d3c0dcdde4b5f1dbf52f472ff0b0ec82987b27ffa2a4e0e72928408"},
{file = "claude_agent_sdk-0.1.58-py3-none-win_amd64.whl", hash = "sha256:2c2130a7ffe06ed4f88d56b217a5091c91c9bcb1a69cfd94d5dcf0d2946d8c55"},
{file = "claude_agent_sdk-0.1.58.tar.gz", hash = "sha256:77bee8fd60be033cb870def46c2ab1625a512fa8a3de4ff8d766664ffb16d6a6"},
]
[package.dependencies]
@@ -8928,4 +8929,4 @@ cffi = ["cffi (>=1.17,<2.0) ; platform_python_implementation != \"PyPy\" and pyt
[metadata]
lock-version = "2.1"
python-versions = ">=3.10,<3.14"
content-hash = "da61798b73758b9292fc1933268d488fbe739dc1fbf5c6586cd0c76a3411eb2e"
content-hash = "c4cc6a0a26869a167ce182b178224554135d89d8ffa4605257d17b3f495cdf59"

View File

@@ -18,7 +18,7 @@ apscheduler = "^3.11.1"
autogpt-libs = { path = "../autogpt_libs", develop = true }
bleach = { extras = ["css"], version = "^6.2.0" }
cachetools = "^5.5.0"
claude-agent-sdk = "0.1.45" # see copilot/sdk/sdk_compat_test.py for capability checks
claude-agent-sdk = "0.1.58" # latest stable; bundled CLI 2.1.97 ships the broken context-management beta and REQUIRES the openrouter_compat_proxy. See sdk_compat_test.py.
click = "^8.2.0"
cryptography = "^46.0"
discord-py = "^2.5.2"