Repository navigation
Update client identities and WorkBuddy failover - #115
Conversation
Reviewer's GuideThis PR refreshes client identity and runtime headers across CLI and WorkBuddy profiles, then adds narrowly scoped domestic WorkBuddy origin failover that retries eligible pre-response gateway or connection failures against the official backup origin using the same account and request domain. Sequence diagram for domestic WorkBuddy origin failoversequenceDiagram
participant Client
participant API
participant Primary as WorkBuddyPrimary
participant Backup as WorkBuddyBackup
Client->>API: chat request
API->>Primary: _backend_stream(/v2/chat/completions)
alt HTTP 200 before response
Primary-->>API: response stream
API-->>Client: response
else 408/502/503/504
Primary-->>API: gateway error
API->>Backup: _backend_stream(wb.tencentbuddy.com)
Backup-->>API: response stream
API-->>Client: response
else connection failure before response
Primary--xAPI: httpx.HTTPError
API->>Backup: _backend_stream(wb.tencentbuddy.com)
Backup-->>API: response stream
API-->>Client: response
end
Flow diagram for failover eligibilityflowchart TD
A[Domestic WorkBuddy request] --> B{Primary attempt fails before response?}
B -->|No| C[Return primary response or error]
B -->|Yes| D{408, 502, 503, 504, or safe connection failure?}
D -->|No| E[Propagate failure]
D -->|Yes| F[Retry once at wb.tencentbuddy.com]
F --> G[Preserve same account and X-Domain]
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 3 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="converter.py" line_range="5046" />
<code_context>
+ yield response
+ return
+ raw = await read_bounded_error(response)
+ if should_origin_fallback(origin_url, response.status_code):
+ switch_origin(status_code=response.status_code)
+ continue
</code_context>
<issue_to_address>
**Gateway retries duplicate completions**
When the primary returns 408, 502, 503, or 504 after its backend has accepted or completed the request, the primary may already have accepted and processed the POST before returning an eligible gateway status, so `_checked_chat_stream` sends the same completion again to the backup without an upstream idempotency key; the caller receives one response while the primary and backup can both incur generation or billing.
Only replay statuses known to mean the request was not processed, or use an upstream-supported idempotency mechanism before retrying ambiguous responses.
</issue_to_address>
### Comment 2
<location path="converter.py" line_range="5046" />
<code_context>
+ yield response
+ return
+ raw = await read_bounded_error(response)
+ if should_origin_fallback(origin_url, response.status_code):
+ switch_origin(status_code=response.status_code)
+ continue
</code_context>
<issue_to_address>
**Refusals are replayed to backup**
When a selected transient HTTP status carries a recognized content-filter or no-failover business error, `_checked_chat_stream` checks the status and switches origins before `_check_upstream_status` can apply its protections, so the request is replayed instead of preserving the original refusal.
Apply the error-body protections before switching origins, and do not fail over for recognized content-filter or no-failover errors.
</issue_to_address>
### Comment 3
<location path="app/client_profiles.py" line_range="124-125" />
<code_context>
+ profile = profile_for_auth(auth)
+ headers = {**SDK_HEADERS, **_auth_headers(auth, account), **_identity_claim_headers(auth, account)}
+ if profile in ("cn-cli", "intl-cli"):
+ headers["x-stainless-package-version"] = "7.39.0"
+ headers["X-Agent-Runtime"] = "other"
+ elif profile == "intl-work":
+ headers["X-Agent-Runtime"] = "local-agent"
</code_context>
<issue_to_address>
**Profile identity differs across requests**
When a CLI or international WorkBuddy profile makes a refresh or account-lookup request, or a CLI profile makes a catalog request, `refresh_headers` omits the profile overrides, so CLI refresh and account requests use SDK version 6.25 and omit the runtime header; `catalog_headers` also omits the runtime header for CLI requests. The upstream therefore receives inconsistent client identities across the account flow.
Apply the profile-specific SDK-version and runtime overrides in the refresh and catalog header builders as well as the chat header builder.
Also at `docs/advanced.md:140`, `docs/advanced.zh-CN.md:140`.
</issue_to_address>Sourcery assessment
Needs a human reviewer. 3 findings to address first, and on eligible domestic WorkBuddy failures, the gateway sends the same request and account headers to wb.tencentbuddy.com, so a prompt or identity data already sent there cannot be taken back by reverting. Retrying transient gateway responses may also cause the upstream to process a request more than once.
Blocking findings: converter.py:5046, converter.py:5046, app/client_profiles.py:125
| yield response | ||
| return | ||
| raw = await read_bounded_error(response) | ||
| if should_origin_fallback(origin_url, response.status_code): |
There was a problem hiding this comment.
🟡 Medium · Gateway retries duplicate completions
When the primary returns 408, 502, 503, or 504 after its backend has accepted or completed the request, the primary may already have accepted and processed the POST before returning an eligible gateway status, so _checked_chat_stream sends the same completion again to the backup without an upstream idempotency key; the caller receives one response while the primary and backup can both incur generation or billing.
Only replay statuses known to mean the request was not processed, or use an upstream-supported idempotency mechanism before retrying ambiguous responses.
Prompt for AI agents
In `converter.py` at line 5046:
**Gateway retries duplicate completions**
When the primary returns 408, 502, 503, or 504 after its backend has accepted or completed the request, the primary may already have accepted and processed the POST before returning an eligible gateway status, so `_checked_chat_stream` sends the same completion again to the backup without an upstream idempotency key; the caller receives one response while the primary and backup can both incur generation or billing.
Only replay statuses known to mean the request was not processed, or use an upstream-supported idempotency mechanism before retrying ambiguous responses.| headers["x-stainless-package-version"] = "7.39.0" | ||
| headers["X-Agent-Runtime"] = "other" |
There was a problem hiding this comment.
🟡 Medium · Profile identity differs across requests
When a CLI or international WorkBuddy profile makes a refresh or account-lookup request, or a CLI profile makes a catalog request, refresh_headers omits the profile overrides, so CLI refresh and account requests use SDK version 6.25 and omit the runtime header; catalog_headers also omits the runtime header for CLI requests. The upstream therefore receives inconsistent client identities across the account flow.
Apply the profile-specific SDK-version and runtime overrides in the refresh and catalog header builders as well as the chat header builder.
Also at docs/advanced.md:140, docs/advanced.zh-CN.md:140.
Prompt for AI agents
In `app/client_profiles.py` at lines 124-125:
**Profile identity differs across requests**
When a CLI or international WorkBuddy profile makes a refresh or account-lookup request, or a CLI profile makes a catalog request, `refresh_headers` omits the profile overrides, so CLI refresh and account requests use SDK version 6.25 and omit the runtime header; `catalog_headers` also omits the runtime header for CLI requests. The upstream therefore receives inconsistent client identities across the account flow.
Apply the profile-specific SDK-version and runtime overrides in the refresh and catalog header builders as well as the chat header builder.
Also at `docs/advanced.md:140`, `docs/advanced.zh-CN.md:140`.
Summary
Validation
Summary by Sourcery
Align client identities with current upstream profiles and add safe domestic WorkBuddy backup-origin failover before responses start.
New Features:
Bug Fixes:
Enhancements:
Documentation:
Tests: