mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-11-17 23:32:30 +08:00
fix(claude): preserve beta profiles across thread turns
- Reuse successful wire beta snapshots across thread turns and executor reloads. - Keep continuations on their original credential and honor explicit payload/header policies. - Recover snapshots through KV and publish state before streaming completion, with regression coverage. Closes: #7470 Co-authored-by: Dylan <1990016@gmail.com> Co-authored-by: lzt404 <2596933790@qq.com>
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -19,6 +20,7 @@ import (
|
||||
"github.com/router-for-me/CLIProxyAPI/v8/internal/util"
|
||||
cliproxyauth "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/auth"
|
||||
cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/executor"
|
||||
cliproxysession "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/session"
|
||||
sdktranslator "github.com/router-for-me/CLIProxyAPI/v8/sdk/translator"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
@@ -34,6 +36,7 @@ type ClaudeExecutor struct {
|
||||
// oauthToolAliases is shared by every ForAPIKey copy of this executor;
|
||||
// constructors allocate it so copies made per request reuse one store.
|
||||
oauthToolAliases *claudeOAuthToolAliasStore
|
||||
threadBetas *helps.ClaudeThreadBetaStore
|
||||
}
|
||||
|
||||
type claudeOAuthCancellationError struct {
|
||||
@@ -342,7 +345,104 @@ func stripTranslatedDefaultMaxTokens(from sdktranslator.Format, originalPayload,
|
||||
}
|
||||
|
||||
func NewClaudeExecutor(cfg *config.Config) *ClaudeExecutor {
|
||||
return &ClaudeExecutor{cfg: cfg, oauthToolAliases: &claudeOAuthToolAliasStore{}}
|
||||
return &ClaudeExecutor{cfg: cfg, oauthToolAliases: &claudeOAuthToolAliasStore{}, threadBetas: &helps.ClaudeThreadBetaStore{}}
|
||||
}
|
||||
|
||||
func (e *ClaudeExecutor) prepareClaudeThreadBetas(ctx context.Context, request *http.Request, auth *cliproxyauth.Auth, body []byte, extraBetas []string, payloadBetasConfigured bool, incomingHeaders http.Header, uncloak bool, metadataSets ...map[string]any) (*helps.ClaudeThreadBetaRequest, error) {
|
||||
if !ojson.GetBytesNoCopy(body, "thread").IsObject() {
|
||||
return nil, nil
|
||||
}
|
||||
apiKey, _ := claudeCreds(auth)
|
||||
fp := resolveClaudeFingerprintPolicy(e.cfg, auth, apiKey)
|
||||
identity := claudeDiagnosticsCredentialIdentity(auth)
|
||||
if identity == "" {
|
||||
identity = apiKey
|
||||
}
|
||||
caller := resolveClaudeMCPAliasOptions(ctx, metadataSets...).secret
|
||||
scope := sha256.Sum256([]byte(e.upstreamRequestLogProvider() + "\x00" + request.URL.Scheme + "://" + request.URL.Host + request.URL.Path + "\x00" + identity + "\x00" + caller))
|
||||
var hd config.ClaudeHeaderDefaults
|
||||
if e.cfg != nil {
|
||||
hd = e.cfg.ClaudeHeaderDefaults
|
||||
}
|
||||
var customBetas []string
|
||||
betaAttrs := make(map[string]string)
|
||||
if auth != nil && !isAnthropicUpstreamURL(request.URL) {
|
||||
for name, value := range auth.Attributes {
|
||||
if strings.EqualFold(name, "header:Anthropic-Beta") {
|
||||
customBetas = append(customBetas, value)
|
||||
betaAttrs[name] = value
|
||||
}
|
||||
}
|
||||
}
|
||||
sort.Strings(customBetas)
|
||||
customBetaApplied := false
|
||||
if len(customBetas) > 0 {
|
||||
probe := (&http.Request{Header: make(http.Header)}).WithContext(request.Context())
|
||||
util.ApplyCustomHeadersFromAttrs(probe, betaAttrs, incomingHeaders)
|
||||
resolved := helps.HeaderValuesCaseInsensitive(probe.Header, "Anthropic-Beta")
|
||||
customBetaApplied = len(resolved) > 0
|
||||
customBetas = append(customBetas, strings.Join(resolved, ","))
|
||||
}
|
||||
policy := sha256.Sum256([]byte(fmt.Sprintf("%q\x00%q\x00%q\x00%t\x00%t", hd.AnthropicBetaStrip, hd.AnthropicBetaAppend, customBetas, uncloak, fp.UseOAuthBetas)))
|
||||
// Literal executors need the same lazy-allocation protection as alias state.
|
||||
claudeOAuthToolAliasStoreInitMu.Lock()
|
||||
if e.threadBetas == nil {
|
||||
e.threadBetas = &helps.ClaudeThreadBetaStore{}
|
||||
}
|
||||
store := e.threadBetas
|
||||
claudeOAuthToolAliasStoreInitMu.Unlock()
|
||||
// Track only body declarations that actually contribute independent flags.
|
||||
independent := claudeRequestedBetas(strings.Join(helps.HeaderValuesCaseInsensitive(incomingHeaders, "Anthropic-Beta"), ","), hd.AnthropicBetaAppend)
|
||||
actual := claudeRequestedBetas(strings.Join(helps.HeaderValuesCaseInsensitive(request.Header, "Anthropic-Beta"), ","), nil)
|
||||
var bodyBetas []string
|
||||
for _, beta := range extraBetas {
|
||||
beta = strings.TrimSpace(beta)
|
||||
credentialBeta := fp.UseOAuthBetas && (beta == claudeOAuthBeta || beta == claudeExtendedCacheTTLBeta)
|
||||
if !customBetaApplied && actual[beta] && !independent[beta] && !credentialBeta {
|
||||
bodyBetas = append(bodyBetas, beta)
|
||||
independent[beta] = true
|
||||
}
|
||||
}
|
||||
state, found, errBind := store.Bind(string(scope[:]), ojson.GetBytesNoCopy(body, "thread.previous_message_id").String(), request.Header, helps.ClaudeThreadBetaPolicy{Fingerprint: hex.EncodeToString(policy[:]), BodyBetas: strings.Join(bodyBetas, ","), PayloadConfigured: payloadBetasConfigured})
|
||||
if errBind != nil {
|
||||
return nil, errBind
|
||||
}
|
||||
if !found {
|
||||
return nil, helps.ClaudeThreadBetaConflictError{}
|
||||
}
|
||||
// The restored profile must satisfy this final business payload on every path.
|
||||
declared := claudeRequestedBetas(strings.Join(helps.HeaderValuesCaseInsensitive(request.Header, "Anthropic-Beta"), ","), nil)
|
||||
for beta, needed := range map[string]bool{
|
||||
claudeFastModeBeta: strings.EqualFold(ojson.GetBytesNoCopy(body, "speed").String(), "fast"),
|
||||
claudeAdvisorToolBeta: claudeBodyHasAdvisorTool(body),
|
||||
claudeThinkingBindingBeta: ojson.GetBytesNoCopy(body, "thinking.block_binding").Exists(),
|
||||
claudeExtendedCacheTTLBeta: helps.ClaudePayloadHas1hTTL(body),
|
||||
} {
|
||||
if needed && !declared[beta] {
|
||||
return nil, helps.ClaudeThreadBetaConflictError{}
|
||||
}
|
||||
}
|
||||
selectedAuthID := ""
|
||||
for _, metadata := range metadataSets {
|
||||
if selectedAuthID = metadataString(metadata, cliproxyexecutor.SelectedAuthMetadataKey); selectedAuthID != "" {
|
||||
break
|
||||
}
|
||||
}
|
||||
if auth != nil && auth.ID != "" && selectedAuthID == auth.ID && e.upstreamRequestLogProvider() == "claude" {
|
||||
ownerScope := cliproxysession.ClaudeThreadCallerScope(helps.APIKeyFromContext(ctx), metadataSets...)
|
||||
authID := strings.Clone(auth.ID)
|
||||
state.OnCommit(func(messageID string) {
|
||||
cliproxysession.RememberClaudeThreadAuth(ownerScope, messageID, authID)
|
||||
})
|
||||
}
|
||||
return state, nil
|
||||
}
|
||||
|
||||
// WithConfig rebinds configuration without discarding successful Thread state.
|
||||
// Like ForAPIKey, the copy shares pointer-owned stores and copies no mutexes.
|
||||
func (e ClaudeExecutor) WithConfig(cfg *config.Config) *ClaudeExecutor {
|
||||
e.cfg = cfg
|
||||
return &e
|
||||
}
|
||||
|
||||
func (e *ClaudeExecutor) Identifier() string { return "claude" }
|
||||
|
||||
@@ -252,7 +252,7 @@ func (e *ClaudeExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, r
|
||||
touchedPayloadPaths := helps.ApplyPayloadConfigWithTrackedPathsDoc(
|
||||
e.cfg, baseModel, to.String(), from.String(), "", doc, stripTranslatedDefaultMaxTokens(from, originalPayload, originalTranslated),
|
||||
helps.PayloadRequestedModel(opts, req.Model), helps.PayloadRequestPath(opts), opts.Headers,
|
||||
"diagnostics",
|
||||
"diagnostics", "betas",
|
||||
)
|
||||
if touchedPayloadPaths["diagnostics"] {
|
||||
diagnosticsState = claudeDiagnosticsRequestState{}
|
||||
@@ -292,6 +292,10 @@ func (e *ClaudeExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, r
|
||||
); errHeaders != nil {
|
||||
return resp, errHeaders
|
||||
}
|
||||
threadBetas, errThread := e.prepareClaudeThreadBetas(ctx, httpReq, auth, bodyForUpstream, extraBetas, touchedPayloadPaths["betas"], incomingHeaders, wirePolicy.Uncloak, opts.Metadata, req.Metadata)
|
||||
if errThread != nil {
|
||||
return resp, errThread
|
||||
}
|
||||
fastRequest := isAnthropicUpstreamBase(baseURL) && claudeRequestIsFast(httpReq, bodyForUpstream)
|
||||
authID, authLabel, authType, authValue := claudeAuthLogIdentity(auth)
|
||||
helps.RecordAPIRequest(ctx, e.cfg, helps.UpstreamRequestLog{
|
||||
@@ -368,12 +372,14 @@ func (e *ClaudeExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, r
|
||||
return resp, wrapClaudeFastRequestError(fastRequest, httpResp.StatusCode, err)
|
||||
}
|
||||
var streamUsage helps.StreamUsageBuffer
|
||||
threadMessageID := ""
|
||||
if upstreamStream {
|
||||
if errValidate := validateClaudeStreamingResponse(data); errValidate != nil {
|
||||
helps.RecordAPIResponseError(ctx, e.cfg, errValidate)
|
||||
return resp, wrapClaudeFastRequestError(fastRequest, httpResp.StatusCode, errValidate)
|
||||
}
|
||||
if msgID := claudeMessageIDFromSSE(data); msgID != "" {
|
||||
threadMessageID = claudeMessageIDFromSSE(data)
|
||||
if msgID := threadMessageID; msgID != "" {
|
||||
commitClaudeContinuity(diagnosticsState, msgID, helps.HeaderValueCaseInsensitive(httpResp.Header, "request-id"))
|
||||
e.rememberClaudeOAuthToolAliases(body, oauthToolNamesReverseMap, msgID)
|
||||
}
|
||||
@@ -415,6 +421,7 @@ func (e *ClaudeExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, r
|
||||
data = bytes.Join(lines, []byte("\n"))
|
||||
} else {
|
||||
messageID := claudeMessageIDFromResponse(data)
|
||||
threadMessageID = messageID
|
||||
commitClaudeContinuity(diagnosticsState, messageID, helps.HeaderValueCaseInsensitive(httpResp.Header, "request-id"))
|
||||
e.rememberClaudeOAuthToolAliases(body, oauthToolNamesReverseMap, messageID)
|
||||
reporter.ObserveResponseModel(data)
|
||||
@@ -457,6 +464,7 @@ func (e *ClaudeExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, r
|
||||
if responseFormat == sdktranslator.FormatOpenAIResponse {
|
||||
out = helps.EnsureResponsesUsageDetails(out)
|
||||
}
|
||||
threadBetas.Commit(threadMessageID)
|
||||
resp = cliproxyexecutor.Response{Payload: out, Headers: httpResp.Header.Clone()}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
@@ -1255,6 +1255,7 @@ func applyClaudeHeadersWithNativeProfile(
|
||||
|
||||
incomingBetas := strings.TrimSpace(strings.Join(helps.HeaderValuesCaseInsensitive(incomingHeaders, "Anthropic-Beta"), ","))
|
||||
countTokens := r.URL != nil && strings.HasSuffix(r.URL.Path, "/count_tokens")
|
||||
preserveThreadCapabilities := !countTokens && (incomingBetas != "" || len(extraBetas) > 0) && ojson.GetBytesNoCopy(body, "thread").IsObject()
|
||||
if !confirmedClaudeCode && !wirePolicy.Cloak && wirePolicy.ExplicitNever && !wirePolicy.Uncloak {
|
||||
det := helps.DetectClaudeCodeRequest(incomingHeaders, body, countTokens, cfg)
|
||||
if det.ConfirmedShape {
|
||||
@@ -1363,6 +1364,25 @@ func applyClaudeHeadersWithNativeProfile(
|
||||
}
|
||||
}
|
||||
}
|
||||
// A Thread declares capabilities for all turns, not just its first body.
|
||||
// Preserve them before binding the final emitted profile to its response ID.
|
||||
if preserveThreadCapabilities {
|
||||
threadCapabilities := incomingBetas
|
||||
if useOAuthBetas {
|
||||
threadCapabilities = withClaudeOAuthCredentialBetas(threadCapabilities, !wirePolicy.Uncloak)
|
||||
}
|
||||
for _, beta := range extraBetas {
|
||||
threadCapabilities = withClaudeBetaAppended(threadCapabilities, beta)
|
||||
}
|
||||
// Keep required protocol additions after the client-declared capabilities.
|
||||
for _, beta := range strings.Split(baseBetas, ",") {
|
||||
threadCapabilities = withClaudeBetaAppended(threadCapabilities, beta)
|
||||
}
|
||||
if advisorNeeded {
|
||||
threadCapabilities = withClaudeAdvisorToolBeta(threadCapabilities)
|
||||
}
|
||||
baseBetas = threadCapabilities
|
||||
}
|
||||
applyBetaHeader := func() {
|
||||
if wirePolicy.Uncloak {
|
||||
baseBetas = withoutClaudeBeta(baseBetas, claudeCodeBeta)
|
||||
@@ -1379,30 +1399,31 @@ func applyClaudeHeadersWithNativeProfile(
|
||||
baseBetas = withoutClaudeBeta(baseBetas, claudeExtendedCacheTTLBeta)
|
||||
}
|
||||
}
|
||||
// Enforce strict native Claude Code 2.1.321 model & turn beta gating:
|
||||
if !claudeRequestSupportsEffort(body, nil) {
|
||||
baseBetas = withoutClaudeBeta(baseBetas, claudeEffortBeta)
|
||||
}
|
||||
reqProbeOrHelper := helps.IsClaudeProbeOrHelperRequest(body)
|
||||
if reqProbeOrHelper && !helperProfile {
|
||||
baseBetas = withoutClaudeBeta(baseBetas, claudeServerSideFallbackBeta)
|
||||
baseBetas = withoutClaudeBeta(baseBetas, claudeThinkingDisplayUpdatesBeta)
|
||||
baseBetas = withoutClaudeBeta(baseBetas, claudeExtendedCacheTTLBeta)
|
||||
}
|
||||
reqThinkingType := ojson.GetBytesNoCopy(body, "thinking.type").String()
|
||||
if reqThinkingType == "disabled" {
|
||||
baseBetas = withoutClaudeBeta(baseBetas, claudeThinkingDisplayUpdatesBeta)
|
||||
}
|
||||
if helps.IsClaudeSubagentRequest(incomingHeaders, body) && !helps.ClaudeSubagentRequests1h(incomingHeaders, body) {
|
||||
baseBetas = withoutClaudeBeta(baseBetas, claudeExtendedCacheTTLBeta)
|
||||
if !preserveThreadCapabilities {
|
||||
// Per-turn gating remains unchanged outside caller-declared Threads.
|
||||
if !claudeRequestSupportsEffort(body, nil) {
|
||||
baseBetas = withoutClaudeBeta(baseBetas, claudeEffortBeta)
|
||||
}
|
||||
if reqProbeOrHelper && !helperProfile {
|
||||
baseBetas = withoutClaudeBeta(baseBetas, claudeServerSideFallbackBeta)
|
||||
baseBetas = withoutClaudeBeta(baseBetas, claudeThinkingDisplayUpdatesBeta)
|
||||
baseBetas = withoutClaudeBeta(baseBetas, claudeExtendedCacheTTLBeta)
|
||||
}
|
||||
if ojson.GetBytesNoCopy(body, "thinking.type").String() == "disabled" {
|
||||
baseBetas = withoutClaudeBeta(baseBetas, claudeThinkingDisplayUpdatesBeta)
|
||||
}
|
||||
if helps.IsClaudeSubagentRequest(incomingHeaders, body) && !helps.ClaudeSubagentRequests1h(incomingHeaders, body) {
|
||||
baseBetas = withoutClaudeBeta(baseBetas, claudeExtendedCacheTTLBeta)
|
||||
}
|
||||
reqModel := strings.ToLower(strings.TrimSpace(ojson.GetBytesNoCopy(body, "model").String()))
|
||||
if isClaudeHaikuModel(reqModel) && !ojson.GetBytesNoCopy(body, "fallbacks").Exists() && !helperProfile {
|
||||
baseBetas = withoutClaudeBeta(baseBetas, claudeServerSideFallbackBeta)
|
||||
}
|
||||
}
|
||||
if !reqProbeOrHelper && !countTokens && helps.ClaudePayloadHas1hTTL(body) {
|
||||
baseBetas = withClaudeExtendedCacheTTLBeta(baseBetas)
|
||||
}
|
||||
reqModel := strings.ToLower(strings.TrimSpace(ojson.GetBytesNoCopy(body, "model").String()))
|
||||
if isClaudeHaikuModel(reqModel) && !ojson.GetBytesNoCopy(body, "fallbacks").Exists() && !helperProfile {
|
||||
baseBetas = withoutClaudeBeta(baseBetas, claudeServerSideFallbackBeta)
|
||||
}
|
||||
// A body carrying thinking.block_binding is rejected upstream unless
|
||||
// the binding-controls beta travels with it, in every fingerprint
|
||||
// mode (cloaked, caller-owned, and confirmed passthrough alike).
|
||||
|
||||
@@ -246,7 +246,7 @@ func (e *ClaudeExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.A
|
||||
touchedPayloadPaths := helps.ApplyPayloadConfigWithTrackedPathsDoc(
|
||||
e.cfg, baseModel, to.String(), from.String(), "", doc, stripTranslatedDefaultMaxTokens(from, originalPayload, originalTranslated),
|
||||
helps.PayloadRequestedModel(opts, req.Model), helps.PayloadRequestPath(opts), opts.Headers,
|
||||
"diagnostics",
|
||||
"diagnostics", "betas",
|
||||
)
|
||||
if touchedPayloadPaths["diagnostics"] {
|
||||
diagnosticsState = claudeDiagnosticsRequestState{}
|
||||
@@ -286,6 +286,10 @@ func (e *ClaudeExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.A
|
||||
); errHeaders != nil {
|
||||
return nil, errHeaders
|
||||
}
|
||||
threadBetas, errThread := e.prepareClaudeThreadBetas(ctx, httpReq, auth, bodyForUpstream, extraBetas, touchedPayloadPaths["betas"], incomingHeaders, wirePolicy.Uncloak, opts.Metadata, req.Metadata)
|
||||
if errThread != nil {
|
||||
return nil, errThread
|
||||
}
|
||||
fastRequest := isAnthropicUpstreamBase(baseURL) && claudeRequestIsFast(httpReq, bodyForUpstream)
|
||||
authID, authLabel, authType, authValue := claudeAuthLogIdentity(auth)
|
||||
helps.RecordAPIRequest(ctx, e.cfg, helps.UpstreamRequestLog{
|
||||
@@ -392,9 +396,13 @@ func (e *ClaudeExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.A
|
||||
}
|
||||
}
|
||||
|
||||
streamBody := threadBetas.StreamReader(decodedBody, func(messageID string) {
|
||||
commitClaudeContinuity(diagnosticsState, messageID, helps.HeaderValueCaseInsensitive(httpResp.Header, "request-id"))
|
||||
e.rememberClaudeOAuthToolAliases(body, oauthToolNamesReverseMap, messageID)
|
||||
})
|
||||
// If the response target is Claude, directly forward complete SSE events without translation.
|
||||
if responseFormat == to {
|
||||
scanner := bufio.NewScanner(decodedBody)
|
||||
scanner := bufio.NewScanner(streamBody)
|
||||
scanner.Buffer(nil, 52_428_800) // 50MB
|
||||
var event bytes.Buffer
|
||||
var upstreamMessageID string
|
||||
@@ -472,7 +480,7 @@ func (e *ClaudeExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.A
|
||||
}
|
||||
|
||||
// For other formats, use translation
|
||||
scanner := bufio.NewScanner(decodedBody)
|
||||
scanner := bufio.NewScanner(streamBody)
|
||||
scanner.Buffer(nil, 52_428_800) // 50MB
|
||||
var param any
|
||||
helps.InitializeApplyPatchStream(ctx, to, responseFormat, req.Model, helps.ApplyPatchOriginalRequest(req, opts), bodyForTranslation, ¶m)
|
||||
|
||||
@@ -0,0 +1,267 @@
|
||||
package executor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/router-for-me/CLIProxyAPI/v8/internal/config"
|
||||
homekv "github.com/router-for-me/CLIProxyAPI/v8/internal/home"
|
||||
"github.com/router-for-me/CLIProxyAPI/v8/internal/kvstore"
|
||||
"github.com/router-for-me/CLIProxyAPI/v8/internal/registry"
|
||||
"github.com/router-for-me/CLIProxyAPI/v8/internal/runtime/executor/helps"
|
||||
cliproxyauth "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/auth"
|
||||
cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/executor"
|
||||
sdktranslator "github.com/router-for-me/CLIProxyAPI/v8/sdk/translator"
|
||||
)
|
||||
|
||||
func TestClaudeThreadCachedProfileRejectsMissingCapabilities(t *testing.T) {
|
||||
for _, addition := range []string{`"speed":"fast"`, `"thinking":{"type":"adaptive","block_binding":{"prefix_mismatch_behavior":"drop_block"}}`} {
|
||||
t.Run(addition, func(t *testing.T) {
|
||||
calls := 0
|
||||
transport := roundTripperFunc(func(req *http.Request) (*http.Response, error) {
|
||||
calls++
|
||||
return claudeThreadTestResponse(req, http.StatusOK, "application/json", claudeThreadTestMessage("msg_thread_1")), nil
|
||||
})
|
||||
ctx := context.WithValue(context.Background(), "cliproxy.roundtripper", http.RoundTripper(transport))
|
||||
executor := NewClaudeExecutor(&config.Config{})
|
||||
auth := &cliproxyauth.Auth{ID: t.Name(), Attributes: map[string]string{"api_key": "key-thread-new-capability"}}
|
||||
opts := cliproxyexecutor.Options{SourceFormat: sdktranslator.FormatClaude, Headers: http.Header{"Anthropic-Beta": []string{"message-threads-2026-08-12"}}}
|
||||
request := cliproxyexecutor.Request{Model: "claude-opus-5", Payload: []byte(`{"model":"claude-opus-5","thread":{"type":"create"},"messages":[{"role":"user","content":"first"}]}`)}
|
||||
if _, errExecute := executor.Execute(ctx, auth, request, opts); errExecute != nil {
|
||||
t.Fatal(errExecute)
|
||||
}
|
||||
request.Payload = []byte(`{"model":"claude-opus-5","thread":{"type":"continue","previous_message_id":"msg_thread_1"},` + addition + `,"messages":[{"role":"user","content":"second"}]}`)
|
||||
_, errExecute := executor.Execute(ctx, auth, request, opts)
|
||||
var requestErr cliproxyexecutor.RequestScopedError
|
||||
if errExecute == nil || !errors.As(errExecute, &requestErr) || !requestErr.IsRequestScoped() || calls != 1 {
|
||||
t.Fatalf("incompatible capability: error=%v, sends=%d, want request-scoped conflict without sending", errExecute, calls)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestClaudeThreadDynamicBetaPolicyCannotBeOverridden(t *testing.T) {
|
||||
calls := 0
|
||||
transport := roundTripperFunc(func(req *http.Request) (*http.Response, error) {
|
||||
calls++
|
||||
return claudeThreadTestResponse(req, http.StatusOK, "application/json", claudeThreadTestMessage("msg_thread_1")), nil
|
||||
})
|
||||
ctx := context.WithValue(context.Background(), "cliproxy.roundtripper", http.RoundTripper(transport))
|
||||
executor := NewClaudeExecutor(&config.Config{})
|
||||
auth := &cliproxyauth.Auth{ID: t.Name(), Attributes: map[string]string{"api_key": "key-thread-dynamic", "base_url": "https://gateway.example.com", "header:Anthropic-Beta": "$X-Upstream-Betas"}}
|
||||
opts := cliproxyexecutor.Options{SourceFormat: sdktranslator.FormatClaude, Headers: http.Header{"Anthropic-Beta": []string{"message-threads-2026-08-12"}, "X-Upstream-Betas": []string{"configured-a"}}}
|
||||
request := cliproxyexecutor.Request{Model: "claude-opus-5", Payload: []byte(`{"model":"claude-opus-5","thread":{"type":"create"},"messages":[{"role":"user","content":"first"}]}`)}
|
||||
if _, errExecute := executor.Execute(ctx, auth, request, opts); errExecute != nil {
|
||||
t.Fatal(errExecute)
|
||||
}
|
||||
opts.Headers.Set("X-Upstream-Betas", "configured-b")
|
||||
request.Payload = []byte(`{"model":"claude-opus-5","thread":{"type":"continue","previous_message_id":"msg_thread_1"},"messages":[{"role":"user","content":"second"}]}`)
|
||||
_, errExecute := executor.Execute(ctx, auth, request, opts)
|
||||
var requestErr cliproxyexecutor.RequestScopedError
|
||||
if errExecute == nil || !errors.As(errExecute, &requestErr) || !requestErr.IsRequestScoped() || calls != 1 {
|
||||
t.Fatalf("dynamic configured beta was overwritten: error=%v, sends=%d", errExecute, calls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClaudeThreadUnknownProfileRequiresReplay(t *testing.T) {
|
||||
transport := roundTripperFunc(func(req *http.Request) (*http.Response, error) {
|
||||
t.Error("sent an unverified Thread beta guess")
|
||||
return claudeThreadTestResponse(req, http.StatusOK, "application/json", claudeThreadTestMessage("msg_unknown")), nil
|
||||
})
|
||||
ctx := context.WithValue(context.Background(), "cliproxy.roundtripper", http.RoundTripper(transport))
|
||||
auth := &cliproxyauth.Auth{ID: t.Name(), Attributes: map[string]string{"api_key": "key-thread-unknown"}}
|
||||
request := cliproxyexecutor.Request{Model: "claude-opus-5", Payload: []byte(`{"model":"claude-opus-5","thread":{"type":"continue","previous_message_id":"msg_unknown"},"messages":[{"role":"user","content":"second"}]}`)}
|
||||
opts := cliproxyexecutor.Options{SourceFormat: sdktranslator.FormatClaude, Headers: http.Header{"Anthropic-Beta": []string{"message-threads-2026-08-12"}}}
|
||||
if _, errExecute := NewClaudeExecutor(&config.Config{}).Execute(ctx, auth, request, opts); errExecute == nil {
|
||||
t.Fatal("missing actual snapshot was accepted instead of requiring replay")
|
||||
}
|
||||
}
|
||||
|
||||
func TestClaudeThreadCallerBodyBetasMayBeOmittedOnContinuation(t *testing.T) {
|
||||
for _, stream := range []bool{false, true} {
|
||||
t.Run(fmt.Sprintf("stream=%v", stream), func(t *testing.T) {
|
||||
var sent []string
|
||||
transport := roundTripperFunc(func(req *http.Request) (*http.Response, error) {
|
||||
sent = append(sent, strings.Join(helps.HeaderValuesCaseInsensitive(req.Header, "Anthropic-Beta"), ","))
|
||||
if stream {
|
||||
return claudeThreadTestResponse(req, http.StatusOK, "text/event-stream", claudeThreadTestSSE("msg_thread_1")), nil
|
||||
}
|
||||
return claudeThreadTestResponse(req, http.StatusOK, "application/json", claudeThreadTestMessage("msg_thread_1")), nil
|
||||
})
|
||||
ctx := context.WithValue(context.Background(), "cliproxy.roundtripper", http.RoundTripper(transport))
|
||||
executor := NewClaudeExecutor(&config.Config{})
|
||||
auth := &cliproxyauth.Auth{ID: t.Name(), Attributes: map[string]string{"api_key": "key-body-declaration"}}
|
||||
opts := cliproxyexecutor.Options{SourceFormat: sdktranslator.FormatClaude, Headers: http.Header{"Anthropic-Beta": []string{"message-threads-2026-08-12"}}}
|
||||
for _, body := range []string{
|
||||
`{"model":"claude-opus-5","thread":{"type":"create"},"betas":["effort-2025-11-24"],"messages":[{"role":"user","content":"first"}]}`,
|
||||
`{"model":"claude-opus-5","thread":{"type":"continue","previous_message_id":"msg_thread_1"},"thinking":{"type":"disabled"},"messages":[{"role":"user","content":"second"}]}`,
|
||||
} {
|
||||
request := cliproxyexecutor.Request{Model: "claude-opus-5", Payload: []byte(body)}
|
||||
if stream {
|
||||
result, errStream := executor.ExecuteStream(ctx, auth, request, opts)
|
||||
if errStream != nil {
|
||||
t.Fatal(errStream)
|
||||
}
|
||||
for chunk := range result.Chunks {
|
||||
if chunk.Err != nil {
|
||||
t.Fatal(chunk.Err)
|
||||
}
|
||||
}
|
||||
} else if _, errExecute := executor.Execute(ctx, auth, request, opts); errExecute != nil {
|
||||
t.Fatal(errExecute)
|
||||
}
|
||||
}
|
||||
if len(sent) != 2 || sent[0] != sent[1] || !strings.Contains(sent[0], "effort-2025-11-24") {
|
||||
t.Fatalf("caller body declaration was not retained: %q", sent)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestClaudeThreadPayloadFilterDoesNotConstrainIndependentHeaderDeclarations(t *testing.T) {
|
||||
var sent []string
|
||||
transport := roundTripperFunc(func(req *http.Request) (*http.Response, error) {
|
||||
sent = append(sent, strings.Join(helps.HeaderValuesCaseInsensitive(req.Header, "Anthropic-Beta"), ","))
|
||||
return claudeThreadTestResponse(req, http.StatusOK, "application/json", claudeThreadTestMessage("msg_thread_1")), nil
|
||||
})
|
||||
ctx := context.WithValue(context.Background(), "cliproxy.roundtripper", http.RoundTripper(transport))
|
||||
executor := NewClaudeExecutor(&config.Config{})
|
||||
auth := &cliproxyauth.Auth{ID: t.Name(), Attributes: map[string]string{"api_key": "sk-ant-oat-header-declaration"}, Metadata: map[string]any{"account_uuid": "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa"}}
|
||||
opts := cliproxyexecutor.Options{SourceFormat: sdktranslator.FormatClaude, Headers: http.Header{"Anthropic-Beta": []string{"message-threads-2026-08-12,effort-2025-11-24"}}}
|
||||
request := cliproxyexecutor.Request{Model: "claude-opus-5", Payload: []byte(`{"model":"claude-opus-5","thread":{"type":"create"},"betas":["effort-2025-11-24"],"safeguards":{},"messages":[{"role":"user","content":"first"}]}`)}
|
||||
if _, errExecute := executor.Execute(ctx, auth, request, opts); errExecute != nil {
|
||||
t.Fatal(errExecute)
|
||||
}
|
||||
cfg := &config.Config{Payload: config.PayloadConfig{Filter: []config.PayloadFilterRule{{Models: []config.PayloadModelRule{{Name: "claude-opus-5"}}, Params: []string{"betas"}}}}}
|
||||
request.Payload = []byte(`{"model":"claude-opus-5","thread":{"type":"continue","previous_message_id":"msg_thread_1"},"betas":["effort-2025-11-24"],"thinking":{"type":"disabled"},"messages":[{"role":"user","content":"second"}]}`)
|
||||
if _, errExecute := executor.WithConfig(cfg).Execute(ctx, auth, request, opts); errExecute != nil {
|
||||
t.Fatalf("body filter invalidated an independently declared header capability: %v", errExecute)
|
||||
}
|
||||
if len(sent) != 2 || sent[0] != sent[1] {
|
||||
t.Fatalf("header declarations changed while body declarations were filtered: %q", sent)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClaudeThreadManagerRoutesToOriginalCredential(t *testing.T) {
|
||||
for _, stream := range []bool{false, true} {
|
||||
for _, sameKey := range []bool{false, true} {
|
||||
for _, variant := range []string{"normal", "disabled owner", "conflicting pin", "foreign caller"} {
|
||||
t.Run(fmt.Sprintf("stream=%v/same-key=%v/%s", stream, sameKey, variant), func(t *testing.T) {
|
||||
caller := t.Name() + uuid.NewString()
|
||||
manager := cliproxyauth.NewManager(nil, nil, nil)
|
||||
manager.SetRetryConfig(0, 0, 0)
|
||||
manager.RegisterExecutor(NewClaudeExecutor(&config.Config{}))
|
||||
if _, sticky := manager.Selector().(*cliproxyauth.SessionAffinitySelector); sticky {
|
||||
t.Fatal("test requires default round-robin without session affinity")
|
||||
}
|
||||
auths := make([]*cliproxyauth.Auth, 0, 2)
|
||||
for i := range 2 {
|
||||
key := "key-thread-routing"
|
||||
if !sameKey {
|
||||
key += fmt.Sprint(i)
|
||||
}
|
||||
auth := &cliproxyauth.Auth{ID: fmt.Sprintf("%s-%d", caller, i), Provider: "claude", Attributes: map[string]string{"api_key": key}}
|
||||
registry.GetGlobalRegistry().RegisterClient(auth.ID, "claude", []*registry.ModelInfo{{ID: "claude-opus-5"}})
|
||||
t.Cleanup(func() { registry.GetGlobalRegistry().UnregisterClient(auth.ID) })
|
||||
if _, errRegister := manager.Register(context.Background(), auth); errRegister != nil {
|
||||
t.Fatal(errRegister)
|
||||
}
|
||||
auths = append(auths, auth)
|
||||
}
|
||||
calls := 0
|
||||
transport := roundTripperFunc(func(req *http.Request) (*http.Response, error) {
|
||||
calls++
|
||||
if stream {
|
||||
return claudeThreadTestResponse(req, http.StatusOK, "text/event-stream", claudeThreadTestSSE("msg_thread_1")), nil
|
||||
}
|
||||
return claudeThreadTestResponse(req, http.StatusOK, "application/json", claudeThreadTestMessage("msg_thread_1")), nil
|
||||
})
|
||||
ctx := context.WithValue(context.Background(), "cliproxy.roundtripper", http.RoundTripper(transport))
|
||||
var selected []string
|
||||
opts := cliproxyexecutor.Options{SourceFormat: sdktranslator.FormatClaude, Headers: http.Header{"Anthropic-Beta": []string{"message-threads-2026-08-12"}}, Metadata: map[string]any{
|
||||
cliproxyexecutor.CallerScopeMetadataKey: caller,
|
||||
cliproxyexecutor.SelectedAuthCallbackMetadataKey: func(id string) { selected = append(selected, id) },
|
||||
}}
|
||||
execute := func(request cliproxyexecutor.Request) error {
|
||||
if stream {
|
||||
result, errStream := manager.ExecuteStream(ctx, []string{"claude"}, request, opts)
|
||||
if errStream != nil {
|
||||
return errStream
|
||||
}
|
||||
for chunk := range result.Chunks {
|
||||
if chunk.Err != nil {
|
||||
return chunk.Err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
_, errExecute := manager.Execute(ctx, []string{"claude"}, request, opts)
|
||||
return errExecute
|
||||
}
|
||||
request := cliproxyexecutor.Request{Model: "claude-opus-5", Payload: []byte(`{"model":"claude-opus-5","thread":{"type":"create"},"messages":[{"role":"user","content":"first"}]}`)}
|
||||
if errExecute := execute(request); errExecute != nil {
|
||||
t.Fatal(errExecute)
|
||||
}
|
||||
if len(selected) != 1 {
|
||||
t.Fatalf("initial selections = %v", selected)
|
||||
}
|
||||
switch variant {
|
||||
case "disabled owner":
|
||||
owner, _ := manager.GetByID(selected[0])
|
||||
owner.Disabled = true
|
||||
if _, errUpdate := manager.Update(ctx, owner); errUpdate != nil {
|
||||
t.Fatal(errUpdate)
|
||||
}
|
||||
case "conflicting pin":
|
||||
for _, auth := range auths {
|
||||
if auth.ID != selected[0] {
|
||||
opts.Metadata[cliproxyexecutor.PinnedAuthMetadataKey] = auth.ID
|
||||
}
|
||||
}
|
||||
case "foreign caller":
|
||||
opts.Metadata[cliproxyexecutor.CallerScopeMetadataKey] = "foreign-" + caller
|
||||
}
|
||||
request.Payload = []byte(`{"model":"claude-opus-5","thread":{"type":"continue","previous_message_id":"msg_thread_1"},"messages":[{"role":"user","content":"second"}]}`)
|
||||
errExecute := execute(request)
|
||||
if variant == "normal" {
|
||||
if errExecute != nil || calls != 2 || len(selected) != 2 || selected[1] != selected[0] {
|
||||
t.Fatalf("Thread lost original credential: error=%v, calls=%d, selections=%v", errExecute, calls, selected)
|
||||
}
|
||||
} else {
|
||||
if errExecute == nil || calls != 1 {
|
||||
t.Fatalf("unsafe continuation: error=%v, calls=%d, selections=%v", errExecute, calls, selected)
|
||||
}
|
||||
if variant != "foreign caller" && len(selected) != 1 {
|
||||
t.Fatalf("selected a different credential instead of honoring ownership: %v", selected)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func claudeThreadTestKVStore(t *testing.T) string {
|
||||
t.Helper()
|
||||
homekv.ClearCurrent()
|
||||
if errShutdown := kvstore.ShutdownLocal(); errShutdown != nil {
|
||||
t.Fatal(errShutdown)
|
||||
}
|
||||
path := filepath.Join(t.TempDir(), "kv.db")
|
||||
if errConfigure := kvstore.ConfigureLocal(true, path); errConfigure != nil {
|
||||
t.Fatal(errConfigure)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
homekv.ClearCurrent()
|
||||
if errShutdown := kvstore.ShutdownLocal(); errShutdown != nil {
|
||||
t.Error(errShutdown)
|
||||
}
|
||||
})
|
||||
return path
|
||||
}
|
||||
591
internal/runtime/executor/claude_executor_thread_beta_test.go
Normal file
591
internal/runtime/executor/claude_executor_thread_beta_test.go
Normal file
@@ -0,0 +1,591 @@
|
||||
package executor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/MoeForge/moego/ojson"
|
||||
"github.com/router-for-me/CLIProxyAPI/v8/internal/config"
|
||||
"github.com/router-for-me/CLIProxyAPI/v8/internal/kvstore"
|
||||
"github.com/router-for-me/CLIProxyAPI/v8/internal/runtime/executor/helps"
|
||||
cliproxyauth "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/auth"
|
||||
cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/executor"
|
||||
sdktranslator "github.com/router-for-me/CLIProxyAPI/v8/sdk/translator"
|
||||
)
|
||||
|
||||
// Changing thinking or safeguards between turns must not change the wire beta
|
||||
// profile that Anthropic bound to the Thread's first successful message.
|
||||
func TestClaudeThreadBetasRemainStable(t *testing.T) {
|
||||
for _, stream := range []bool{false, true} {
|
||||
for _, callerBetas := range []bool{false, true} {
|
||||
t.Run(fmt.Sprintf("stream=%v/caller-betas=%v", stream, callerBetas), func(t *testing.T) {
|
||||
auth := &cliproxyauth.Auth{ID: t.Name(), Attributes: map[string]string{"api_key": "key-thread-beta"}}
|
||||
headers := http.Header{}
|
||||
if callerBetas {
|
||||
headers.Set("Anthropic-Beta", "message-threads-2026-08-12,effort-2025-11-24")
|
||||
} else {
|
||||
auth.Attributes["api_key"] = "sk-ant-oat-thread-beta"
|
||||
auth.Metadata = map[string]any{"account_uuid": "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa"}
|
||||
}
|
||||
var sent []string
|
||||
transport := roundTripperFunc(func(req *http.Request) (*http.Response, error) {
|
||||
body, errRead := io.ReadAll(req.Body)
|
||||
if errRead != nil {
|
||||
return nil, errRead
|
||||
}
|
||||
betas := strings.Join(helps.HeaderValuesCaseInsensitive(req.Header, "Anthropic-Beta"), ",")
|
||||
sent = append(sent, betas)
|
||||
if len(sent) > 1 {
|
||||
if got := ojson.GetBytes(body, "thread.previous_message_id").String(); got != "msg_thread_1" {
|
||||
t.Errorf("previous_message_id = %q, want msg_thread_1", got)
|
||||
}
|
||||
if betas != sent[0] {
|
||||
return claudeThreadTestResponse(req, http.StatusBadRequest, "application/json", `{"type":"error","error":{"type":"invalid_request_error","message":"differing field(s): anthropic-beta. These fields must stay the same across a Thread's turns"}}`), nil
|
||||
}
|
||||
}
|
||||
id := fmt.Sprintf("msg_thread_%d", len(sent))
|
||||
if ojson.GetBytes(body, "stream").Bool() {
|
||||
return claudeThreadTestResponse(req, http.StatusOK, "text/event-stream", claudeThreadTestSSE(id)), nil
|
||||
}
|
||||
return claudeThreadTestResponse(req, http.StatusOK, "application/json", claudeThreadTestMessage(id)), nil
|
||||
})
|
||||
ctx := context.WithValue(context.Background(), "cliproxy.roundtripper", http.RoundTripper(transport))
|
||||
executor := NewClaudeExecutor(&config.Config{})
|
||||
opts := cliproxyexecutor.Options{SourceFormat: sdktranslator.FormatClaude, Headers: headers}
|
||||
bodies := []string{
|
||||
`{"model":"claude-opus-5","max_tokens":2048,"thread":{"type":"create"},"thinking":{"type":"enabled","budget_tokens":1024},"safeguards":{},"messages":[{"role":"user","content":"first"}]}`,
|
||||
`{"model":"claude-opus-5","max_tokens":2048,"thread":{"type":"continue","previous_message_id":"msg_thread_1"},"thinking":{"type":"disabled"},"messages":[{"role":"user","content":"second"}]}`,
|
||||
}
|
||||
for turn, body := range bodies {
|
||||
request := cliproxyexecutor.Request{Model: "claude-opus-5", Payload: []byte(body)}
|
||||
if stream {
|
||||
result, errStream := executor.ExecuteStream(ctx, auth, request, opts)
|
||||
if errStream != nil {
|
||||
t.Fatalf("turn %d ExecuteStream: %v; wire betas=%q", turn+1, errStream, sent)
|
||||
}
|
||||
for chunk := range result.Chunks {
|
||||
if chunk.Err != nil {
|
||||
t.Fatalf("turn %d stream: %v", turn+1, chunk.Err)
|
||||
}
|
||||
}
|
||||
} else if _, errExecute := executor.Execute(ctx, auth, request, opts); errExecute != nil {
|
||||
t.Fatalf("turn %d Execute: %v; wire betas=%q", turn+1, errExecute, sent)
|
||||
}
|
||||
}
|
||||
if len(sent) != 2 || sent[0] == "" || sent[0] != sent[1] {
|
||||
t.Fatalf("wire betas = %q, want two identical nonempty profiles", sent)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Declared Thread capabilities must survive the first turn even if that turn
|
||||
// does not exercise them: later turns cannot add them to the bound profile.
|
||||
func TestClaudeThreadFirstTurnKeepsDeclaredCapabilities(t *testing.T) {
|
||||
const betas = "message-threads-2026-08-12,effort-2025-11-24,thinking-display-updates-2026-08-18,extended-cache-ttl-2025-04-11"
|
||||
for _, confirmed := range []bool{false, true} {
|
||||
t.Run(fmt.Sprintf("confirmed=%v", confirmed), func(t *testing.T) {
|
||||
body := []byte(`{"model":"claude-opus-5","thread":{"type":"create"},"thinking":{"type":"disabled"},"messages":[{"role":"user","content":"hello"}]}`)
|
||||
req, errRequest := http.NewRequest(http.MethodPost, "https://api.anthropic.com/v1/messages", nil)
|
||||
if errRequest != nil {
|
||||
t.Fatal(errRequest)
|
||||
}
|
||||
incoming := http.Header{"Anthropic-Beta": []string{betas}}
|
||||
auth := &cliproxyauth.Auth{Attributes: map[string]string{"api_key": "key-thread-declarations"}}
|
||||
if errHeaders := applyClaudeHeaders(req, auth, "key-thread-declarations", false, nil, body, nil, incoming, confirmed); errHeaders != nil {
|
||||
t.Fatal(errHeaders)
|
||||
}
|
||||
if got := req.Header.Get("Anthropic-Beta"); got != betas {
|
||||
t.Fatalf("first-turn betas = %q, want declared Thread capabilities %q", got, betas)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestClaudeThreadFirstTurnKeepsRequiredBetas(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
body string
|
||||
beta string
|
||||
}{
|
||||
{name: "fast", body: `"speed":"fast"`, beta: "fast-mode-2026-02-01"},
|
||||
{name: "advisor", body: `"tools":[{"type":"advisor_20260301","name":"advisor"}]`, beta: "advisor-tool-2026-03-01"},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
body := []byte(`{"model":"claude-opus-5","thread":{"type":"create"},` + test.body + `,"messages":[{"role":"user","content":"hello"}]}`)
|
||||
req, errRequest := http.NewRequest(http.MethodPost, "https://api.anthropic.com/v1/messages", nil)
|
||||
if errRequest != nil {
|
||||
t.Fatal(errRequest)
|
||||
}
|
||||
incoming := http.Header{"Anthropic-Beta": []string{"message-threads-2026-08-12"}}
|
||||
auth := &cliproxyauth.Auth{Attributes: map[string]string{"api_key": "key-thread-required"}}
|
||||
if errHeaders := applyClaudeHeaders(req, auth, "key-thread-required", false, nil, body, nil, incoming, false); errHeaders != nil {
|
||||
t.Fatal(errHeaders)
|
||||
}
|
||||
if got := req.Header.Get("Anthropic-Beta"); !strings.Contains(got, test.beta) {
|
||||
t.Fatalf("first-turn beta = %q, missing required %s", got, test.beta)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestClaudeThreadOrdinaryRefusalCanContinue(t *testing.T) {
|
||||
for _, stream := range []bool{false, true} {
|
||||
t.Run(fmt.Sprintf("stream=%v", stream), func(t *testing.T) {
|
||||
calls := 0
|
||||
transport := roundTripperFunc(func(req *http.Request) (*http.Response, error) {
|
||||
calls++
|
||||
if stream && calls == 1 {
|
||||
return claudeThreadTestResponse(req, http.StatusOK, "text/event-stream", strings.ReplaceAll(claudeThreadTestSSE("msg_thread_1"), "end_turn", "refusal")), nil
|
||||
}
|
||||
return claudeThreadTestResponse(req, http.StatusOK, "application/json", strings.ReplaceAll(claudeThreadTestMessage(fmt.Sprintf("msg_thread_%d", calls)), "end_turn", "refusal")), nil
|
||||
})
|
||||
ctx := context.WithValue(context.Background(), "cliproxy.roundtripper", http.RoundTripper(transport))
|
||||
executor := NewClaudeExecutor(&config.Config{})
|
||||
auth := &cliproxyauth.Auth{ID: t.Name(), Attributes: map[string]string{"api_key": "key-thread-refusal"}}
|
||||
opts := cliproxyexecutor.Options{SourceFormat: sdktranslator.FormatClaude, Headers: http.Header{"Anthropic-Beta": []string{"message-threads-2026-08-12"}}}
|
||||
request := cliproxyexecutor.Request{Model: "claude-opus-5", Payload: []byte(`{"model":"claude-opus-5","thread":{"type":"create"},"messages":[{"role":"user","content":"first"}]}`)}
|
||||
if stream {
|
||||
result, errStream := executor.ExecuteStream(ctx, auth, request, opts)
|
||||
if errStream != nil {
|
||||
t.Fatal(errStream)
|
||||
}
|
||||
for chunk := range result.Chunks {
|
||||
if chunk.Err != nil {
|
||||
t.Fatal(chunk.Err)
|
||||
}
|
||||
}
|
||||
} else if _, errExecute := executor.Execute(ctx, auth, request, opts); errExecute != nil {
|
||||
t.Fatal(errExecute)
|
||||
}
|
||||
request.Payload = []byte(`{"model":"claude-opus-5","thread":{"type":"continue","previous_message_id":"msg_thread_1"},"messages":[{"role":"user","content":"second"}]}`)
|
||||
if _, errExecute := executor.Execute(ctx, auth, request, opts); errExecute != nil {
|
||||
t.Fatalf("continuation after successful ordinary refusal: %v", errExecute)
|
||||
}
|
||||
if calls != 2 {
|
||||
t.Fatalf("upstream requests = %d, want 2", calls)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// The reader is paused exactly before message_stop. Once its gate is reached,
|
||||
// all earlier sends have happened and the producer cannot enqueue more output.
|
||||
// Draining the channel at that point deterministically exposes early completion.
|
||||
func TestClaudeThreadStreamPublishesStateBeforeCompletion(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
format sdktranslator.Format
|
||||
oauth bool
|
||||
}{
|
||||
{format: sdktranslator.FormatClaude},
|
||||
{format: sdktranslator.FormatOpenAI},
|
||||
{format: sdktranslator.FormatOpenAIResponse},
|
||||
{format: sdktranslator.FormatClaude, oauth: true},
|
||||
{format: sdktranslator.FormatOpenAI, oauth: true},
|
||||
{format: sdktranslator.FormatOpenAIResponse, oauth: true},
|
||||
} {
|
||||
t.Run(fmt.Sprintf("%s/oauth=%v", test.format, test.oauth), func(t *testing.T) {
|
||||
waitingStop := make(chan struct{})
|
||||
releaseStop := make(chan struct{})
|
||||
defer func() {
|
||||
select {
|
||||
case <-releaseStop:
|
||||
default:
|
||||
close(releaseStop)
|
||||
}
|
||||
}()
|
||||
prefix, suffix, ok := strings.Cut(claudeThreadTestSSE("msg_thread_1"), "event: message_stop")
|
||||
if !ok {
|
||||
t.Fatal("SSE fixture has no message_stop")
|
||||
}
|
||||
body := &claudeThreadGatedReader{prefix: strings.NewReader(prefix), suffix: strings.NewReader("event: message_stop" + suffix), waiting: waitingStop, release: releaseStop}
|
||||
var sent []string
|
||||
transport := roundTripperFunc(func(req *http.Request) (*http.Response, error) {
|
||||
sent = append(sent, strings.Join(helps.HeaderValuesCaseInsensitive(req.Header, "Anthropic-Beta"), ","))
|
||||
if len(sent) == 1 {
|
||||
resp := claudeThreadTestResponse(req, http.StatusOK, "text/event-stream", "")
|
||||
resp.Body = io.NopCloser(body)
|
||||
return resp, nil
|
||||
}
|
||||
if sent[0] != sent[1] {
|
||||
t.Errorf("continuation beta changed: %q", sent)
|
||||
}
|
||||
return claudeThreadTestResponse(req, http.StatusOK, "application/json", claudeThreadTestMessage("msg_thread_2")), nil
|
||||
})
|
||||
ctx, cancel := context.WithCancel(context.WithValue(context.Background(), "cliproxy.roundtripper", http.RoundTripper(transport)))
|
||||
defer cancel()
|
||||
executor := NewClaudeExecutor(&config.Config{})
|
||||
auth := &cliproxyauth.Auth{ID: t.Name(), Attributes: map[string]string{"api_key": "key-thread-timing"}}
|
||||
if test.oauth {
|
||||
auth.Attributes["api_key"] = "sk-ant-oat-thread-timing"
|
||||
auth.Metadata = map[string]any{"account_uuid": "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa"}
|
||||
}
|
||||
headers := http.Header{"Anthropic-Beta": []string{"message-threads-2026-08-12,effort-2025-11-24"}}
|
||||
opts := cliproxyexecutor.Options{SourceFormat: sdktranslator.FormatClaude, ResponseFormat: test.format, Headers: headers}
|
||||
request := cliproxyexecutor.Request{Model: "claude-opus-5", Payload: []byte(`{"model":"claude-opus-5","max_tokens":2048,"thread":{"type":"create"},"messages":[{"role":"user","content":"first"}]}`)}
|
||||
result, errStream := executor.ExecuteStream(ctx, auth, request, opts)
|
||||
if errStream != nil {
|
||||
t.Fatal(errStream)
|
||||
}
|
||||
var beforeStop [][]byte
|
||||
waiting:
|
||||
for {
|
||||
select {
|
||||
case chunk, open := <-result.Chunks:
|
||||
if !open || chunk.Err != nil {
|
||||
t.Fatalf("stream ended before stop gate: %v", chunk.Err)
|
||||
}
|
||||
beforeStop = append(beforeStop, chunk.Payload)
|
||||
case <-waitingStop:
|
||||
for len(result.Chunks) > 0 {
|
||||
chunk := <-result.Chunks
|
||||
beforeStop = append(beforeStop, chunk.Payload)
|
||||
}
|
||||
break waiting
|
||||
}
|
||||
}
|
||||
for _, payload := range beforeStop {
|
||||
if claudeThreadTestTerminal(payload) {
|
||||
t.Errorf("completion escaped before upstream message_stop: %s", payload)
|
||||
}
|
||||
}
|
||||
close(releaseStop)
|
||||
continued := false
|
||||
for chunk := range result.Chunks {
|
||||
if chunk.Err != nil {
|
||||
t.Fatalf("stream: %v", chunk.Err)
|
||||
}
|
||||
if !continued && claudeThreadTestTerminal(chunk.Payload) {
|
||||
continued = true
|
||||
request.Payload = []byte(`{"model":"claude-opus-5","max_tokens":2048,"thread":{"type":"continue","previous_message_id":"msg_thread_1"},"thinking":{"type":"disabled"},"messages":[{"role":"user","content":"second"}]}`)
|
||||
opts.ResponseFormat = sdktranslator.FormatClaude
|
||||
if _, errExecute := executor.Execute(ctx, auth, request, opts); errExecute != nil {
|
||||
t.Errorf("immediate continuation after first completion: %v", errExecute)
|
||||
}
|
||||
}
|
||||
}
|
||||
if !continued || len(sent) != 2 {
|
||||
t.Fatalf("continued=%v, upstream requests=%d, want successful immediate continuation", continued, len(sent))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestClaudeThreadBetasSharedAcrossScopedCopiesAndIsolated(t *testing.T) {
|
||||
for _, stream := range []bool{false, true} {
|
||||
t.Run(fmt.Sprintf("stream=%v", stream), func(t *testing.T) {
|
||||
calls := 0
|
||||
transport := roundTripperFunc(func(req *http.Request) (*http.Response, error) {
|
||||
calls++
|
||||
if got := strings.Join(helps.HeaderValuesCaseInsensitive(req.Header, "Anthropic-Beta"), ","); got != "gateway-thread-beta" {
|
||||
t.Errorf("final configured wire profile = %q, want gateway-thread-beta", got)
|
||||
}
|
||||
if stream && calls == 1 {
|
||||
return claudeThreadTestResponse(req, http.StatusOK, "text/event-stream", claudeThreadTestSSE("msg_thread_1")), nil
|
||||
}
|
||||
return claudeThreadTestResponse(req, http.StatusOK, "application/json", claudeThreadTestMessage("msg_thread_1")), nil
|
||||
})
|
||||
ctx := context.WithValue(context.Background(), "cliproxy.roundtripper", http.RoundTripper(transport))
|
||||
registered := NewClaudeExecutor(&config.Config{})
|
||||
auth := &cliproxyauth.Auth{ID: "credential-a", Attributes: map[string]string{"api_key": "key-scoped-thread", "base_url": "https://gateway.example.com", "header:Anthropic-Beta": "gateway-thread-beta"}}
|
||||
opts := cliproxyexecutor.Options{SourceFormat: sdktranslator.FormatClaude, Metadata: map[string]any{cliproxyexecutor.CallerScopeMetadataKey: "caller-a"}}
|
||||
request := cliproxyexecutor.Request{Model: "claude-opus-5", Payload: []byte(`{"model":"claude-opus-5","thread":{"type":"create"},"messages":[{"role":"user","content":"first"}]}`)}
|
||||
first := registered.ForAPIKey()
|
||||
if stream {
|
||||
result, errStream := first.ExecuteStream(ctx, auth, request, opts)
|
||||
if errStream != nil {
|
||||
t.Fatal(errStream)
|
||||
}
|
||||
for chunk := range result.Chunks {
|
||||
if chunk.Err != nil {
|
||||
t.Fatal(chunk.Err)
|
||||
}
|
||||
}
|
||||
} else if _, errExecute := first.Execute(ctx, auth, request, opts); errExecute != nil {
|
||||
t.Fatal(errExecute)
|
||||
}
|
||||
request.Payload = []byte(`{"model":"claude-opus-5","thread":{"type":"continue","previous_message_id":"msg_thread_1"},"thinking":{"type":"disabled"},"messages":[{"role":"user","content":"second"}]}`)
|
||||
second := registered.ForAPIKey()
|
||||
if _, errExecute := second.Execute(ctx, auth, request, opts); errExecute != nil {
|
||||
t.Fatalf("continuation via a fresh scoped executor: %v", errExecute)
|
||||
}
|
||||
for _, isolated := range []string{"caller", "credential", "upstream"} {
|
||||
otherAuth := auth.Clone()
|
||||
otherOpts := opts
|
||||
switch isolated {
|
||||
case "caller":
|
||||
otherOpts.Metadata = map[string]any{cliproxyexecutor.CallerScopeMetadataKey: "caller-b"}
|
||||
case "credential":
|
||||
otherAuth.ID = "credential-b"
|
||||
case "upstream":
|
||||
otherAuth.Attributes["base_url"] = "https://other.example.com"
|
||||
}
|
||||
_, errExecute := second.Execute(ctx, otherAuth, request, otherOpts)
|
||||
var requestErr cliproxyexecutor.RequestScopedError
|
||||
if errExecute == nil || !errors.As(errExecute, &requestErr) || !requestErr.IsRequestScoped() {
|
||||
t.Errorf("%s isolation error = %v, want request-scoped missing Thread", isolated, errExecute)
|
||||
}
|
||||
}
|
||||
if calls != 2 {
|
||||
t.Fatalf("upstream requests = %d, want no sends for foreign Thread state", calls)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestClaudeThreadRejectsChangedBetaPolicy(t *testing.T) {
|
||||
for _, operation := range []string{"strip", "append", "payload replace", "payload filter"} {
|
||||
t.Run(operation, func(t *testing.T) {
|
||||
calls := 0
|
||||
transport := roundTripperFunc(func(req *http.Request) (*http.Response, error) {
|
||||
calls++
|
||||
return claudeThreadTestResponse(req, http.StatusOK, "application/json", claudeThreadTestMessage("msg_thread_1")), nil
|
||||
})
|
||||
ctx := context.WithValue(context.Background(), "cliproxy.roundtripper", http.RoundTripper(transport))
|
||||
initial := &config.Config{}
|
||||
if strings.HasPrefix(operation, "payload") {
|
||||
initial.Payload.Override = []config.PayloadRule{{Models: []config.PayloadModelRule{{Name: "claude-opus-5"}}, Params: map[string]any{"betas": []string{"payload-a-2099-01-01"}}}}
|
||||
}
|
||||
executor := NewClaudeExecutor(initial)
|
||||
auth := &cliproxyauth.Auth{ID: t.Name(), Attributes: map[string]string{"api_key": "key-thread-policy"}}
|
||||
opts := cliproxyexecutor.Options{SourceFormat: sdktranslator.FormatClaude, Headers: http.Header{"Anthropic-Beta": []string{"message-threads-2026-08-12,effort-2025-11-24"}}}
|
||||
request := cliproxyexecutor.Request{Model: "claude-opus-5", Payload: []byte(`{"model":"claude-opus-5","thread":{"type":"create"},"messages":[{"role":"user","content":"first"}]}`)}
|
||||
if _, errExecute := executor.Execute(ctx, auth, request, opts); errExecute != nil {
|
||||
t.Fatal(errExecute)
|
||||
}
|
||||
cfg := &config.Config{}
|
||||
switch operation {
|
||||
case "strip":
|
||||
cfg.ClaudeHeaderDefaults.AnthropicBetaStrip = []string{"effort-2025-11-24"}
|
||||
case "append":
|
||||
cfg.ClaudeHeaderDefaults.AnthropicBetaAppend = []string{"operator-beta-2099-01-01"}
|
||||
case "payload replace":
|
||||
cfg.Payload.Override = []config.PayloadRule{{Models: []config.PayloadModelRule{{Name: "claude-opus-5"}}, Params: map[string]any{"betas": []string{"payload-b-2099-01-01"}}}}
|
||||
case "payload filter":
|
||||
cfg.Payload.Override = initial.Payload.Override
|
||||
cfg.Payload.Filter = []config.PayloadFilterRule{{Models: []config.PayloadModelRule{{Name: "claude-opus-5"}}, Params: []string{"betas"}}}
|
||||
}
|
||||
request.Payload = []byte(`{"model":"claude-opus-5","thread":{"type":"continue","previous_message_id":"msg_thread_1"},"messages":[{"role":"user","content":"second"}]}`)
|
||||
_, errExecute := executor.WithConfig(cfg).Execute(ctx, auth, request, opts)
|
||||
var requestErr cliproxyexecutor.RequestScopedError
|
||||
if errExecute == nil || !errors.As(errExecute, &requestErr) || !requestErr.IsRequestScoped() {
|
||||
t.Fatalf("changed beta policy error = %v, want request-scoped replay requirement", errExecute)
|
||||
}
|
||||
if calls != 1 {
|
||||
t.Fatalf("sent %d requests, want no send that violates configured beta policy", calls)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestClaudeThreadCallerProfileSurvivesMissingCache(t *testing.T) {
|
||||
for _, missing := range []string{"restart", "eviction", "body restart", "body eviction", "body only restart", "body only eviction", "derived restart", "derived eviction"} {
|
||||
t.Run(missing, func(t *testing.T) {
|
||||
dbPath := claudeThreadTestKVStore(t)
|
||||
const betas = "message-threads-2026-08-12,effort-2025-11-24"
|
||||
wantBetas := betas
|
||||
callerBetas := betas
|
||||
bodyField := ""
|
||||
if strings.HasPrefix(missing, "body") {
|
||||
wantBetas += ",context-1m-2025-08-07"
|
||||
bodyField = `"betas":["context-1m-2025-08-07"],`
|
||||
}
|
||||
if strings.Contains(missing, "only") {
|
||||
callerBetas = ""
|
||||
bodyField = `"betas":["message-threads-2026-08-12","effort-2025-11-24","context-1m-2025-08-07"],`
|
||||
}
|
||||
initialFields := bodyField
|
||||
if strings.HasPrefix(missing, "derived") {
|
||||
wantBetas += ",fast-mode-2026-02-01"
|
||||
initialFields += `"speed":"fast",`
|
||||
}
|
||||
calls := 0
|
||||
transport := roundTripperFunc(func(req *http.Request) (*http.Response, error) {
|
||||
calls++
|
||||
if got := strings.Join(helps.HeaderValuesCaseInsensitive(req.Header, "Anthropic-Beta"), ","); got != wantBetas {
|
||||
t.Errorf("wire beta = %q, want client's stable profile %q", got, wantBetas)
|
||||
}
|
||||
return claudeThreadTestResponse(req, http.StatusOK, "application/json", claudeThreadTestMessage(fmt.Sprintf("msg_thread_%d", calls))), nil
|
||||
})
|
||||
ctx := context.WithValue(context.Background(), "cliproxy.roundtripper", http.RoundTripper(transport))
|
||||
executor := NewClaudeExecutor(&config.Config{})
|
||||
auth := &cliproxyauth.Auth{ID: t.Name(), Attributes: map[string]string{"api_key": "key-thread-cold-cache"}}
|
||||
opts := cliproxyexecutor.Options{SourceFormat: sdktranslator.FormatClaude, Headers: http.Header{"Anthropic-Beta": []string{callerBetas}}}
|
||||
request := cliproxyexecutor.Request{Model: "claude-opus-5", Payload: []byte(`{` + initialFields + `"model":"claude-opus-5","thread":{"type":"create"},"thinking":{"type":"disabled"},"messages":[{"role":"user","content":"first"}]}`)}
|
||||
if _, errExecute := executor.Execute(ctx, auth, request, opts); errExecute != nil {
|
||||
t.Fatal(errExecute)
|
||||
}
|
||||
if strings.HasSuffix(missing, "restart") {
|
||||
if errShutdown := kvstore.ShutdownLocal(); errShutdown != nil {
|
||||
t.Fatal(errShutdown)
|
||||
}
|
||||
if errConfigure := kvstore.ConfigureLocal(true, dbPath); errConfigure != nil {
|
||||
t.Fatal(errConfigure)
|
||||
}
|
||||
executor = NewClaudeExecutor(&config.Config{})
|
||||
} else {
|
||||
otherOpts := opts
|
||||
otherOpts.Metadata = map[string]any{cliproxyexecutor.CallerScopeMetadataKey: "other-caller"}
|
||||
for range 1024 {
|
||||
if _, errExecute := executor.Execute(ctx, auth, request, otherOpts); errExecute != nil {
|
||||
t.Fatal(errExecute)
|
||||
}
|
||||
}
|
||||
}
|
||||
request.Payload = []byte(`{` + bodyField + `"model":"claude-opus-5","thread":{"type":"continue","previous_message_id":"msg_thread_1"},"thinking":{"type":"disabled"},"messages":[{"role":"user","content":"second"}]}`)
|
||||
if _, errExecute := executor.Execute(ctx, auth, request, opts); errExecute != nil {
|
||||
t.Fatalf("valid caller-owned Thread rejected after %s: %v", missing, errExecute)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestClaudeThreadNativeOAuthResumesWithoutCache(t *testing.T) {
|
||||
for _, stream := range []bool{false, true} {
|
||||
t.Run(fmt.Sprintf("stream=%v", stream), func(t *testing.T) {
|
||||
claudeThreadTestKVStore(t)
|
||||
var sent []string
|
||||
transport := roundTripperFunc(func(req *http.Request) (*http.Response, error) {
|
||||
body, errRead := io.ReadAll(req.Body)
|
||||
if errRead != nil {
|
||||
return nil, errRead
|
||||
}
|
||||
sent = append(sent, strings.Join(helps.HeaderValuesCaseInsensitive(req.Header, "Anthropic-Beta"), ","))
|
||||
if len(sent) > 1 && sent[1] != sent[0] {
|
||||
t.Errorf("native OAuth profile changed after restart: %q", sent)
|
||||
}
|
||||
if ojson.GetBytes(body, "stream").Bool() {
|
||||
return claudeThreadTestResponse(req, http.StatusOK, "text/event-stream", claudeThreadTestSSE("msg_thread_1")), nil
|
||||
}
|
||||
return claudeThreadTestResponse(req, http.StatusOK, "application/json", claudeThreadTestMessage("msg_thread_1")), nil
|
||||
})
|
||||
ctx := context.WithValue(context.Background(), "cliproxy.roundtripper", http.RoundTripper(transport))
|
||||
auth := claudeNativeHelperOAuthAuth("")
|
||||
headers := claudeNativeHelperHeaders("claude-code-20250219,interleaved-thinking-2025-05-14,message-threads-2026-08-12,advanced-tool-use-2025-11-20,effort-2025-11-24,advisor-tool-2026-03-01", "gzip, deflate, br, zstd")
|
||||
opts := cliproxyexecutor.Options{SourceFormat: sdktranslator.FormatClaude, Headers: headers}
|
||||
for turn := range 2 {
|
||||
doc := ojson.NewDocString(`{"model":"claude-opus-5","max_tokens":2048,"thread":{"type":"create"},"messages":[{"role":"user","content":"hello"}]}`)
|
||||
if errSet := doc.Set("betas", []string{"context-1m-2025-08-07"}); errSet != nil {
|
||||
t.Fatal(errSet)
|
||||
}
|
||||
if errSet := doc.Set("metadata.user_id", claudeNativeHelperUserID); errSet != nil {
|
||||
t.Fatal(errSet)
|
||||
}
|
||||
if turn == 1 {
|
||||
if errSet := doc.Set("thread", map[string]string{"type": "continue", "previous_message_id": "msg_thread_1"}); errSet != nil {
|
||||
t.Fatal(errSet)
|
||||
}
|
||||
if errSet := doc.Set("thinking.type", "disabled"); errSet != nil {
|
||||
t.Fatal(errSet)
|
||||
}
|
||||
}
|
||||
request := cliproxyexecutor.Request{Model: "claude-opus-5", Payload: doc.Bytes()}
|
||||
// A new executor on each turn models a restart with no cached state.
|
||||
executor := NewClaudeExecutor(&config.Config{})
|
||||
if stream {
|
||||
result, errStream := executor.ExecuteStream(ctx, auth, request, opts)
|
||||
if errStream != nil {
|
||||
t.Fatalf("turn %d: %v", turn+1, errStream)
|
||||
}
|
||||
for chunk := range result.Chunks {
|
||||
if chunk.Err != nil {
|
||||
t.Fatal(chunk.Err)
|
||||
}
|
||||
}
|
||||
} else if _, errExecute := executor.Execute(ctx, auth, request, opts); errExecute != nil {
|
||||
t.Fatalf("turn %d: %v", turn+1, errExecute)
|
||||
}
|
||||
}
|
||||
if len(sent) != 2 {
|
||||
t.Fatalf("upstream requests = %d, want 2", len(sent))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestClaudeThreadSnapshotCannotUndoUncloak(t *testing.T) {
|
||||
calls := 0
|
||||
transport := roundTripperFunc(func(req *http.Request) (*http.Response, error) {
|
||||
calls++
|
||||
return claudeThreadTestResponse(req, http.StatusOK, "application/json", claudeThreadTestMessage("msg_thread_1")), nil
|
||||
})
|
||||
ctx := context.WithValue(context.Background(), "cliproxy.roundtripper", http.RoundTripper(transport))
|
||||
executor := NewClaudeExecutor(&config.Config{})
|
||||
auth := &cliproxyauth.Auth{ID: t.Name(), Attributes: map[string]string{"api_key": "key-thread-uncloak", "cloak_mode": "always"}}
|
||||
opts := cliproxyexecutor.Options{SourceFormat: sdktranslator.FormatClaude}
|
||||
request := cliproxyexecutor.Request{Model: "claude-opus-5", Payload: []byte(`{"model":"claude-opus-5","thread":{"type":"create"},"messages":[{"role":"user","content":"first"}]}`)}
|
||||
if _, errExecute := executor.Execute(ctx, auth, request, opts); errExecute != nil {
|
||||
t.Fatal(errExecute)
|
||||
}
|
||||
auth.Attributes["cloak_mode"] = "uncloak"
|
||||
request.Payload = []byte(`{"model":"claude-opus-5","thread":{"type":"continue","previous_message_id":"msg_thread_1"},"messages":[{"role":"user","content":"second"}]}`)
|
||||
if _, errExecute := executor.Execute(ctx, auth, request, opts); errExecute == nil {
|
||||
t.Fatal("continuation restored a cloaked profile after explicit uncloak; want a Thread replay error")
|
||||
}
|
||||
if calls != 1 {
|
||||
t.Fatalf("sent %d requests, want no send after incompatible identity policy change", calls)
|
||||
}
|
||||
}
|
||||
|
||||
type claudeThreadGatedReader struct {
|
||||
prefix *strings.Reader
|
||||
suffix *strings.Reader
|
||||
waiting chan struct{}
|
||||
release chan struct{}
|
||||
paused bool
|
||||
}
|
||||
|
||||
func (r *claudeThreadGatedReader) Read(p []byte) (int, error) {
|
||||
if r.prefix.Len() > 0 {
|
||||
return r.prefix.Read(p)
|
||||
}
|
||||
if !r.paused {
|
||||
r.paused = true
|
||||
close(r.waiting)
|
||||
<-r.release
|
||||
}
|
||||
return r.suffix.Read(p)
|
||||
}
|
||||
|
||||
func claudeThreadTestTerminal(payload []byte) bool {
|
||||
for _, line := range strings.Split(string(payload), "\n") {
|
||||
line = strings.TrimSpace(line)
|
||||
if !strings.HasPrefix(line, "data:") && !strings.HasPrefix(line, "{") {
|
||||
continue
|
||||
}
|
||||
root := ojson.Parse(strings.TrimSpace(strings.TrimPrefix(line, "data:")))
|
||||
switch root.Get("type").String() {
|
||||
case "message_delta":
|
||||
return root.Get("delta.stop_reason").String() != ""
|
||||
case "message_stop", "response.completed", "response.incomplete":
|
||||
return true
|
||||
}
|
||||
if value := root.Get("choices.0.finish_reason"); value.Exists() && value.Type != ojson.Null {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func claudeThreadTestResponse(req *http.Request, status int, contentType, body string) *http.Response {
|
||||
return &http.Response{
|
||||
StatusCode: status,
|
||||
Header: http.Header{"Content-Type": []string{contentType}},
|
||||
Body: io.NopCloser(strings.NewReader(body)),
|
||||
Request: req,
|
||||
}
|
||||
}
|
||||
|
||||
func claudeThreadTestMessage(id string) string {
|
||||
return fmt.Sprintf(`{"id":%q,"type":"message","model":"claude-opus-5","role":"assistant","content":[{"type":"text","text":"ok"}],"stop_reason":"end_turn","stop_sequence":null,"usage":{"input_tokens":1,"output_tokens":1}}`, id)
|
||||
}
|
||||
|
||||
func claudeThreadTestSSE(id string) string {
|
||||
return fmt.Sprintf("event: message_start\ndata: {\"type\":\"message_start\",\"message\":{\"id\":%q,\"type\":\"message\",\"model\":\"claude-opus-5\",\"role\":\"assistant\",\"content\":[],\"stop_reason\":null,\"stop_sequence\":null,\"usage\":{\"input_tokens\":1,\"output_tokens\":0}}}\n\nevent: content_block_start\ndata: {\"type\":\"content_block_start\",\"index\":0,\"content_block\":{\"type\":\"text\",\"text\":\"\"}}\n\nevent: content_block_delta\ndata: {\"type\":\"content_block_delta\",\"index\":0,\"delta\":{\"type\":\"text_delta\",\"text\":\"ok\"}}\n\nevent: content_block_stop\ndata: {\"type\":\"content_block_stop\",\"index\":0}\n\nevent: message_delta\ndata: {\"type\":\"message_delta\",\"delta\":{\"stop_reason\":\"end_turn\",\"stop_sequence\":null},\"usage\":{\"output_tokens\":1}}\n\nevent: message_stop\ndata: {\"type\":\"message_stop\"}\n\n", id)
|
||||
}
|
||||
308
internal/runtime/executor/helps/claude_thread.go
Normal file
308
internal/runtime/executor/helps/claude_thread.go
Normal file
@@ -0,0 +1,308 @@
|
||||
package helps
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/MoeForge/moego/ojson"
|
||||
"github.com/router-for-me/CLIProxyAPI/v8/internal/clienterror"
|
||||
"github.com/router-for-me/CLIProxyAPI/v8/internal/kvstore"
|
||||
cliproxysession "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/session"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
const (
|
||||
claudeThreadBetaLimit = 1024
|
||||
claudeThreadBetaTTL = cliproxysession.ClaudeThreadStateTTL
|
||||
claudeThreadEventLimit = 52_428_800
|
||||
)
|
||||
|
||||
type claudeThreadBetaKey struct {
|
||||
scope string
|
||||
messageID string
|
||||
}
|
||||
|
||||
type claudeThreadBetaProfile struct {
|
||||
betas string
|
||||
policy string
|
||||
bodyBetas string
|
||||
}
|
||||
|
||||
// ClaudeThreadBetaPolicy separates explicit payload rules from caller declarations.
|
||||
type ClaudeThreadBetaPolicy struct {
|
||||
Fingerprint string
|
||||
BodyBetas string
|
||||
PayloadConfigured bool
|
||||
}
|
||||
|
||||
// ClaudeThreadBetaConflictError requires replay when a fixed Thread profile
|
||||
// cannot satisfy the current explicit policy or declared protocol capabilities.
|
||||
type ClaudeThreadBetaConflictError struct{}
|
||||
|
||||
func (ClaudeThreadBetaConflictError) Error() string {
|
||||
return "The Thread beta profile conflicts with the current header policy or cannot be reconstructed. Replay the full conversation with thread create."
|
||||
}
|
||||
|
||||
func (ClaudeThreadBetaConflictError) StatusCode() int { return http.StatusConflict }
|
||||
|
||||
func (ClaudeThreadBetaConflictError) IsRequestScoped() bool { return true }
|
||||
|
||||
// ClaudeThreadBetaStore keeps successful Thread wire profiles bounded and shared
|
||||
// across credential-scoped executor copies. Empty profiles are valid entries.
|
||||
type ClaudeThreadBetaStore struct {
|
||||
mu sync.Mutex
|
||||
entries map[claudeThreadBetaKey]claudeThreadBetaProfile
|
||||
order []claudeThreadBetaKey
|
||||
}
|
||||
|
||||
// ClaudeThreadBetaRequest binds a request to the profile sent to its upstream.
|
||||
type ClaudeThreadBetaRequest struct {
|
||||
store *ClaudeThreadBetaStore
|
||||
scope string
|
||||
betas string
|
||||
policy string
|
||||
bodyBetas string
|
||||
onCommit func(string)
|
||||
}
|
||||
|
||||
// OnCommit registers routing state that must be visible before completion.
|
||||
func (r *ClaudeThreadBetaRequest) OnCommit(commit func(string)) {
|
||||
if r != nil {
|
||||
r.onCommit = commit
|
||||
}
|
||||
}
|
||||
|
||||
// Bind restores only an actual successful profile, recovering it from the
|
||||
// configured KV store after memory eviction or restart. Unknown profiles are
|
||||
// never guessed from the current turn's potentially different capabilities.
|
||||
func (s *ClaudeThreadBetaStore) Bind(scope, previousMessageID string, header http.Header, policy ClaudeThreadBetaPolicy) (*ClaudeThreadBetaRequest, bool, error) {
|
||||
betas := strings.Join(HeaderValuesCaseInsensitive(header, "Anthropic-Beta"), ",")
|
||||
bodyBetas := policy.BodyBetas
|
||||
if previousMessageID != "" {
|
||||
key := claudeThreadBetaKey{scope: scope, messageID: previousMessageID}
|
||||
s.mu.Lock()
|
||||
previous, ok := s.entries[key]
|
||||
s.mu.Unlock()
|
||||
if !ok {
|
||||
client, kvMode, errClient := kvstore.CurrentKVClient()
|
||||
if errClient != nil {
|
||||
log.WithError(errClient).Warn("Claude Thread beta store unavailable")
|
||||
return nil, false, ClaudeThreadBetaConflictError{}
|
||||
}
|
||||
if !kvMode || client == nil {
|
||||
return nil, false, nil
|
||||
}
|
||||
raw, found, errGet := client.KVGet(context.Background(), claudeThreadBetaKVKey(key))
|
||||
if errGet != nil {
|
||||
log.WithError(errGet).Warn("Claude Thread beta profile load failed")
|
||||
return nil, false, ClaudeThreadBetaConflictError{}
|
||||
}
|
||||
if !found {
|
||||
return nil, false, nil
|
||||
}
|
||||
storedPolicy, rest, validPolicy := strings.Cut(string(raw), "\n")
|
||||
storedBodyBetas, storedBetas, validBetas := strings.Cut(rest, "\n")
|
||||
if !validPolicy || !validBetas || strings.ContainsAny(storedBetas+storedBodyBetas, "\r\n") {
|
||||
return nil, false, ClaudeThreadBetaConflictError{}
|
||||
}
|
||||
previous = claudeThreadBetaProfile{betas: storedBetas, policy: storedPolicy, bodyBetas: storedBodyBetas}
|
||||
s.save(key, previous)
|
||||
}
|
||||
payloadConflict := false
|
||||
if policy.PayloadConfigured {
|
||||
current := claudeThreadBetaNames(betas)
|
||||
frozen := claudeThreadBetaNames(previous.betas)
|
||||
for beta := range claudeThreadBetaNames(previous.bodyBetas) {
|
||||
payloadConflict = payloadConflict || !current[beta]
|
||||
}
|
||||
for beta := range claudeThreadBetaNames(policy.BodyBetas) {
|
||||
payloadConflict = payloadConflict || !frozen[beta]
|
||||
}
|
||||
}
|
||||
if (previous.policy != policy.Fingerprint && previous.betas != betas) || payloadConflict {
|
||||
return nil, true, ClaudeThreadBetaConflictError{}
|
||||
}
|
||||
betas = previous.betas
|
||||
bodyBetas = previous.bodyBetas
|
||||
for name := range header {
|
||||
if strings.EqualFold(name, "Anthropic-Beta") {
|
||||
delete(header, name)
|
||||
}
|
||||
}
|
||||
if betas != "" {
|
||||
header.Set("Anthropic-Beta", betas)
|
||||
}
|
||||
}
|
||||
return &ClaudeThreadBetaRequest{store: s, scope: scope, betas: betas, policy: policy.Fingerprint, bodyBetas: bodyBetas}, true, nil
|
||||
}
|
||||
|
||||
// Commit publishes only a successful response ID. The existing KV store keeps
|
||||
// exact snapshots recoverable across cache eviction and ordinary server restarts.
|
||||
func (r *ClaudeThreadBetaRequest) Commit(messageID string) {
|
||||
if r == nil || messageID == "" {
|
||||
return
|
||||
}
|
||||
key := claudeThreadBetaKey{scope: r.scope, messageID: messageID}
|
||||
if r.store.save(key, claudeThreadBetaProfile{betas: r.betas, policy: r.policy, bodyBetas: r.bodyBetas}) {
|
||||
// Policy is hex; the delimiters cannot occur in HTTP header values.
|
||||
kvstore.KVSetNXBestEffort(context.Background(), claudeThreadBetaKVKey(key), []byte(r.policy+"\n"+r.bodyBetas+"\n"+r.betas), claudeThreadBetaTTL)
|
||||
}
|
||||
if r.onCommit != nil {
|
||||
r.onCommit(messageID)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *ClaudeThreadBetaStore) save(key claudeThreadBetaKey, profile claudeThreadBetaProfile) bool {
|
||||
key.messageID = strings.Clone(key.messageID)
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
if s.entries == nil {
|
||||
s.entries = make(map[claudeThreadBetaKey]claudeThreadBetaProfile)
|
||||
}
|
||||
if _, exists := s.entries[key]; exists {
|
||||
return false
|
||||
}
|
||||
s.entries[key] = profile
|
||||
s.order = append(s.order, key)
|
||||
if len(s.order) > claudeThreadBetaLimit {
|
||||
delete(s.entries, s.order[0])
|
||||
s.order = s.order[1:]
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func claudeThreadBetaNames(betas string) map[string]bool {
|
||||
names := make(map[string]bool)
|
||||
for _, beta := range strings.Split(betas, ",") {
|
||||
if beta = strings.TrimSpace(beta); beta != "" {
|
||||
names[beta] = true
|
||||
}
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
||||
func claudeThreadBetaKVKey(key claudeThreadBetaKey) string {
|
||||
return "claude:thread-beta:v1:" + kvstore.HashKeyPart(key.scope) + ":" + kvstore.HashKeyPart(key.messageID)
|
||||
}
|
||||
|
||||
// StreamReader withholds terminal events until message_stop confirms success.
|
||||
// This is before translation, so even translators that finish on message_delta
|
||||
// cannot expose completion before the next turn can load its Thread profile.
|
||||
func (r *ClaudeThreadBetaRequest) StreamReader(body io.Reader, beforeComplete func(string)) io.Reader {
|
||||
if r == nil {
|
||||
return body
|
||||
}
|
||||
scanner := bufio.NewScanner(body)
|
||||
scanner.Buffer(nil, claudeThreadEventLimit)
|
||||
return &claudeThreadStreamReader{scanner: scanner, commit: func(messageID string) {
|
||||
if beforeComplete != nil {
|
||||
beforeComplete(messageID)
|
||||
}
|
||||
r.Commit(messageID)
|
||||
}}
|
||||
}
|
||||
|
||||
type claudeThreadStreamReader struct {
|
||||
scanner *bufio.Scanner
|
||||
commit func(string)
|
||||
ready bytes.Buffer
|
||||
pending bytes.Buffer
|
||||
messageID string
|
||||
failed bool
|
||||
finished bool
|
||||
}
|
||||
|
||||
func (r *claudeThreadStreamReader) Read(p []byte) (int, error) {
|
||||
if len(p) == 0 {
|
||||
return 0, nil
|
||||
}
|
||||
for r.ready.Len() == 0 {
|
||||
if r.finished {
|
||||
return 0, io.EOF
|
||||
}
|
||||
var event bytes.Buffer
|
||||
for r.scanner.Scan() {
|
||||
line := r.scanner.Bytes()
|
||||
event.Write(line)
|
||||
event.WriteByte('\n')
|
||||
if event.Len() > claudeThreadEventLimit {
|
||||
return 0, errors.New("Claude Thread event exceeds buffer limit")
|
||||
}
|
||||
if len(bytes.TrimSpace(line)) == 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
if event.Len() == 0 {
|
||||
if errScan := r.scanner.Err(); errScan != nil {
|
||||
return 0, errScan
|
||||
}
|
||||
// Never release a completion signal from a truncated Thread turn.
|
||||
return 0, io.ErrUnexpectedEOF
|
||||
}
|
||||
payload := claudeThreadEventPayload(event.Bytes())
|
||||
if bytes.HasPrefix(payload, []byte("{")) && !ojson.ValidBytes(payload) {
|
||||
return 0, errors.New("malformed Claude Thread stream event")
|
||||
}
|
||||
if r.pending.Len()+event.Len() > claudeThreadEventLimit {
|
||||
return 0, errors.New("Claude Thread completion exceeds buffer limit")
|
||||
}
|
||||
root := ojson.ParseBytes(payload)
|
||||
typ := root.Get("type").String()
|
||||
terminal := false
|
||||
if _, blocked := clienterror.ClaudePolicyRefusalMessage(payload); blocked {
|
||||
r.failed = true
|
||||
}
|
||||
switch typ {
|
||||
case "message_start":
|
||||
r.messageID = strings.Clone(root.Get("message.id").String())
|
||||
case "message_delta":
|
||||
terminal = root.Get("delta.stop_reason").String() != ""
|
||||
case "error":
|
||||
r.failed = true
|
||||
// Drop any speculative completion and let the executor report the error.
|
||||
r.pending.Reset()
|
||||
case "message_stop":
|
||||
r.finished = true
|
||||
if !r.failed && r.messageID != "" {
|
||||
r.commit(r.messageID)
|
||||
}
|
||||
case "message":
|
||||
if root.Get("stop_reason").String() != "" {
|
||||
r.finished = true
|
||||
if !r.failed {
|
||||
r.commit(root.Get("id").String())
|
||||
}
|
||||
}
|
||||
}
|
||||
if terminal || r.pending.Len() > 0 {
|
||||
r.pending.Write(event.Bytes())
|
||||
if r.finished {
|
||||
r.ready.Write(r.pending.Bytes())
|
||||
r.pending.Reset()
|
||||
}
|
||||
} else {
|
||||
r.ready.Write(event.Bytes())
|
||||
}
|
||||
}
|
||||
return r.ready.Read(p)
|
||||
}
|
||||
|
||||
func claudeThreadEventPayload(event []byte) []byte {
|
||||
var lines [][]byte
|
||||
for _, line := range bytes.Split(event, []byte("\n")) {
|
||||
line = bytes.TrimSpace(line)
|
||||
if bytes.HasPrefix(line, []byte("data:")) {
|
||||
lines = append(lines, bytes.TrimSpace(line[len("data:"):]))
|
||||
}
|
||||
}
|
||||
if len(lines) == 0 {
|
||||
return bytes.TrimSpace(event)
|
||||
}
|
||||
return bytes.Join(lines, []byte("\n"))
|
||||
}
|
||||
128
internal/runtime/executor/helps/claude_thread_test.go
Normal file
128
internal/runtime/executor/helps/claude_thread_test.go
Normal file
@@ -0,0 +1,128 @@
|
||||
package helps
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"testing/iotest"
|
||||
)
|
||||
|
||||
func TestClaudeThreadBetaStoreRestoresWireProfile(t *testing.T) {
|
||||
for _, betas := range []string{"", "message-threads-2026-08-12,effort-2025-11-24"} {
|
||||
t.Run(betas, func(t *testing.T) {
|
||||
store := &ClaudeThreadBetaStore{}
|
||||
first, ok, _ := store.Bind("caller-a", "", http.Header{"Anthropic-Beta": []string{betas}}, ClaudeThreadBetaPolicy{})
|
||||
if !ok {
|
||||
t.Fatal("create was rejected")
|
||||
}
|
||||
first.Commit("message-a")
|
||||
for _, scope := range []string{"caller-b", "other-credential"} {
|
||||
if _, found, _ := store.Bind(scope, "message-a", http.Header{}, ClaudeThreadBetaPolicy{}); found {
|
||||
t.Fatalf("profile leaked into scope %q", scope)
|
||||
}
|
||||
}
|
||||
header := http.Header{"Anthropic-Beta": []string{"derived-beta"}, "anthropic-beta": []string{"custom-beta"}}
|
||||
second, found, _ := store.Bind("caller-a", "message-a", header, ClaudeThreadBetaPolicy{})
|
||||
if !found || header.Get("Anthropic-Beta") != betas {
|
||||
t.Fatalf("restored header = %v, found=%v, want %q", header, found, betas)
|
||||
}
|
||||
if _, duplicate := header["anthropic-beta"]; duplicate {
|
||||
t.Fatal("restoring retained a duplicate wire-cased header")
|
||||
}
|
||||
second.Commit("message-b")
|
||||
thirdHeader := http.Header{"Anthropic-Beta": []string{"new-body-beta"}}
|
||||
if _, foundThird, _ := store.Bind("caller-a", "message-b", thirdHeader, ClaudeThreadBetaPolicy{}); !foundThird || thirdHeader.Get("Anthropic-Beta") != betas {
|
||||
t.Fatalf("third-turn profile = %v, found=%v", thirdHeader, foundThird)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestClaudeThreadBetaStoreEvictsOldestMessage(t *testing.T) {
|
||||
store := &ClaudeThreadBetaStore{}
|
||||
for i := range claudeThreadBetaLimit + 1 {
|
||||
request, _, _ := store.Bind("scope", "", http.Header{"Anthropic-Beta": []string{"stable-beta"}}, ClaudeThreadBetaPolicy{})
|
||||
request.Commit(fmt.Sprintf("message-%d", i))
|
||||
}
|
||||
if _, found, _ := store.Bind("scope", "message-0", http.Header{}, ClaudeThreadBetaPolicy{}); found {
|
||||
t.Fatal("oldest profile was not evicted")
|
||||
}
|
||||
if _, found, _ := store.Bind("scope", fmt.Sprintf("message-%d", claudeThreadBetaLimit), http.Header{}, ClaudeThreadBetaPolicy{}); !found {
|
||||
t.Fatal("newest profile was evicted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestClaudeThreadBetaStoreConcurrentCallers(t *testing.T) {
|
||||
store := &ClaudeThreadBetaStore{}
|
||||
var wg sync.WaitGroup
|
||||
for caller := range 16 {
|
||||
wg.Go(func() {
|
||||
scope := fmt.Sprintf("caller-%d", caller)
|
||||
request, _, _ := store.Bind(scope, "", http.Header{"Anthropic-Beta": []string{scope}}, ClaudeThreadBetaPolicy{})
|
||||
request.Commit("shared-message-id")
|
||||
for range 10 {
|
||||
header := http.Header{}
|
||||
if _, found, _ := store.Bind(scope, "shared-message-id", header, ClaudeThreadBetaPolicy{}); !found || header.Get("Anthropic-Beta") != scope {
|
||||
t.Errorf("profile crossed callers: %v, scope=%s", header, scope)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
func TestClaudeThreadOversizedCompletionDoesNotPublishState(t *testing.T) {
|
||||
const start = "data: {\"type\":\"message_start\",\"message\":{\"id\":\"message-a\"}}\n\n"
|
||||
const terminal = "data: {\"type\":\"message_delta\",\"delta\":{\"stop_reason\":\"end_turn\"}}\n\n"
|
||||
const stop = "data: {\"type\":\"message_stop\"}\n\n"
|
||||
const pingPrefix = "data: {\"type\":\"ping\",\"padding\":\""
|
||||
const pingSuffix = "\"}\n\n"
|
||||
// Each event fits, but message_stop pushes the pending completion over its cap.
|
||||
pingSize := (claudeThreadEventLimit - 1 - len(terminal)) / 2
|
||||
ping := pingPrefix + strings.Repeat("x", pingSize-len(pingPrefix)-len(pingSuffix)) + pingSuffix
|
||||
body := io.MultiReader(strings.NewReader(start+terminal), strings.NewReader(ping), strings.NewReader(ping), strings.NewReader(stop))
|
||||
store := &ClaudeThreadBetaStore{}
|
||||
request, _, _ := store.Bind("scope", "", http.Header{"Anthropic-Beta": []string{"profile"}}, ClaudeThreadBetaPolicy{})
|
||||
if _, errRead := io.Copy(io.Discard, request.StreamReader(body, nil)); errRead == nil {
|
||||
t.Fatal("oversized completion was accepted")
|
||||
}
|
||||
if _, found, _ := store.Bind("scope", "message-a", http.Header{}, ClaudeThreadBetaPolicy{}); found {
|
||||
t.Fatal("rejected oversized completion published Thread state")
|
||||
}
|
||||
}
|
||||
|
||||
func TestClaudeThreadStreamNeverCommitsFailedTurn(t *testing.T) {
|
||||
const start = "data: {\"type\":\"message_start\",\"message\":{\"id\":\"message-a\"}}\n\n"
|
||||
const terminal = "data: {\"type\":\"message_delta\",\"delta\":{\"stop_reason\":\"end_turn\"}}\n\n"
|
||||
const stop = "data: {\"type\":\"message_stop\"}\n\n"
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
body io.Reader
|
||||
}{
|
||||
{name: "truncated", body: strings.NewReader(start + terminal)},
|
||||
{name: "malformed stop", body: strings.NewReader(start + terminal + "data: {\"type\":\"message_stop\",broken}\n\n")},
|
||||
{name: "error after terminal", body: strings.NewReader(start + terminal + "data: {\"type\":\"error\",\"error\":{\"message\":\"failed\"}}\n\n")},
|
||||
{name: "policy refusal", body: strings.NewReader(start + "data: {\"type\":\"message_delta\",\"delta\":{\"stop_reason\":\"refusal\",\"stop_details\":{\"type\":\"refusal\",\"category\":\"cyber\"}}}\n\n" + stop)},
|
||||
{name: "canceled", body: io.MultiReader(strings.NewReader(start), iotest.ErrReader(context.Canceled))},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
store := &ClaudeThreadBetaStore{}
|
||||
request, _, _ := store.Bind("scope", "", http.Header{"Anthropic-Beta": []string{"profile"}}, ClaudeThreadBetaPolicy{})
|
||||
data, errRead := io.ReadAll(request.StreamReader(test.body, nil))
|
||||
if test.name == "canceled" && !errors.Is(errRead, context.Canceled) {
|
||||
t.Fatalf("cancellation error = %v", errRead)
|
||||
}
|
||||
if test.name == "truncated" && (errRead == nil || strings.Contains(string(data), "message_delta")) {
|
||||
t.Fatalf("truncated turn released completion: data=%s, error=%v", data, errRead)
|
||||
}
|
||||
if _, found, _ := store.Bind("scope", "message-a", http.Header{}, ClaudeThreadBetaPolicy{}); found {
|
||||
t.Fatal("failed turn published continuation state")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -42,6 +42,7 @@ func NewKimiExecutor(cfg *config.Config) *KimiExecutor {
|
||||
requestLogProvider: "kimi",
|
||||
upstreamModelNormalizer: normalizeKimiUpstreamModel,
|
||||
oauthToolAliases: &claudeOAuthToolAliasStore{},
|
||||
threadBetas: &helps.ClaudeThreadBetaStore{},
|
||||
},
|
||||
cfg: cfg,
|
||||
}
|
||||
|
||||
56
sdk/cliproxy/auth/conductor_claude_thread.go
Normal file
56
sdk/cliproxy/auth/conductor_claude_thread.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"github.com/MoeForge/moego/ojson"
|
||||
cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/executor"
|
||||
cliproxysession "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/session"
|
||||
coreusage "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/usage"
|
||||
sdktranslator "github.com/router-for-me/CLIProxyAPI/v8/sdk/translator"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// withClaudeThreadAuth pins continuations before ordinary selection, including
|
||||
// default round-robin. It never bypasses the selector's availability checks.
|
||||
func withClaudeThreadAuth(ctx context.Context, providers []string, req cliproxyexecutor.Request, opts cliproxyexecutor.Options) (cliproxyexecutor.Options, error) {
|
||||
if opts.SourceFormat != sdktranslator.FormatClaude {
|
||||
return opts, nil
|
||||
}
|
||||
hasClaude := false
|
||||
for _, provider := range providers {
|
||||
if provider == "claude" {
|
||||
hasClaude = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !hasClaude {
|
||||
return opts, nil
|
||||
}
|
||||
previousID := ojson.GetBytesNoCopy(req.Payload, "thread.previous_message_id").String()
|
||||
if previousID == "" {
|
||||
return opts, nil
|
||||
}
|
||||
callerScope := cliproxysession.ClaudeThreadCallerScope(coreusage.APIKeyFromContext(ctx), opts.Metadata, req.Metadata)
|
||||
owner, found, errOwner := cliproxysession.ClaudeThreadAuth(ctx, callerScope, previousID)
|
||||
if errOwner != nil {
|
||||
if ctx != nil && ctx.Err() != nil {
|
||||
return opts, ctx.Err()
|
||||
}
|
||||
log.WithError(errOwner).Warn("Claude Thread credential binding lookup failed")
|
||||
return opts, &Error{Code: "thread_auth_unavailable", Message: "Claude Thread credential binding is temporarily unavailable", Retryable: true, HTTPStatus: http.StatusServiceUnavailable}
|
||||
}
|
||||
if !found {
|
||||
return opts, nil
|
||||
}
|
||||
if pinned := pinnedAuthIDFromMetadata(opts.Metadata); pinned != "" && pinned != owner {
|
||||
return opts, &Error{Code: "thread_auth_conflict", Message: "the pinned credential does not own this Claude Thread; replay with thread create", HTTPStatus: http.StatusConflict}
|
||||
}
|
||||
opts.Metadata = cloneRequestMetadata(opts.Metadata)
|
||||
if opts.Metadata == nil {
|
||||
opts.Metadata = make(map[string]any)
|
||||
}
|
||||
opts.Metadata[cliproxyexecutor.PinnedAuthMetadataKey] = owner
|
||||
return opts, nil
|
||||
}
|
||||
@@ -208,6 +208,10 @@ func (m *Manager) Execute(ctx context.Context, providers []string, req cliproxye
|
||||
if len(normalized) == 0 {
|
||||
return cliproxyexecutor.Response{}, &Error{Code: "provider_not_found", Message: "no provider supplied"}
|
||||
}
|
||||
opts, errThread := withClaudeThreadAuth(ctx, normalized, req, opts)
|
||||
if errThread != nil {
|
||||
return cliproxyexecutor.Response{}, errThread
|
||||
}
|
||||
if m.HomeEnabled() {
|
||||
resp, errHome := m.executeHome(ctx, normalized, req, opts, false)
|
||||
return resp, unwrapExecutionBoundaryError(errHome)
|
||||
@@ -348,6 +352,10 @@ func (m *Manager) ExecuteStream(ctx context.Context, providers []string, req cli
|
||||
if len(normalized) == 0 {
|
||||
return nil, &Error{Code: "provider_not_found", Message: "no provider supplied"}
|
||||
}
|
||||
opts, errThread := withClaudeThreadAuth(ctx, normalized, req, opts)
|
||||
if errThread != nil {
|
||||
return nil, errThread
|
||||
}
|
||||
|
||||
defaultRequestRetry, maxRetryCredentials, maxWait := m.retrySettings()
|
||||
|
||||
|
||||
62
sdk/cliproxy/service_claude_thread_test.go
Normal file
62
sdk/cliproxy/service_claude_thread_test.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package cliproxy
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/router-for-me/CLIProxyAPI/v8/internal/runtime/executor/helps"
|
||||
coreauth "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/auth"
|
||||
cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/executor"
|
||||
"github.com/router-for-me/CLIProxyAPI/v8/sdk/config"
|
||||
sdktranslator "github.com/router-for-me/CLIProxyAPI/v8/sdk/translator"
|
||||
)
|
||||
|
||||
func TestClaudeThreadSurvivesExecutorRebinding(t *testing.T) {
|
||||
for _, force := range []bool{false, true} {
|
||||
t.Run(map[bool]string{false: "credential update", true: "config reload"}[force], func(t *testing.T) {
|
||||
service := &Service{cfg: &config.Config{}, coreManager: coreauth.NewManager(nil, nil, nil)}
|
||||
auth := &coreauth.Auth{ID: t.Name(), Provider: "claude", Attributes: map[string]string{"api_key": "key-service-thread"}}
|
||||
service.registerExecutorForAuth(auth, false)
|
||||
first, found := service.coreManager.Executor("claude")
|
||||
if !found {
|
||||
t.Fatal("Claude executor was not registered")
|
||||
}
|
||||
calls := 0
|
||||
transport := claudeThreadServiceRoundTripper(func(req *http.Request) (*http.Response, error) {
|
||||
calls++
|
||||
if got := strings.Join(helps.HeaderValuesCaseInsensitive(req.Header, "Anthropic-Beta"), ","); got != "message-threads-2026-08-12,effort-2025-11-24" {
|
||||
t.Errorf("wire beta = %q, want original Thread profile", got)
|
||||
}
|
||||
return &http.Response{StatusCode: http.StatusOK, Header: http.Header{"Content-Type": []string{"application/json"}}, Body: io.NopCloser(strings.NewReader(`{"id":"msg_service_thread","type":"message","role":"assistant","model":"claude-opus-5","content":[{"type":"text","text":"ok"}],"stop_reason":"end_turn","usage":{"input_tokens":1,"output_tokens":1}}`)), Request: req}, nil
|
||||
})
|
||||
ctx := context.WithValue(context.Background(), "cliproxy.roundtripper", http.RoundTripper(transport))
|
||||
opts := cliproxyexecutor.Options{SourceFormat: sdktranslator.FormatClaude, Headers: http.Header{"Anthropic-Beta": []string{"message-threads-2026-08-12,effort-2025-11-24"}}}
|
||||
request := cliproxyexecutor.Request{Model: "claude-opus-5", Payload: []byte(`{"model":"claude-opus-5","thread":{"type":"create"},"messages":[{"role":"user","content":"first"}]}`)}
|
||||
if _, errExecute := first.Execute(ctx, auth, request, opts); errExecute != nil {
|
||||
t.Fatal(errExecute)
|
||||
}
|
||||
service.cfg = &config.Config{}
|
||||
service.registerExecutorForAuth(&coreauth.Auth{ID: "unrelated-credential", Provider: "claude"}, force)
|
||||
second, foundSecond := service.coreManager.Executor("claude")
|
||||
if !foundSecond || second == first {
|
||||
t.Fatal("test requires a freshly rebound executor")
|
||||
}
|
||||
request.Payload = []byte(`{"model":"claude-opus-5","thread":{"type":"continue","previous_message_id":"msg_service_thread"},"thinking":{"type":"disabled"},"messages":[{"role":"user","content":"second"}]}`)
|
||||
if _, errExecute := second.Execute(ctx, auth, request, opts); errExecute != nil {
|
||||
t.Fatalf("active Thread lost after unrelated executor rebinding: %v", errExecute)
|
||||
}
|
||||
if calls != 2 {
|
||||
t.Fatalf("upstream requests = %d, want 2", calls)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
type claudeThreadServiceRoundTripper func(*http.Request) (*http.Response, error)
|
||||
|
||||
func (f claudeThreadServiceRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
return f(req)
|
||||
}
|
||||
@@ -309,6 +309,12 @@ func (s *Service) registerExecutorForAuth(a *coreauth.Auth, forceReplace bool) {
|
||||
case "antigravity":
|
||||
s.coreManager.RegisterExecutor(executor.NewAntigravityExecutor(cfg))
|
||||
case "claude":
|
||||
if existing, found := s.coreManager.Executor("claude"); found {
|
||||
if previous, ok := existing.(*executor.ClaudeExecutor); ok {
|
||||
s.coreManager.RegisterExecutor(previous.WithConfig(cfg))
|
||||
return
|
||||
}
|
||||
}
|
||||
s.coreManager.RegisterExecutor(executor.NewClaudeExecutor(cfg))
|
||||
case "kimi", "kimi-ai", "kimi.ai", "kimi.com":
|
||||
s.coreManager.RegisterExecutor(executor.NewKimiExecutor(cfg))
|
||||
|
||||
107
sdk/cliproxy/session/claude_thread.go
Normal file
107
sdk/cliproxy/session/claude_thread.go
Normal file
@@ -0,0 +1,107 @@
|
||||
package session
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/router-for-me/CLIProxyAPI/v8/internal/kvstore"
|
||||
cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/executor"
|
||||
)
|
||||
|
||||
// ClaudeThreadStateTTL bounds recoverable Thread routing and wire-profile state.
|
||||
const ClaudeThreadStateTTL = 24 * time.Hour
|
||||
|
||||
const claudeThreadAuthLimit = 1024
|
||||
|
||||
type claudeThreadAuthEntry struct {
|
||||
authID string
|
||||
expiresAt time.Time
|
||||
}
|
||||
|
||||
var claudeThreadAuthCache = struct {
|
||||
sync.Mutex
|
||||
entries map[string]claudeThreadAuthEntry
|
||||
order []string
|
||||
}{entries: make(map[string]claudeThreadAuthEntry)}
|
||||
|
||||
// ClaudeThreadCallerScope uses the same authenticated caller namespace at the
|
||||
// execution and routing boundaries, without depending on a conversation ID.
|
||||
func ClaudeThreadCallerScope(apiKey string, metadataSets ...map[string]any) string {
|
||||
for _, metadata := range metadataSets {
|
||||
if scope := metadataString(metadata, cliproxyexecutor.CallerScopeMetadataKey); scope != "" {
|
||||
return scope
|
||||
}
|
||||
}
|
||||
if scope := CallerScope(apiKey); scope != "" {
|
||||
return scope
|
||||
}
|
||||
return "cpa-claude-thread-default-caller"
|
||||
}
|
||||
|
||||
func claudeThreadAuthKey(callerScope, messageID string) string {
|
||||
return "claude:thread-auth:v1:" + kvstore.HashKeyPart(callerScope) + ":" + kvstore.HashKeyPart(messageID)
|
||||
}
|
||||
|
||||
func cacheClaudeThreadAuth(key, authID string) {
|
||||
now := time.Now()
|
||||
claudeThreadAuthCache.Lock()
|
||||
defer claudeThreadAuthCache.Unlock()
|
||||
entry, exists := claudeThreadAuthCache.entries[key]
|
||||
if exists && now.Before(entry.expiresAt) {
|
||||
return
|
||||
}
|
||||
if !exists {
|
||||
claudeThreadAuthCache.order = append(claudeThreadAuthCache.order, key)
|
||||
}
|
||||
claudeThreadAuthCache.entries[key] = claudeThreadAuthEntry{authID: strings.Clone(authID), expiresAt: now.Add(ClaudeThreadStateTTL)}
|
||||
if len(claudeThreadAuthCache.order) > claudeThreadAuthLimit {
|
||||
delete(claudeThreadAuthCache.entries, claudeThreadAuthCache.order[0])
|
||||
claudeThreadAuthCache.order = claudeThreadAuthCache.order[1:]
|
||||
}
|
||||
}
|
||||
|
||||
// RememberClaudeThreadAuth publishes ownership only after upstream success.
|
||||
// Existing generic KV persistence keeps the binding available after a restart.
|
||||
func RememberClaudeThreadAuth(callerScope, messageID, authID string) {
|
||||
if callerScope == "" || messageID == "" || authID == "" {
|
||||
return
|
||||
}
|
||||
key := claudeThreadAuthKey(callerScope, messageID)
|
||||
cacheClaudeThreadAuth(key, authID)
|
||||
kvstore.KVSetNXBestEffort(context.Background(), key, []byte(authID), ClaudeThreadStateTTL)
|
||||
}
|
||||
|
||||
// ClaudeThreadAuth returns a caller-isolated routing hint, not an authorization
|
||||
// grant. The normal selector still validates credential availability and models.
|
||||
func ClaudeThreadAuth(ctx context.Context, callerScope, messageID string) (string, bool, error) {
|
||||
if callerScope == "" || messageID == "" {
|
||||
return "", false, nil
|
||||
}
|
||||
key := claudeThreadAuthKey(callerScope, messageID)
|
||||
claudeThreadAuthCache.Lock()
|
||||
entry, found := claudeThreadAuthCache.entries[key]
|
||||
claudeThreadAuthCache.Unlock()
|
||||
if found && time.Now().Before(entry.expiresAt) {
|
||||
return entry.authID, true, nil
|
||||
}
|
||||
client, kvMode, errClient := kvstore.CurrentKVClient()
|
||||
if !kvMode || errClient != nil || client == nil {
|
||||
return "", false, errClient
|
||||
}
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
raw, foundKV, errGet := client.KVGet(ctx, key)
|
||||
if errGet != nil || !foundKV {
|
||||
return "", false, errGet
|
||||
}
|
||||
authID := strings.TrimSpace(string(raw))
|
||||
if authID == "" {
|
||||
return "", false, errors.New("empty Claude Thread credential binding")
|
||||
}
|
||||
cacheClaudeThreadAuth(key, authID)
|
||||
return authID, true, nil
|
||||
}
|
||||
78
sdk/cliproxy/session/claude_thread_test.go
Normal file
78
sdk/cliproxy/session/claude_thread_test.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package session
|
||||
|
||||
import (
|
||||
"context"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/router-for-me/CLIProxyAPI/v8/internal/kvstore"
|
||||
)
|
||||
|
||||
func TestClaudeThreadAuthRecoversFromKVWithCallerIsolation(t *testing.T) {
|
||||
if errShutdown := kvstore.ShutdownLocal(); errShutdown != nil {
|
||||
t.Fatal(errShutdown)
|
||||
}
|
||||
path := filepath.Join(t.TempDir(), "kv.db")
|
||||
if errConfigure := kvstore.ConfigureLocal(true, path); errConfigure != nil {
|
||||
t.Fatal(errConfigure)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
if errShutdown := kvstore.ShutdownLocal(); errShutdown != nil {
|
||||
t.Error(errShutdown)
|
||||
}
|
||||
})
|
||||
scope := t.Name()
|
||||
RememberClaudeThreadAuth(scope, "message-a", "credential-a")
|
||||
key := claudeThreadAuthKey(scope, "message-a")
|
||||
claudeThreadAuthCache.Lock()
|
||||
delete(claudeThreadAuthCache.entries, key)
|
||||
claudeThreadAuthCache.Unlock()
|
||||
if errShutdown := kvstore.ShutdownLocal(); errShutdown != nil {
|
||||
t.Fatal(errShutdown)
|
||||
}
|
||||
if errConfigure := kvstore.ConfigureLocal(true, path); errConfigure != nil {
|
||||
t.Fatal(errConfigure)
|
||||
}
|
||||
authID, found, errLookup := ClaudeThreadAuth(context.Background(), scope, "message-a")
|
||||
if errLookup != nil || !found || authID != "credential-a" {
|
||||
t.Fatalf("restored owner=%q, found=%v, error=%v", authID, found, errLookup)
|
||||
}
|
||||
if _, foreign, errForeign := ClaudeThreadAuth(context.Background(), scope+"-other", "message-a"); errForeign != nil || foreign {
|
||||
t.Fatalf("owner crossed caller scopes: found=%v, error=%v", foreign, errForeign)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClaudeThreadAuthMemoryIsBounded(t *testing.T) {
|
||||
if errShutdown := kvstore.ShutdownLocal(); errShutdown != nil {
|
||||
t.Fatal(errShutdown)
|
||||
}
|
||||
scope := t.Name()
|
||||
for i := 0; i <= claudeThreadAuthLimit; i++ {
|
||||
RememberClaudeThreadAuth(scope, "message-"+strconv.Itoa(i), "credential-a")
|
||||
}
|
||||
if _, found, errLookup := ClaudeThreadAuth(context.Background(), scope, "message-0"); errLookup != nil || found {
|
||||
t.Fatalf("oldest owner was not evicted: found=%v, error=%v", found, errLookup)
|
||||
}
|
||||
if authID, found, errLookup := ClaudeThreadAuth(context.Background(), scope, "message-"+strconv.Itoa(claudeThreadAuthLimit)); errLookup != nil || !found || authID != "credential-a" {
|
||||
t.Fatalf("newest owner was lost: auth=%q, found=%v, error=%v", authID, found, errLookup)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClaudeThreadAuthMemoryExpiration(t *testing.T) {
|
||||
if errShutdown := kvstore.ShutdownLocal(); errShutdown != nil {
|
||||
t.Fatal(errShutdown)
|
||||
}
|
||||
scope := t.Name()
|
||||
RememberClaudeThreadAuth(scope, "message-a", "credential-a")
|
||||
key := claudeThreadAuthKey(scope, "message-a")
|
||||
claudeThreadAuthCache.Lock()
|
||||
entry := claudeThreadAuthCache.entries[key]
|
||||
entry.expiresAt = time.Unix(0, 0)
|
||||
claudeThreadAuthCache.entries[key] = entry
|
||||
claudeThreadAuthCache.Unlock()
|
||||
if _, found, errLookup := ClaudeThreadAuth(context.Background(), scope, "message-a"); errLookup != nil || found {
|
||||
t.Fatalf("expired owner remained routable: found=%v, error=%v", found, errLookup)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user