diff --git a/internal/cache/antigravity_reasoning_replay_cache.go b/internal/cache/antigravity_reasoning_replay_cache.go index a53017f400..d1aaad73e9 100644 --- a/internal/cache/antigravity_reasoning_replay_cache.go +++ b/internal/cache/antigravity_reasoning_replay_cache.go @@ -4,13 +4,13 @@ import ( "bytes" "context" "crypto/rand" - "encoding/json" "fmt" "sort" "strings" "sync" "time" + "github.com/MoeForge/moego/base64" "github.com/MoeForge/moego/ojson" homekv "github.com/router-for-me/CLIProxyAPI/v8/internal/home" "github.com/router-for-me/CLIProxyAPI/v8/internal/util" @@ -165,9 +165,28 @@ func GetAntigravityReasoningReplayItemsRequired(ctx context.Context, modelName, return items, found, errGet } +// PeekAntigravityReasoningReplayItemsRequired retrieves existing replay items +// without creating an absence reservation tombstone on a cache miss. Use this +// for read-only paths such as CountTokens that never commit or delete a turn. +func PeekAntigravityReasoningReplayItemsRequired(ctx context.Context, modelName, sessionKey string) ([][]byte, bool, error) { + items, _, found, errGet := getAntigravityReasoningReplayItemsWithSnapshot(ctx, modelName, sessionKey, false) + return items, found, errGet +} + +// PeekAntigravityReasoningReplayItemsWithSnapshotRequired retrieves existing +// replay items and their snapshot without creating an absence reservation +// tombstone on a cache miss. +func PeekAntigravityReasoningReplayItemsWithSnapshotRequired(ctx context.Context, modelName, sessionKey string) ([][]byte, AntigravityReasoningReplaySnapshot, bool, error) { + return getAntigravityReasoningReplayItemsWithSnapshot(ctx, modelName, sessionKey, false) +} + // GetAntigravityReasoningReplayItemsWithSnapshotRequired retrieves replay items // and the exact cache state that guarded this request. func GetAntigravityReasoningReplayItemsWithSnapshotRequired(ctx context.Context, modelName, sessionKey string) ([][]byte, AntigravityReasoningReplaySnapshot, bool, error) { + return getAntigravityReasoningReplayItemsWithSnapshot(ctx, modelName, sessionKey, true) +} + +func getAntigravityReasoningReplayItemsWithSnapshot(ctx context.Context, modelName, sessionKey string, reserveOnMiss bool) ([][]byte, AntigravityReasoningReplaySnapshot, bool, error) { key := antigravityReasoningReplayCacheKey(modelName, sessionKey) if key == "" { return nil, AntigravityReasoningReplaySnapshot{}, false, nil @@ -190,6 +209,9 @@ func GetAntigravityReasoningReplayItemsWithSnapshotRequired(ctx context.Context, found = true break } + if !reserveOnMiss { + return nil, AntigravityReasoningReplaySnapshot{loaded: true}, false, nil + } reservation := newAntigravityReasoningReplayTombstone() swapped, errReserve := client.KVCompareAndSwap(ctx, kvKey, nil, false, reservation, AntigravityReasoningReplayCacheTTL) if errReserve != nil { @@ -233,11 +255,17 @@ func GetAntigravityReasoningReplayItemsWithSnapshotRequired(ctx context.Context, defer antigravityReasoningReplayMu.Unlock() entry, ok := antigravityReasoningReplayEntries[key] if !ok { + if !reserveOnMiss { + return nil, AntigravityReasoningReplaySnapshot{loaded: true, evictionEpoch: antigravityReasoningReplayEvictionEpoch}, false, nil + } return nil, reserveAntigravityReasoningReplayAbsentLocked(key, now), false, nil } if now.Sub(entry.Timestamp) > AntigravityReasoningReplayCacheTTL { antigravityReasoningReplayEvictionEpoch++ delete(antigravityReasoningReplayEntries, key) + if !reserveOnMiss { + return nil, AntigravityReasoningReplaySnapshot{loaded: true, evictionEpoch: antigravityReasoningReplayEvictionEpoch}, false, nil + } return nil, reserveAntigravityReasoningReplayAbsentLocked(key, now), false, nil } entry.Timestamp = now @@ -425,6 +453,48 @@ func newAntigravityReasoningReplayGeneration() string { return fmt.Sprintf("%x", nonce[:]) } +func marshalReasoningReplayRawJSONArray(items [][]byte) ([]byte, error) { + doc := ojson.NewDocString("[]") + for i, item := range items { + trimmed := bytes.TrimSpace(item) + if len(trimmed) == 0 || !ojson.ValidBytes(trimmed) { + return nil, fmt.Errorf("invalid replay item json at index %d", i) + } + if errSet := doc.SetRawBytes("-1", trimmed); errSet != nil { + return nil, errSet + } + } + return doc.Bytes(), nil +} + +func unmarshalReasoningReplayRawJSONArray(raw []byte) ([][]byte, error) { + trimmed := bytes.TrimSpace(raw) + if len(trimmed) == 0 || !ojson.ValidBytes(trimmed) { + return nil, fmt.Errorf("invalid reasoning replay KV payload (please upgrade CLIProxyAPI to a matching version)") + } + root := util.ParseGJSONBytesNoCopy(trimmed) + if !root.IsArray() { + return nil, fmt.Errorf("unsupported reasoning replay KV format (please upgrade CLIProxyAPI to a matching version)") + } + elems := root.Array() + items := make([][]byte, 0, len(elems)) + for _, elem := range elems { + switch { + case elem.IsObject(): + items = append(items, []byte(elem.Raw)) + case elem.Type == ojson.String: + decoded, errDecode := base64.StdEncoding.DecodeString(elem.String()) + if errDecode != nil { + return nil, fmt.Errorf("invalid legacy base64 reasoning replay item (please upgrade CLIProxyAPI): %w", errDecode) + } + items = append(items, decoded) + default: + return nil, fmt.Errorf("unsupported reasoning replay KV element type (please upgrade CLIProxyAPI to a matching version)") + } + } + return items, nil +} + func marshalAntigravityReasoningReplayHomeValue(items [][]byte, branch string) ([]byte, error) { if branch == "" { branch = newAntigravityReasoningReplayGeneration() @@ -436,11 +506,14 @@ func marshalAntigravityReasoningReplayHomeValue(items [][]byte, branch string) ( stored := make([][]byte, 0, len(items)+1) stored = append(stored, doc.Bytes()) stored = append(stored, items...) - return json.Marshal(stored) + return marshalReasoningReplayRawJSONArray(stored) } func decodeAntigravityReasoningReplayHomeValue(raw []byte) (items [][]byte, deleted bool, generation, branch string, ok bool) { - if errUnmarshal := json.Unmarshal(raw, &items); errUnmarshal != nil { + var errUnmarshal error + items, errUnmarshal = unmarshalReasoningReplayRawJSONArray(raw) + if errUnmarshal != nil { + log.Warnf("antigravity reasoning replay KV decode failed: %v", errUnmarshal) return nil, false, "", "", false } if len(items) == 0 || strings.TrimSpace(ojson.GetBytesNoCopy(items[0], "type").String()) != antigravityReasoningReplayGenerationItemType { @@ -470,7 +543,7 @@ func newAntigravityReasoningReplayTombstone() []byte { _ = doc.Set("type", antigravityReasoningReplayGenerationItemType) _ = doc.Set("generation", newAntigravityReasoningReplayGeneration()) _ = doc.Set("branch", newAntigravityReasoningReplayGeneration()) - raw, _ := json.Marshal([][]byte{doc.Bytes()}) + raw, _ := marshalReasoningReplayRawJSONArray([][]byte{doc.Bytes()}) return raw } diff --git a/internal/cache/antigravity_reasoning_replay_cache_test.go b/internal/cache/antigravity_reasoning_replay_cache_test.go index c0215c7f91..9cef703e98 100644 --- a/internal/cache/antigravity_reasoning_replay_cache_test.go +++ b/internal/cache/antigravity_reasoning_replay_cache_test.go @@ -3,12 +3,12 @@ package cache import ( "bytes" "context" - "encoding/json" "fmt" "sync" "testing" "time" + "github.com/MoeForge/moego/base64" "github.com/MoeForge/moego/ojson" homekv "github.com/router-for-me/CLIProxyAPI/v8/internal/home" ) @@ -494,21 +494,29 @@ func TestAntigravityReasoningReplayLocalAbsenceReservationsStayWithinEntryBound( } } -func TestAntigravityReasoningReplayHomeWritesRemainLegacyArrayReadable(t *testing.T) { +func TestAntigravityReasoningReplayHomeWritesUnwrappedJSONArrayAndReadsLegacyBase64(t *testing.T) { client := newFakeAntigravityReasoningReplayKVClient() useFakeAntigravityReasoningReplayKVClient(t, client, true) - const model, session = "gemini-3.6-flash-high", "home-legacy-readable" + const model, session = "gemini-3.6-flash-high", "home-unwrapped-json-array" item := antigravityReplayTestItem("legacy-readable-signature-123456") if !CacheAntigravityReasoningReplayItems(model, session, [][]byte{item}) { t.Fatal("Home write failed") } raw := client.values[antigravityReasoningReplayKVKey(model, session)] - var legacyItems [][]byte - if errUnmarshal := json.Unmarshal(raw, &legacyItems); errUnmarshal != nil { - t.Fatalf("new Home value is not readable as legacy [][]byte: %v", errUnmarshal) + root := ojson.ParseBytes(raw) + if !root.IsArray() || len(root.Array()) != 2 || !root.Get("0").IsObject() || !root.Get("1").IsObject() { + t.Fatalf("new Home value must be an unwrapped JSON object array without base64 double encoding: %s", raw) } - if len(legacyItems) != 2 || ojson.GetBytes(legacyItems[0], "type").String() != antigravityReasoningReplayGenerationItemType || !bytes.Contains(legacyItems[1], []byte("legacy-readable-signature")) { - t.Fatalf("legacy-readable Home array malformed: %q", legacyItems) + if root.Get("0.type").String() != antigravityReasoningReplayGenerationItemType || root.Get("1.thoughtSignature").String() != "legacy-readable-signature-123456" { + t.Fatalf("unwrapped Home array malformed: %s", raw) + } + + // Verify backward-compatible decoding of legacy base64-encoded [][]byte arrays. + legacyRaw := []byte(`["` + base64.StdEncoding.EncodeToString(item) + `"]`) + client.values[antigravityReasoningReplayKVKey(model, session)] = legacyRaw + gotLegacy, _, foundLegacy, errLegacy := GetAntigravityReasoningReplayItemsWithSnapshotRequired(context.Background(), model, session) + if errLegacy != nil || !foundLegacy || len(gotLegacy) != 1 || ojson.GetBytes(gotLegacy[0], "thoughtSignature").String() != "legacy-readable-signature-123456" { + t.Fatalf("legacy base64 Home array read = %q, found=%v err=%v", gotLegacy, foundLegacy, errLegacy) } } @@ -518,7 +526,7 @@ func TestAntigravityReasoningReplayHomeReadNormalizesAndRejectsMixedInvalidChain const model, session = "gemini-3.6-flash-high", "home-validation" key := antigravityReasoningReplayKVKey(model, session) valid := []byte(`{"type":"function_call_part","name":"run","args":{"b":2,"a":1},"targetOccurrence":1,"thoughtSignature":"valid-home-signature-123456"}`) - raw, errMarshal := json.Marshal([][]byte{valid}) + raw, errMarshal := marshalReasoningReplayRawJSONArray([][]byte{valid}) if errMarshal != nil { t.Fatal(errMarshal) } @@ -534,7 +542,7 @@ func TestAntigravityReasoningReplayHomeReadNormalizesAndRejectsMixedInvalidChain t.Fatalf("valid Home read expire count = %d, want 1", client.expireCount) } - invalidRaw, errInvalidMarshal := json.Marshal([][]byte{valid, []byte(`{"type":"unknown"}`)}) + invalidRaw, errInvalidMarshal := marshalReasoningReplayRawJSONArray([][]byte{valid, []byte(`{"type":"unknown"}`)}) if errInvalidMarshal != nil { t.Fatal(errInvalidMarshal) } diff --git a/internal/cache/codex_reasoning_replay_cache.go b/internal/cache/codex_reasoning_replay_cache.go index f989878981..8ff8d640f9 100644 --- a/internal/cache/codex_reasoning_replay_cache.go +++ b/internal/cache/codex_reasoning_replay_cache.go @@ -2,7 +2,6 @@ package cache import ( "context" - "encoding/json" "sort" "strings" "sync" @@ -88,7 +87,7 @@ func CacheCodexReasoningReplayItemsBestEffort(ctx context.Context, modelName, se log.Errorf("home kv best-effort codex reasoning replay set failed prefix=cpa:codex:*: %v", errClient) return false } - raw, errMarshal := json.Marshal(normalized) + raw, errMarshal := marshalReasoningReplayRawJSONArray(normalized) if errMarshal != nil { log.Errorf("home kv best-effort codex reasoning replay set failed prefix=cpa:codex:*: %v", errMarshal) return false @@ -146,13 +145,15 @@ func AppendCodexReasoningReplayItemsBestEffort(ctx context.Context, modelName, s } var existing [][]byte if found { - if errUnmarshal := json.Unmarshal(existingRaw, &existing); errUnmarshal != nil { - log.Errorf("home kv best-effort codex reasoning replay append failed prefix=cpa:codex:*: %v", errUnmarshal) + var errUnmarshal error + existing, errUnmarshal = unmarshalReasoningReplayRawJSONArray(existingRaw) + if errUnmarshal != nil { + log.Errorf("home kv best-effort codex reasoning replay append failed prefix=cpa:codex:* (please upgrade CLIProxyAPI): %v", errUnmarshal) return false } } combined := appendCodexReasoningReplayTurn(existing, normalized) - raw, errMarshal := json.Marshal(combined) + raw, errMarshal := marshalReasoningReplayRawJSONArray(combined) if errMarshal != nil { log.Errorf("home kv best-effort codex reasoning replay append failed prefix=cpa:codex:*: %v", errMarshal) return false @@ -267,8 +268,9 @@ func GetCodexReasoningReplayItemsRequired(ctx context.Context, modelName, sessio if errGet != nil || !found { return nil, false, errGet } - var homeItems [][]byte - if errUnmarshal := json.Unmarshal(raw, &homeItems); errUnmarshal != nil { + homeItems, errUnmarshal := unmarshalReasoningReplayRawJSONArray(raw) + if errUnmarshal != nil { + log.Warnf("codex reasoning replay KV decode failed (please upgrade CLIProxyAPI): %v", errUnmarshal) return nil, false, errUnmarshal } if _, errExpire := client.KVExpire(ctx, codexReasoningReplayKVKey(modelName, sessionKey), CodexReasoningReplayCacheTTL); errExpire != nil { diff --git a/internal/cache/codex_reasoning_replay_cache_test.go b/internal/cache/codex_reasoning_replay_cache_test.go index 441a2d928d..3a01a89a07 100644 --- a/internal/cache/codex_reasoning_replay_cache_test.go +++ b/internal/cache/codex_reasoning_replay_cache_test.go @@ -3,14 +3,13 @@ package cache import ( "bytes" "context" - "encoding/json" "errors" "fmt" - "github.com/MoeForge/moego/base64" "sync" "testing" "time" + "github.com/MoeForge/moego/base64" "github.com/MoeForge/moego/ojson" homekv "github.com/router-for-me/CLIProxyAPI/v8/internal/home" ) @@ -130,7 +129,7 @@ func validCodexReasoningReplayItemForTest(seed byte) []byte { func mustCodexReasoningReplayJSON(t *testing.T, items [][]byte) []byte { t.Helper() - raw, errMarshal := json.Marshal(items) + raw, errMarshal := marshalReasoningReplayRawJSONArray(items) if errMarshal != nil { t.Fatalf("marshal replay items: %v", errMarshal) } diff --git a/internal/cache/xai_reasoning_replay_cache.go b/internal/cache/xai_reasoning_replay_cache.go index b786aeba71..b33614cc2b 100644 --- a/internal/cache/xai_reasoning_replay_cache.go +++ b/internal/cache/xai_reasoning_replay_cache.go @@ -2,7 +2,6 @@ package cache import ( "context" - "encoding/json" "sort" "strings" "sync" @@ -101,7 +100,7 @@ func StoreXAIReasoningReplayItems(ctx context.Context, modelName, sessionKey str log.Errorf("home kv best-effort xai reasoning replay set failed prefix=cpa:xai:*: %v", errClient) return XAIReasoningReplayStoreBackendError } - raw, errMarshal := json.Marshal(normalized) + raw, errMarshal := marshalReasoningReplayRawJSONArray(normalized) if errMarshal != nil { log.Errorf("home kv best-effort xai reasoning replay set failed prefix=cpa:xai:*: %v", errMarshal) return XAIReasoningReplayStoreBackendError @@ -164,8 +163,9 @@ func GetXAIReasoningReplayItemsRequired(ctx context.Context, modelName, sessionK if errGet != nil || !found { return nil, false, errGet } - var homeItems [][]byte - if errUnmarshal := json.Unmarshal(raw, &homeItems); errUnmarshal != nil { + homeItems, errUnmarshal := unmarshalReasoningReplayRawJSONArray(raw) + if errUnmarshal != nil { + log.Warnf("xai reasoning replay KV decode failed (please upgrade CLIProxyAPI): %v", errUnmarshal) return nil, false, errUnmarshal } if _, errExpire := client.KVExpire(ctx, xaiReasoningReplayKVKey(modelName, sessionKey), XAIReasoningReplayCacheTTL); errExpire != nil { diff --git a/internal/cache/xai_reasoning_replay_cache_test.go b/internal/cache/xai_reasoning_replay_cache_test.go index e3faf53fd4..cdeefe1388 100644 --- a/internal/cache/xai_reasoning_replay_cache_test.go +++ b/internal/cache/xai_reasoning_replay_cache_test.go @@ -3,12 +3,11 @@ package cache import ( "context" "crypto/sha256" - "encoding/json" "errors" - "github.com/MoeForge/moego/base64" "testing" "time" + "github.com/MoeForge/moego/base64" "github.com/MoeForge/moego/ojson" homekv "github.com/router-for-me/CLIProxyAPI/v8/internal/home" ) @@ -90,7 +89,7 @@ func useFakeXAIReasoningReplayKVClient(t *testing.T, client *fakeXAIReasoningRep func mustXAIReasoningReplayJSON(t *testing.T, items [][]byte) []byte { t.Helper() - raw, err := json.Marshal(items) + raw, err := marshalReasoningReplayRawJSONArray(items) if err != nil { t.Fatalf("marshal replay items: %v", err) } diff --git a/internal/runtime/executor/aistudio_executor.go b/internal/runtime/executor/aistudio_executor.go index a827a4e911..7acfd20b6a 100644 --- a/internal/runtime/executor/aistudio_executor.go +++ b/internal/runtime/executor/aistudio_executor.go @@ -186,8 +186,14 @@ func (e *AIStudioExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, helps.AppendAPIResponseChunk(ctx, e.cfg, wsResp.Body) } if wsResp.Status < 200 || wsResp.Status >= 300 { + if errClear := clearAntigravityReasoningReplayOnInvalidSignature(ctx, body.replayScope, wsResp.Status, wsResp.Body); errClear != nil { + logAntigravityReasoningReplayDegraded(body.replayScope, "invalidate", errClear) + } return resp, statusErr{code: wsResp.Status, msg: string(wsResp.Body)} } + if body.action != "countTokens" { + cacheAntigravityReasoningReplayFromResponse(ctx, body.replayScope, body.replayRequestPayload, wsResp.Body) + } reporter.ObserveResponseModel(wsResp.Body) responseFormat := cliproxyexecutor.ResponseFormatOrSource(opts) var param any @@ -284,20 +290,23 @@ func (e *AIStudioExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth reporter.StartResponseTTFT() metadataLogged = true } - var body bytes.Buffer + var errBody bytes.Buffer if len(firstEvent.Payload) > 0 { reporter.MarkFirstResponseByte() helps.AppendAPIResponseChunk(ctx, e.cfg, firstEvent.Payload) - body.Write(firstEvent.Payload) + errBody.Write(firstEvent.Payload) } if firstEvent.Type == wsrelay.MessageTypeStreamEnd { - return nil, statusErr{code: firstEvent.Status, msg: body.String()} + if errClear := clearAntigravityReasoningReplayOnInvalidSignature(ctx, body.replayScope, firstEvent.Status, errBody.Bytes()); errClear != nil { + logAntigravityReasoningReplayDegraded(body.replayScope, "invalidate", errClear) + } + return nil, statusErr{code: firstEvent.Status, msg: errBody.String()} } for event := range wsStream { if event.Err != nil { helps.RecordAPIResponseError(ctx, e.cfg, event.Err) - if body.Len() == 0 { - body.WriteString(event.Err.Error()) + if errBody.Len() == 0 { + errBody.WriteString(event.Err.Error()) } break } @@ -309,14 +318,18 @@ func (e *AIStudioExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth if len(event.Payload) > 0 { reporter.MarkFirstResponseByte() helps.AppendAPIResponseChunk(ctx, e.cfg, event.Payload) - body.Write(event.Payload) + errBody.Write(event.Payload) } if event.Type == wsrelay.MessageTypeStreamEnd { break } } - return nil, statusErr{code: firstEvent.Status, msg: body.String()} + if errClear := clearAntigravityReasoningReplayOnInvalidSignature(ctx, body.replayScope, firstEvent.Status, errBody.Bytes()); errClear != nil { + logAntigravityReasoningReplayDegraded(body.replayScope, "invalidate", errClear) + } + return nil, statusErr{code: firstEvent.Status, msg: errBody.String()} } + replayAccumulator := newAntigravityReasoningReplayAccumulator(body.replayScope, body.replayRequestPayload) out := make(chan cliproxyexecutor.StreamChunk, 1) go func(first wsrelay.StreamEvent) { defer close(out) @@ -336,12 +349,22 @@ func (e *AIStudioExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth _ = helps.StopTranslatedClaudeStream(ctx, &streamErr, reporter, &streamUsage, out) }() var param any + var replayCommitted bool + commitReplay := func() { + if !replayCommitted && replayAccumulator != nil && replayAccumulator.terminal && streamErr == nil && helps.ApplyPatchTranslationError(param) == nil { + replayAccumulator.Commit(ctx) + replayCommitted = true + } + } helps.InitializeApplyPatchStream(ctx, body.toFormat, responseFormat, req.Model, helps.ApplyPatchOriginalRequest(req, opts), translatedReq, ¶m) metadataLogged := false var relayFrames helps.RelaySSEBuffer processRelayFrames := func(payloads [][]byte) bool { for _, payload := range payloads { for _, frame := range splitRelayFrames(payload) { + if replayAccumulator != nil { + replayAccumulator.ObserveSSELine(frame) + } reporter.ObserveResponseModel(frame) streamUsage.ObserveGeminiStream(frame) filtered := helps.FilterSSEUsageMetadata(frame) @@ -350,6 +373,9 @@ func (e *AIStudioExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth helps.CaptureTranslatedClaudeStreamError(responseFormat, lines, &streamErr, func(msg string) error { return statusErr{code: http.StatusBadGateway, msg: msg} }) + if replayAccumulator != nil && replayAccumulator.terminal && !replayCommitted && antigravityStreamChunksHaveTerminalCommitEvent(lines) { + commitReplay() + } for i := range lines { select { case out <- cliproxyexecutor.StreamChunk{Payload: ensureColonSpacedJSON(lines[i])}: @@ -382,6 +408,7 @@ func (e *AIStudioExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth helps.CaptureTranslatedClaudeStreamError(responseFormat, lines, &streamErr, func(msg string) error { return statusErr{code: http.StatusBadGateway, msg: msg} }) + commitReplay() for _, line := range lines { select { case out <- cliproxyexecutor.StreamChunk{Payload: ensureColonSpacedJSON(line)}: @@ -435,6 +462,9 @@ func (e *AIStudioExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth helps.AppendAPIResponseChunk(ctx, e.cfg, event.Payload) } if event.Status < http.StatusOK || event.Status >= http.StatusMultipleChoices { + if errClear := clearAntigravityReasoningReplayOnInvalidSignature(ctx, body.replayScope, event.Status, event.Payload); errClear != nil { + logAntigravityReasoningReplayDegraded(body.replayScope, "invalidate", errClear) + } errResponse := statusErr{code: event.Status, msg: string(event.Payload)} helps.RecordAPIResponseError(ctx, e.cfg, errResponse) reporter.PublishFailure(ctx, errResponse) @@ -444,6 +474,12 @@ func (e *AIStudioExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth } return false } + if replayAccumulator != nil { + replayAccumulator.observeResponsePayload(event.Payload) + if !replayAccumulator.terminal && (ojson.GetBytesNoCopy(event.Payload, "response.candidates.0").Exists() || ojson.GetBytesNoCopy(event.Payload, "candidates.0").Exists()) { + replayAccumulator.terminal = true + } + } reporter.ObserveResponseModel(event.Payload) streamUsage.Observe(helps.ParseGeminiUsage(event.Payload), true) lines := helps.TranslateStreamWithClaudeInputTokens(ctx, body.toFormat, responseFormat, req.Model, helps.ApplyPatchOriginalRequest(req, opts), translatedReq, event.Payload, ¶m, claudeInputTokens) @@ -451,6 +487,7 @@ func (e *AIStudioExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth helps.CaptureTranslatedClaudeStreamError(responseFormat, lines, &streamErr, func(msg string) error { return statusErr{code: http.StatusBadGateway, msg: msg} }) + commitReplay() for i := range lines { select { case out <- cliproxyexecutor.StreamChunk{Payload: ensureColonSpacedJSON(lines[i])}: @@ -561,9 +598,11 @@ func (e *AIStudioExecutor) Refresh(ctx context.Context, auth *cliproxyauth.Auth) } type translatedPayload struct { - payload []byte - action string - toFormat sdktranslator.Format + payload []byte + action string + toFormat sdktranslator.Format + replayScope antigravityReasoningReplayScope + replayRequestPayload []byte } func (e *AIStudioExecutor) translateRequest(ctx context.Context, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, stream bool) ([]byte, translatedPayload, error) { @@ -608,6 +647,19 @@ func (e *AIStudioExecutor) translateRequest(ctx context.Context, req cliproxyexe } normalizeAIStudioThinkingLevelDoc(doc) internalsignature.SanitizeNonVertexGeminiRequestThoughtSignaturesDoc(doc, "contents") + replayScope := antigravityReasoningReplayScope{} + var replayRequestPayload []byte + if antigravityUsesReasoningReplayCache(baseModel) { + var changedReplay bool + var errReplay error + replayScope, replayRequestPayload, changedReplay, errReplay = prepareGeminiTopLevelReasoningReplayPayloadDoc(ctx, baseModel, req, opts, doc) + if errReplay != nil { + return nil, translatedPayload{}, errReplay + } + if changedReplay { + internalsignature.SanitizeNonVertexGeminiRequestThoughtSignaturesDoc(doc, "contents") + } + } if action == "countTokens" { _ = doc.Delete("generationConfig") _ = doc.Delete("tools") @@ -615,7 +667,13 @@ func (e *AIStudioExecutor) translateRequest(ctx context.Context, req cliproxyexe } helps.ApplyPayloadConfigWithRequestDoc(e.cfg, baseModel, to.String(), from.String(), "", doc, originalTranslated, requestedModel, requestPath, opts.Headers) payload = doc.Bytes() - return payload, translatedPayload{payload: payload, action: action, toFormat: to}, nil + return payload, translatedPayload{ + payload: payload, + action: action, + toFormat: to, + replayScope: replayScope, + replayRequestPayload: replayRequestPayload, + }, nil } // normalizeAIStudioThinkingLevel normalizes thinking levels to Google's canonical uppercase enum values. diff --git a/internal/runtime/executor/antigravity_executor_request.go b/internal/runtime/executor/antigravity_executor_request.go index 2a7df38a20..0516d09fc2 100644 --- a/internal/runtime/executor/antigravity_executor_request.go +++ b/internal/runtime/executor/antigravity_executor_request.go @@ -782,14 +782,22 @@ func generateSessionID() string { } func generateStableSessionID(payload []byte) string { - return generateStableSessionIDFromContents(util.GetGJSONBytesNoCopy(payload, "request.contents")) + contents := util.GetGJSONBytesNoCopy(payload, "request.contents") + if !contents.IsArray() { + contents = util.GetGJSONBytesNoCopy(payload, "contents") + } + return generateStableSessionIDFromContents(contents) } func generateStableSessionIDDoc(doc *ojson.Doc) string { if doc == nil { return generateSessionID() } - return generateStableSessionIDFromContents(doc.Get("request.contents")) + contents := doc.Get("request.contents") + if !contents.IsArray() { + contents = doc.Get("contents") + } + return generateStableSessionIDFromContents(contents) } func generateStableSessionIDFromContents(contents ojson.Result) string { diff --git a/internal/runtime/executor/antigravity_executor_stream.go b/internal/runtime/executor/antigravity_executor_stream.go index 9be7ccf93c..31961e4385 100644 --- a/internal/runtime/executor/antigravity_executor_stream.go +++ b/internal/runtime/executor/antigravity_executor_stream.go @@ -358,14 +358,8 @@ func (e *AntigravityExecutor) ExecuteStream(ctx context.Context, auth *cliproxya } } } - if replayAccumulator != nil && replayAccumulator.terminal && !replayCommitted && responseFormat == sdktranslator.FormatOpenAIResponse { - for _, chunk := range chunks { - for _, line := range bytes.Split(chunk, []byte("\n")) { - if ojson.GetBytesNoCopy(helps.JSONPayload(line), "type").String() == "response.completed" { - commitReplay() - } - } - } + if replayAccumulator != nil && replayAccumulator.terminal && !replayCommitted && antigravityStreamChunksHaveTerminalCommitEvent(chunks) { + commitReplay() } for i := range chunks { select { diff --git a/internal/runtime/executor/antigravity_executor_tokens.go b/internal/runtime/executor/antigravity_executor_tokens.go index a076b45537..bde7323e42 100644 --- a/internal/runtime/executor/antigravity_executor_tokens.go +++ b/internal/runtime/executor/antigravity_executor_tokens.go @@ -66,7 +66,7 @@ func (e *AntigravityExecutor) CountTokens(ctx context.Context, auth *cliproxyaut e.obfuscateSensitiveWordsDoc(doc) helps.PrepareAntigravityClaudeStructuredOutputDoc(baseModel, doc) sanitizeAntigravityGeminiRequestSignaturesDoc(baseModel, doc) - if _, _, errReplay := prepareAntigravityGeminiReasoningReplayPayloadDoc(ctx, baseModel, req, opts, doc); errReplay != nil { + if _, errReplay := prepareAntigravityGeminiCountTokensReasoningReplayPayloadDoc(ctx, baseModel, req, opts, doc); errReplay != nil { return cliproxyexecutor.Response{}, errReplay } ensureAntigravityGeminiLeadingUserContentDoc(baseModel, doc) diff --git a/internal/runtime/executor/antigravity_reasoning_replay.go b/internal/runtime/executor/antigravity_reasoning_replay.go index 24f5c55da8..de83bb2441 100644 --- a/internal/runtime/executor/antigravity_reasoning_replay.go +++ b/internal/runtime/executor/antigravity_reasoning_replay.go @@ -19,9 +19,11 @@ import ( homekv "github.com/router-for-me/CLIProxyAPI/v8/internal/home" "github.com/router-for-me/CLIProxyAPI/v8/internal/runtime/executor/helps" internalsignature "github.com/router-for-me/CLIProxyAPI/v8/internal/signature" + translatorcommon "github.com/router-for-me/CLIProxyAPI/v8/internal/translator/common" "github.com/router-for-me/CLIProxyAPI/v8/internal/util" cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/executor" "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" ) @@ -39,14 +41,22 @@ func antigravityReplayLogKey(value string) string { // antigravityCountClaudeToolProvenanceIDs reports how many reserved // Claude-facing provenance IDs are still present in a Gemini-shaped payload. func antigravityCountClaudeToolProvenanceIDs(payload []byte) int { - return antigravityCountClaudeToolProvenanceIDsResult(util.GetGJSONBytesNoCopy(payload, "request.contents")) + contents := util.GetGJSONBytesNoCopy(payload, "request.contents") + if !contents.IsArray() { + contents = util.GetGJSONBytesNoCopy(payload, "contents") + } + return antigravityCountClaudeToolProvenanceIDsResult(contents) } func antigravityCountClaudeToolProvenanceIDsDoc(doc *ojson.Doc) int { if doc == nil { return 0 } - return antigravityCountClaudeToolProvenanceIDsResult(doc.Get("request.contents")) + contents := doc.Get("request.contents") + if !contents.IsArray() { + contents = doc.Get("contents") + } + return antigravityCountClaudeToolProvenanceIDsResult(contents) } func antigravityCountClaudeToolProvenanceIDsResult(contents ojson.Result) int { @@ -88,6 +98,27 @@ func (s antigravityReasoningReplayScope) valid() bool { return strings.TrimSpace(s.modelName) != "" && strings.TrimSpace(s.sessionKey) != "" } +func antigravityFormatCarriesNativeThoughtSignatures(format sdktranslator.Format) bool { + switch strings.ToLower(strings.TrimSpace(format.String())) { + case sdktranslator.FormatGemini.String(), + sdktranslator.FormatInteractions.String(): + return true + default: + return false + } +} + +func antigravityRequestIsSymmetricForReasoningReplay(opts cliproxyexecutor.Options) bool { + source := strings.TrimSpace(opts.SourceFormat.String()) + if source == "" { + return false + } + if !antigravityFormatCarriesNativeThoughtSignatures(opts.SourceFormat) { + return false + } + return antigravityFormatCarriesNativeThoughtSignatures(cliproxyexecutor.ResponseFormatOrSource(opts)) +} + func antigravityReasoningReplayScopeFromPayload(modelName string, payload []byte) antigravityReasoningReplayScope { sessionID := antigravityReplaySessionIDFromPayload(payload) if sessionID == "" { @@ -127,6 +158,9 @@ func antigravityReasoningReplayScopeFromDoc(modelName string, doc *ojson.Doc) an } func antigravityReasoningReplayScopeFromRequest(ctx context.Context, modelName string, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, payload []byte) antigravityReasoningReplayScope { + if antigravityRequestIsSymmetricForReasoningReplay(opts) && !antigravityPayloadHasClaudeToolProvenanceID(payload) { + return antigravityReasoningReplayScope{} + } // Prefer an explicit downstream session over a provider sessionId synthesized // from request text. This keeps identical prompts in separate client sessions // from sharing an opaque Gemini reasoning chain. @@ -157,6 +191,9 @@ func antigravityReasoningReplayScopeFromRequest(ctx context.Context, modelName s } func antigravityReasoningReplayScopeFromRequestDoc(ctx context.Context, modelName string, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, doc *ojson.Doc) antigravityReasoningReplayScope { + if antigravityRequestIsSymmetricForReasoningReplay(opts) && !antigravityDocHasClaudeToolProvenanceID(doc) { + return antigravityReasoningReplayScope{} + } scope := antigravityReasoningReplayScope{modelName: modelName} if sessionKey := antigravityReasoningReplayClientSessionKey(ctx, req, opts); sessionKey != "" { scope.sessionKey = sessionKey @@ -351,12 +388,117 @@ func prepareAntigravityGeminiReasoningReplayPayload(ctx context.Context, modelNa return doc.Bytes(), scope, nil } +// antigravityStreamChunksHaveTerminalCommitEvent reports whether translated SSE +// chunks carry a client-visible terminal completion/truncation event before +// which accumulated reasoning replay items must be committed to KV. +func antigravityStreamChunksHaveTerminalCommitEvent(chunks [][]byte) bool { + for _, chunk := range chunks { + for _, line := range bytes.Split(chunk, []byte("\n")) { + payload := helps.JSONPayload(line) + if len(payload) == 0 { + continue + } + switch ojson.GetBytesNoCopy(payload, "type").String() { + case "response.completed", "response.incomplete", "response.done", "message_stop": + return true + } + if ojson.GetBytesNoCopy(payload, "event_type").String() == "interaction.completed" { + return true + } + for _, path := range []string{ + "choices.0.finish_reason", + "response.choices.0.finish_reason", + "candidates.0.finishReason", + "response.candidates.0.finishReason", + } { + if finishReason := ojson.GetBytesNoCopy(payload, path); finishReason.Exists() && strings.TrimSpace(finishReason.String()) != "" { + return true + } + } + } + } + return false +} + +func isGeminiCountTokensAction(req cliproxyexecutor.Request, opts cliproxyexecutor.Options) bool { + if req.Metadata != nil { + if action, _ := req.Metadata["action"].(string); strings.EqualFold(strings.TrimSpace(action), "countTokens") { + return true + } + } + if opts.Metadata != nil { + if action, _ := opts.Metadata["action"].(string); strings.EqualFold(strings.TrimSpace(action), "countTokens") { + return true + } + } + return false +} + +func prepareGeminiTopLevelReasoningReplayPayloadDoc(ctx context.Context, modelName string, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, doc *ojson.Doc) (antigravityReasoningReplayScope, []byte, bool, error) { + return prepareGeminiTopLevelReasoningReplayPayloadDocWithMode(ctx, modelName, req, opts, doc, !isGeminiCountTokensAction(req, opts)) +} + +func prepareGeminiTopLevelCountTokensReasoningReplayPayloadDoc(ctx context.Context, modelName string, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, doc *ojson.Doc) (bool, error) { + _, _, changed, err := prepareGeminiTopLevelReasoningReplayPayloadDocWithMode(ctx, modelName, req, opts, doc, false) + return changed, err +} + +func prepareGeminiTopLevelReasoningReplayPayloadDocWithMode(ctx context.Context, modelName string, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, doc *ojson.Doc, reserveOnMiss bool) (antigravityReasoningReplayScope, []byte, bool, error) { + if doc == nil || !antigravityUsesReasoningReplayCache(modelName) { + return antigravityReasoningReplayScope{}, nil, false, nil + } + if doc.Get("request.contents").Exists() { + scope, changed, err := prepareAntigravityGeminiReasoningReplayPayloadDocWithOptions(ctx, modelName, req, opts, doc, true, reserveOnMiss) + var replayRequestPayload []byte + if reserveOnMiss && scope.valid() { + replayRequestPayload, _ = antigravityReplayRequestEnvelopeFromDoc(doc) + } + return scope, replayRequestPayload, changed, err + } + if !doc.Get("contents").Exists() { + return antigravityReasoningReplayScope{}, nil, false, nil + } + wrappedBytes, ok := antigravityReplayRequestEnvelopeFromDoc(doc) + if !ok { + return antigravityReasoningReplayScope{}, nil, false, nil + } + wrappedDoc := ojson.NewDoc(wrappedBytes) + for _, key := range []string{"sessionId", "session_id"} { + if val := doc.Get(key); val.Exists() { + _ = wrappedDoc.SetRawBytes(key, []byte(val.Raw)) + } + } + scope, changed, err := prepareAntigravityGeminiReasoningReplayPayloadDocWithOptions(ctx, modelName, req, opts, wrappedDoc, false, reserveOnMiss) + if err != nil { + return scope, nil, false, err + } + if changed { + if updatedContents := wrappedDoc.Get("request.contents"); updatedContents.Exists() { + _ = doc.SetRawBytes("contents", []byte(updatedContents.Raw)) + } + } + var replayRequestPayload []byte + if reserveOnMiss && scope.valid() { + replayRequestPayload, _ = antigravityReplayRequestEnvelopeFromDoc(doc) + } + return scope, replayRequestPayload, changed, nil +} + func prepareAntigravityGeminiReasoningReplayPayloadDoc(ctx context.Context, modelName string, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, doc *ojson.Doc) (antigravityReasoningReplayScope, bool, error) { + return prepareAntigravityGeminiReasoningReplayPayloadDocWithOptions(ctx, modelName, req, opts, doc, true, !isGeminiCountTokensAction(req, opts)) +} + +func prepareAntigravityGeminiCountTokensReasoningReplayPayloadDoc(ctx context.Context, modelName string, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, doc *ojson.Doc) (bool, error) { + _, changed, err := prepareAntigravityGeminiReasoningReplayPayloadDocWithOptions(ctx, modelName, req, opts, doc, true, false) + return changed, err +} + +func prepareAntigravityGeminiReasoningReplayPayloadDocWithOptions(ctx context.Context, modelName string, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, doc *ojson.Doc, normalizeFunctionResponseRoles, reserveOnMiss bool) (antigravityReasoningReplayScope, bool, error) { if doc == nil || !antigravityUsesReasoningReplayCache(modelName) { return antigravityReasoningReplayScope{}, false, nil } originalContentsRaw := doc.Get("request.contents").Raw - scope, replayApplied, errReplay := applyAntigravityReasoningReplayCacheDoc(ctx, modelName, req, opts, doc) + scope, replayApplied, errReplay := applyAntigravityReasoningReplayCacheDocWithMode(ctx, modelName, req, opts, doc, reserveOnMiss) changed := replayApplied if errReplay != nil { logAntigravityReasoningReplayDegraded(scope, "read", errReplay) @@ -366,7 +508,7 @@ func prepareAntigravityGeminiReasoningReplayPayloadDoc(ctx context.Context, mode changed = false replayApplied = false } - if normalizeAntigravityGeminiFunctionResponseRolesDoc(doc) { + if normalizeFunctionResponseRoles && normalizeAntigravityGeminiFunctionResponseRolesDoc(doc) { changed = true } if antigravityDocHasClaudeToolProvenanceID(doc) { @@ -381,7 +523,7 @@ func prepareAntigravityGeminiReasoningReplayPayloadDoc(ctx context.Context, mode changed = true } } - if normalizeAntigravityGeminiFunctionResponseRolesDoc(doc) { + if normalizeFunctionResponseRoles && normalizeAntigravityGeminiFunctionResponseRolesDoc(doc) { changed = true } } @@ -400,7 +542,7 @@ func prepareAntigravityGeminiReasoningReplayPayloadDoc(ctx context.Context, mode origWrapped = []byte(`{"request":{"contents":` + originalContentsRaw + `}}`) } originalPairingValid := internalsignature.ValidateGeminiFunctionCallPairing(origWrapped) == nil - if replayApplied && originalPairingValid && scope.valid() { + if replayApplied && originalPairingValid && scope.valid() && reserveOnMiss { if _, errDelete := internalcache.DeleteAntigravityReasoningReplayItemsIfUnchanged(ctx, scope.modelName, scope.sessionKey, scope.cacheSnapshot); errDelete != nil { logAntigravityReasoningReplayDegraded(scope, "invalidate", errDelete) } @@ -443,6 +585,10 @@ func applyAntigravityReasoningReplayCache(ctx context.Context, modelName string, } func applyAntigravityReasoningReplayCacheDoc(ctx context.Context, modelName string, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, doc *ojson.Doc) (antigravityReasoningReplayScope, bool, error) { + return applyAntigravityReasoningReplayCacheDocWithMode(ctx, modelName, req, opts, doc, true) +} + +func applyAntigravityReasoningReplayCacheDocWithMode(ctx context.Context, modelName string, req cliproxyexecutor.Request, opts cliproxyexecutor.Options, doc *ojson.Doc, reserveOnMiss bool) (antigravityReasoningReplayScope, bool, error) { if doc == nil { return antigravityReasoningReplayScope{}, false, nil } @@ -450,7 +596,17 @@ func applyAntigravityReasoningReplayCacheDoc(ctx context.Context, modelName stri if !scope.valid() { return scope, false, nil } - items, snapshot, ok, err := internalcache.GetAntigravityReasoningReplayItemsWithSnapshotRequired(ctx, scope.modelName, scope.sessionKey) + var ( + items [][]byte + snapshot internalcache.AntigravityReasoningReplaySnapshot + ok bool + err error + ) + if reserveOnMiss { + items, snapshot, ok, err = internalcache.GetAntigravityReasoningReplayItemsWithSnapshotRequired(ctx, scope.modelName, scope.sessionKey) + } else { + items, snapshot, ok, err = internalcache.PeekAntigravityReasoningReplayItemsWithSnapshotRequired(ctx, scope.modelName, scope.sessionKey) + } scope.cacheSnapshot = snapshot reservedBefore := antigravityCountClaudeToolProvenanceIDsDoc(doc) if err != nil || !ok || len(items) == 0 { @@ -490,9 +646,14 @@ func antigravityReplayRequestEnvelopeFromDoc(doc *ojson.Doc) ([]byte, bool) { if requestNode := doc.Get("request"); requestNode.IsObject() { return []byte(`{"request":` + requestNode.Raw + `}`), true } + prefix := "request." contents := doc.Get("request.contents") if !contents.Exists() { - return nil, false + prefix = "" + contents = doc.Get("contents") + if !contents.Exists() { + return nil, false + } } var buf bytes.Buffer buf.WriteString(`{"request":{`) @@ -501,10 +662,10 @@ func antigravityReplayRequestEnvelopeFromDoc(doc *ojson.Doc) ([]byte, bool) { key string path string }{ - {key: "systemInstruction", path: "request.systemInstruction"}, - {key: "tools", path: "request.tools"}, - {key: "toolConfig", path: "request.toolConfig"}, - {key: "contents", path: "request.contents"}, + {key: "systemInstruction", path: prefix + "systemInstruction"}, + {key: "tools", path: prefix + "tools"}, + {key: "toolConfig", path: prefix + "toolConfig"}, + {key: "contents", path: prefix + "contents"}, } { val := doc.Get(field.path) if !val.Exists() { @@ -1972,14 +2133,22 @@ func antigravityFunctionCallMatchesReplayItem(functionCall, itemResult ojson.Res } func antigravityPayloadHasClaudeToolProvenanceID(payload []byte) bool { - return antigravityContentsHasClaudeToolProvenanceID(util.GetGJSONBytesNoCopy(payload, "request.contents")) + contents := util.GetGJSONBytesNoCopy(payload, "request.contents") + if !contents.IsArray() { + contents = util.GetGJSONBytesNoCopy(payload, "contents") + } + return antigravityContentsHasClaudeToolProvenanceID(contents) } func antigravityDocHasClaudeToolProvenanceID(doc *ojson.Doc) bool { if doc == nil { return false } - return antigravityContentsHasClaudeToolProvenanceID(doc.Get("request.contents")) + contents := doc.Get("request.contents") + if !contents.IsArray() { + contents = doc.Get("contents") + } + return antigravityContentsHasClaudeToolProvenanceID(contents) } func antigravityContentsHasClaudeToolProvenanceID(contents ojson.Result) bool { @@ -2528,6 +2697,12 @@ type antigravityPendingThoughtSignature struct { targetKind string } +type antigravityStreamingReplayToolCall struct { + call *translatorcommon.VertexStreamingToolCall + partIndex int + signature string +} + type antigravityReasoningReplayAccumulator struct { scope antigravityReasoningReplayScope responseContextHash string @@ -2544,6 +2719,8 @@ type antigravityReasoningReplayAccumulator struct { thoughtPartIndex int lastResponseKind string pendingSignatures []antigravityPendingThoughtSignature + activeStreamingFC *antigravityStreamingReplayToolCall + hasIncompleteFC bool itemBytes int overflow bool terminal bool @@ -2674,9 +2851,20 @@ func (a *antigravityReasoningReplayAccumulator) appendItem(item []byte) { a.itemBytes += len(item) } -func (a *antigravityReasoningReplayAccumulator) attachDetachedSignatureToLastFunctionCall(signature string) { +func (a *antigravityReasoningReplayAccumulator) attachDetachedSignatureToLastFunctionCall(signature string) bool { if a == nil || signature == "" { - return + return false + } + if a.activeStreamingFC != nil { + if a.activeStreamingFC.signature != "" { + return false + } + if a.itemBytes+len(signature) > internalcache.AntigravityReasoningReplayCacheMaxBytesPerEntry { + a.overflow = true + return false + } + a.activeStreamingFC.signature = signature + return true } for itemIndex := len(a.items) - 1; itemIndex >= 0; itemIndex-- { item := util.ParseGJSONBytesNoCopy(a.items[itemIndex]) @@ -2684,21 +2872,22 @@ func (a *antigravityReasoningReplayAccumulator) attachDetachedSignatureToLastFun continue } if strings.TrimSpace(item.Get("thoughtSignature").String()) != "" { - return + return false } updated, errSet := ojson.SetBytes(a.items[itemIndex], "thoughtSignature", signature) if errSet != nil { - return + return false } delta := len(updated) - len(a.items[itemIndex]) if a.itemBytes+delta > internalcache.AntigravityReasoningReplayCacheMaxBytesPerEntry { a.overflow = true - return + return false } a.items[itemIndex] = updated a.itemBytes += delta - return + return true } + return false } func (a *antigravityReasoningReplayAccumulator) ObserveSSELine(line []byte) { @@ -2759,56 +2948,17 @@ func (a *antigravityReasoningReplayAccumulator) observeResponsePayload(payload [ signature = "" } if fc := part.Get("functionCall"); fc.Exists() { - if a.lastResponseKind == "text" || a.lastResponseKind == "thought" { - a.flushPendingThoughtSignaturesForKind(a.lastResponseKind) - } - if signature != "" { - remainingPending := a.pendingSignatures[:0] - for _, pending := range a.pendingSignatures { - if pending.targetKind != "" { - remainingPending = append(remainingPending, pending) - } - } - a.pendingSignatures = remainingPending - } - if signature == "" { - for pendingIndex := len(a.pendingSignatures) - 1; pendingIndex >= 0; pendingIndex-- { - if a.pendingSignatures[pendingIndex].targetKind == "" { - signature = a.pendingSignatures[pendingIndex].signature - a.pendingSignatures = append(a.pendingSignatures[:pendingIndex], a.pendingSignatures[pendingIndex+1:]...) - break - } - } - } - if signature == "" { - signature = a.claimPrecedingTurnThoughtSignature() - } - keys := antigravityReplayToolCallKeysFromPart(fc) - for _, key := range keys { - dedupeKey := key + "\x00" + signature - if signature == "" { - dedupeKey = fmt.Sprintf("%s\x00part:%d", key, pi) - } - if a.seenFC[dedupeKey] { - return true - } - a.seenFC[dedupeKey] = true - } - occurrenceKey := antigravityFunctionCallKey(fc.Get("name").String(), fc.Get("args").Raw, "") - occurrence := a.functionCallOccurrences[occurrenceKey] - if occurrenceKey != "" { - a.functionCallOccurrences[occurrenceKey] = occurrence + 1 - } - item := buildAntigravityFunctionCallPartItem(a.contentIndex, pi, occurrence, fc, signature) - if len(item) > 0 { - a.appendItem(antigravitySetReplayItemContextHashValue(item, a.responseContextHash)) - if signature != "" { - a.seenSignatures[signature] = true - } - } - a.lastResponseKind = "function_call" + a.observeFunctionCallPart(pi, fc, signature) return true } + if a.activeStreamingFC != nil { + text := part.Get("text") + hasSemanticText := text.Exists() && text.String() != "" + if hasSemanticText { + a.hasIncompleteFC = true + a.activeStreamingFC = nil + } + } targetKind := "" if part.Get("thought").Bool() { @@ -2818,8 +2968,7 @@ func (a *antigravityReasoningReplayAccumulator) observeResponsePayload(payload [ hasSemanticText := text.Exists() && text.String() != "" signatureOnly := signature != "" && !hasSemanticText if signatureOnly && a.lastResponseKind == "function_call" { - if !a.seenSignatures[signature] { - a.attachDetachedSignatureToLastFunctionCall(signature) + if !a.seenSignatures[signature] && a.attachDetachedSignatureToLastFunctionCall(signature) { a.seenSignatures[signature] = true } return true @@ -3007,15 +3156,163 @@ func (a *antigravityReasoningReplayAccumulator) appendPendingThoughtSignatures() a.pendingSignatures = nil } +func (a *antigravityReasoningReplayAccumulator) resolveFunctionCallSignature(signature string) string { + if a.lastResponseKind == "text" || a.lastResponseKind == "thought" { + a.flushPendingThoughtSignaturesForKind(a.lastResponseKind) + } + if signature != "" { + remainingPending := a.pendingSignatures[:0] + for _, pending := range a.pendingSignatures { + if pending.targetKind != "" { + remainingPending = append(remainingPending, pending) + } + } + a.pendingSignatures = remainingPending + } + if signature == "" { + for pendingIndex := len(a.pendingSignatures) - 1; pendingIndex >= 0; pendingIndex-- { + if a.pendingSignatures[pendingIndex].targetKind == "" { + signature = a.pendingSignatures[pendingIndex].signature + a.pendingSignatures = append(a.pendingSignatures[:pendingIndex], a.pendingSignatures[pendingIndex+1:]...) + break + } + } + } + if signature == "" { + signature = a.claimPrecedingTurnThoughtSignature() + } + return signature +} + +func (a *antigravityReasoningReplayAccumulator) recordCompletedFunctionCall(pi int, fc ojson.Result, signature string) { + keys := antigravityReplayToolCallKeysFromPart(fc) + for _, key := range keys { + dedupeKey := key + "\x00" + signature + if signature == "" { + dedupeKey = fmt.Sprintf("%s\x00part:%d", key, pi) + } + if a.seenFC[dedupeKey] { + return + } + a.seenFC[dedupeKey] = true + } + occurrenceKey := antigravityFunctionCallKey(fc.Get("name").String(), fc.Get("args").Raw, "") + occurrence := a.functionCallOccurrences[occurrenceKey] + if occurrenceKey != "" { + a.functionCallOccurrences[occurrenceKey] = occurrence + 1 + } + item := buildAntigravityFunctionCallPartItem(a.contentIndex, pi, occurrence, fc, signature) + if len(item) > 0 { + a.appendItem(antigravitySetReplayItemContextHashValue(item, a.responseContextHash)) + if signature != "" { + a.seenSignatures[signature] = true + } + } + a.lastResponseKind = "function_call" +} + +func (a *antigravityReasoningReplayAccumulator) flushCompletedStreamingFunctionCall(active *antigravityStreamingReplayToolCall) { + if a == nil || active == nil || active.call == nil || active.call.Assembler == nil { + return + } + if active.call.Assembler.Err() != nil { + a.hasIncompleteFC = true + return + } + argsRaw := strings.TrimSpace(active.call.Assembler.FullArguments()) + if argsRaw == "" { + argsRaw = "{}" + } + if !ojson.Valid(argsRaw) { + a.hasIncompleteFC = true + return + } + fcDoc := ojson.NewDocString(`{"name":""}`) + _ = fcDoc.Set("name", active.call.RawName) + if active.call.UpstreamID != "" { + _ = fcDoc.Set("id", active.call.UpstreamID) + } + _ = fcDoc.SetRaw("args", argsRaw) + a.recordCompletedFunctionCall(active.partIndex, ojson.ParseBytes(fcDoc.Bytes()), active.signature) +} + +func (a *antigravityReasoningReplayAccumulator) observeFunctionCallPart(pi int, fc ojson.Result, signature string) { + if a == nil { + return + } + if active := a.activeStreamingFC; active != nil && active.call != nil && active.call.CanContinueWith(fc) { + if signature != "" && active.signature == "" { + active.signature = signature + } + if upstreamID := strings.TrimSpace(fc.Get("id").String()); upstreamID != "" && active.call.UpstreamID == "" { + active.call.UpstreamID = strings.Clone(upstreamID) + } + _, done := active.call.Assembler.PushFunctionCall(fc) + if active.call.Assembler.Err() != nil { + a.hasIncompleteFC = true + a.activeStreamingFC = nil + return + } + if done { + a.activeStreamingFC = nil + a.flushCompletedStreamingFunctionCall(active) + } + a.lastResponseKind = "function_call" + return + } + if translatorcommon.IsVertexStreamingFunctionCall(fc) { + if a.activeStreamingFC != nil { + a.hasIncompleteFC = true + a.activeStreamingFC = nil + } + rawName := strings.Clone(strings.TrimSpace(fc.Get("name").String())) + if rawName == "" { + a.hasIncompleteFC = true + return + } + upstreamID := strings.Clone(strings.TrimSpace(fc.Get("id").String())) + resolvedSig := a.resolveFunctionCallSignature(signature) + call := translatorcommon.NewVertexStreamingToolCall(rawName, rawName, upstreamID, upstreamID, 0) + _, done := call.Assembler.PushFunctionCall(fc) + if call.Assembler.Err() != nil { + a.hasIncompleteFC = true + return + } + active := &antigravityStreamingReplayToolCall{ + call: call, + partIndex: pi, + signature: resolvedSig, + } + if done { + a.flushCompletedStreamingFunctionCall(active) + } else { + a.activeStreamingFC = active + } + a.lastResponseKind = "function_call" + return + } + if a.activeStreamingFC != nil { + a.hasIncompleteFC = true + a.activeStreamingFC = nil + } + rawName := strings.TrimSpace(fc.Get("name").String()) + if rawName == "" && !fc.Get("args").Exists() { + return + } + resolvedSig := a.resolveFunctionCallSignature(signature) + a.recordCompletedFunctionCall(pi, fc, resolvedSig) +} + func (a *antigravityReasoningReplayAccumulator) Commit(ctx context.Context) { if a == nil || !a.scope.valid() { return } log.Debugf("antigravity replay: accumulator commit terminal=%t overflow=%t items=%d (session=%s)", a.terminal, a.overflow, len(a.items), antigravityReplayLogKey(a.scope.sessionKey)) - if !a.terminal { - // No terminal finishReason means the stream never completed, so this turn - // contributes nothing to the ledger and its tool IDs become unresolvable. + if !a.terminal || a.activeStreamingFC != nil || a.hasIncompleteFC { + // No terminal finishReason (or an unclosed/corrupted streaming tool call) + // means the stream never completed cleanly, so this turn contributes + // nothing to the ledger and its tool IDs become unresolvable. return } if a.overflow { diff --git a/internal/runtime/executor/antigravity_reasoning_replay_test.go b/internal/runtime/executor/antigravity_reasoning_replay_test.go index fc3b754587..1603689707 100644 --- a/internal/runtime/executor/antigravity_reasoning_replay_test.go +++ b/internal/runtime/executor/antigravity_reasoning_replay_test.go @@ -12,6 +12,7 @@ import ( internalcache "github.com/router-for-me/CLIProxyAPI/v8/internal/cache" "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/runtime/executor/helps" internalsignature "github.com/router-for-me/CLIProxyAPI/v8/internal/signature" "github.com/router-for-me/CLIProxyAPI/v8/internal/util" cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/executor" @@ -1955,6 +1956,155 @@ func TestPrepareAntigravityGeminiReasoningReplayStillRejectsBrokenPairing(t *tes } } +func TestAntigravityReasoningReplaySkipsSymmetricGeminiAndInteractionsButKeepsAsymmetricAlias(t *testing.T) { + internalcache.ClearAntigravityReasoningReplayCache() + t.Cleanup(internalcache.ClearAntigravityReasoningReplayCache) + + const model = "gemini-3-flash" + payload := []byte(`{"sessionId":"sess-symmetry","request":{"contents":[{"role":"user","parts":[{"text":"Call get_weather for Beijing"}]}]}}`) + responseSSE := []byte(`data: {"response":{"candidates":[{"content":{"role":"model","parts":[{"thought":true,"text":""},{"thoughtSignature":"EmcKZQFpFH0THU+YNRY5uVwt/0bo2hFo1RmXsMhlX9FCg2n6GssBe5sMV37KB6OrgeKKEP9fxN261QX","functionCall":{"name":"get_weather","args":{"city":"Beijing"},"id":"call_1"}}]},"finishReason":"STOP"}]}}`) + + for _, format := range []sdktranslator.Format{sdktranslator.FormatGemini, sdktranslator.FormatInteractions} { + t.Run("symmetric_"+format.String(), func(t *testing.T) { + internalcache.ClearAntigravityReasoningReplayCache() + req := cliproxyexecutor.Request{Model: model, Payload: payload} + opts := cliproxyexecutor.Options{ + SourceFormat: format, + ResponseFormat: format, + } + _, scope, errPrepare := prepareAntigravityGeminiReasoningReplayPayload(context.Background(), model, req, opts, payload) + if errPrepare != nil { + t.Fatalf("prepare error: %v", errPrepare) + } + if scope.valid() { + t.Fatalf("symmetric format %q must not allocate reasoning replay KV scope, got %+v", format, scope) + } + if acc := newAntigravityReasoningReplayAccumulator(scope, payload); acc != nil { + t.Fatalf("symmetric format %q must not create replay accumulator", format) + } + }) + } + + t.Run("asymmetric_claude_haiku_alias_to_gemini_3_flash", func(t *testing.T) { + internalcache.ClearAntigravityReasoningReplayCache() + // Downstream client sends Claude /v1/messages with model="claude-haiku-4-5-20251001", + // which model-alias resolves to baseModel="gemini-3-flash" inside AntigravityExecutor. + req := cliproxyexecutor.Request{Model: model, Payload: payload} + opts := cliproxyexecutor.Options{ + SourceFormat: sdktranslator.FormatClaude, + ResponseFormat: sdktranslator.FormatClaude, + Metadata: map[string]any{ + cliproxyexecutor.RequestedModelMetadataKey: "claude-haiku-4-5-20251001", + }, + } + _, scope, errPrepare := prepareAntigravityGeminiReasoningReplayPayload(context.Background(), model, req, opts, payload) + if errPrepare != nil { + t.Fatalf("prepare error: %v", errPrepare) + } + if !scope.valid() { + t.Fatal("asymmetric Claude -> Gemini alias request must allocate reasoning replay KV scope") + } + acc := newAntigravityReasoningReplayAccumulator(scope, payload) + if acc == nil { + t.Fatal("expected non-nil accumulator for asymmetric Claude -> Gemini request") + } + acc.ObserveSSELine(responseSSE) + acc.Commit(context.Background()) + + items, ok := internalcache.GetAntigravityReasoningReplayItems(scope.modelName, scope.sessionKey) + if !ok || len(items) == 0 { + t.Fatalf("expected asymmetric replay items to be persisted in KV, ok=%v len=%d", ok, len(items)) + } + }) + + t.Run("top_level_gemini_doc_replays_asymmetric_and_skips_symmetric", func(t *testing.T) { + internalcache.ClearAntigravityReasoningReplayCache() + topLevelTurn1 := []byte(`{"contents":[{"role":"user","parts":[{"text":"Call get_weather for Beijing"}]}]}`) + topLevelTurn2 := []byte(`{"contents":[{"role":"user","parts":[{"text":"Call get_weather for Beijing"}]},{"role":"model","parts":[{"thoughtSignature":"skip_thought_signature_validator","functionCall":{"name":"get_weather","args":{"city":"Beijing"},"id":"call_1"}}]},{"role":"user","parts":[{"functionResponse":{"name":"get_weather","id":"call_1","response":{"result":"sunny"}}}]}]}`) + topLevelResponse := []byte(`{"candidates":[{"content":{"role":"model","parts":[{"thought":true,"text":""},{"thoughtSignature":"EmcKZQFpFH0THU+YNRY5uVwt/0bo2hFo1RmXsMhlX9FCg2n6GssBe5sMV37KB6OrgeKKEP9fxN261QX","functionCall":{"name":"get_weather","args":{"city":"Beijing"},"id":"call_1"}}]},"finishReason":"STOP"}]}`) + + symDoc := ojson.NewDoc(topLevelTurn1) + symScope, _, _, errSym := prepareGeminiTopLevelReasoningReplayPayloadDoc( + context.Background(), + model, + cliproxyexecutor.Request{Model: model, Payload: topLevelTurn1}, + cliproxyexecutor.Options{SourceFormat: sdktranslator.FormatGemini, ResponseFormat: sdktranslator.FormatGemini}, + symDoc, + ) + if errSym != nil { + t.Fatalf("symmetric top-level prepare error: %v", errSym) + } + if symScope.valid() { + t.Fatalf("symmetric top-level Gemini must not allocate replay scope, got %+v", symScope) + } + + headers := make(http.Header) + headers.Set("Session-Id", "sess-non-ag-gemini") + asymOpts := cliproxyexecutor.Options{ + SourceFormat: sdktranslator.FormatOpenAIResponse, + ResponseFormat: sdktranslator.FormatOpenAIResponse, + Headers: headers, + } + doc1 := ojson.NewDoc(topLevelTurn1) + scope1, replayReq1, _, err1 := prepareGeminiTopLevelReasoningReplayPayloadDoc( + context.Background(), + model, + cliproxyexecutor.Request{Model: model, Payload: topLevelTurn1}, + asymOpts, + doc1, + ) + if err1 != nil || !scope1.valid() { + t.Fatalf("asymmetric top-level prepare turn 1: scope=%+v err=%v", scope1, err1) + } + cacheAntigravityReasoningReplayFromResponse(context.Background(), scope1, replayReq1, topLevelResponse) + + doc2 := ojson.NewDoc(topLevelTurn2) + scope2, replayReq2, changed2, err2 := prepareGeminiTopLevelReasoningReplayPayloadDoc( + context.Background(), + model, + cliproxyexecutor.Request{Model: model, Payload: topLevelTurn2}, + asymOpts, + doc2, + ) + if err2 != nil || !changed2 { + t.Fatalf("asymmetric top-level prepare turn 2: changed=%v err=%v", changed2, err2) + } + if gotSig := doc2.Get("contents.1.parts.0.thoughtSignature").String(); gotSig != "EmcKZQFpFH0THU+YNRY5uVwt/0bo2hFo1RmXsMhlX9FCg2n6GssBe5sMV37KB6OrgeKKEP9fxN261QX" { + t.Fatalf("replayed signature = %q, want native thoughtSignature", gotSig) + } + if gotRole := doc2.Get("contents.2.role").String(); gotRole != "user" { + t.Fatalf("non-Antigravity functionResponse role = %q, want user", gotRole) + } + + // Turn 2 simulates Vertex stripping wire-only functionCall.id/functionResponse.id + // after prepareGeminiTopLevelReasoningReplayPayloadDoc; the captured replayRequestPayload + // from prepareGeminiTopLevelReasoningReplayPayloadDoc must still keep canonical call_id + // context so Turn 3 matches both call_1 and call_2 signatures. + helps.StripVertexOpenAIResponsesToolCallIDsDoc(doc2, asymOpts.SourceFormat.String()) + topLevelResponse2 := []byte(`{"candidates":[{"content":{"role":"model","parts":[{"thought":true,"text":""},{"thoughtSignature":"EsMTCsATARFNMg/XNVix5lDpkKaHR7Xg","functionCall":{"name":"get_weather","args":{"city":"Shanghai"},"id":"call_2"}}]},"finishReason":"STOP"}]}`) + cacheAntigravityReasoningReplayFromResponse(context.Background(), scope2, replayReq2, topLevelResponse2) + + topLevelTurn3 := []byte(`{"contents":[{"role":"user","parts":[{"text":"Call get_weather for Beijing"}]},{"role":"model","parts":[{"thoughtSignature":"skip_thought_signature_validator","functionCall":{"name":"get_weather","args":{"city":"Beijing"},"id":"call_1"}}]},{"role":"user","parts":[{"functionResponse":{"name":"get_weather","id":"call_1","response":{"result":"sunny"}}}]},{"role":"model","parts":[{"thoughtSignature":"skip_thought_signature_validator","functionCall":{"name":"get_weather","args":{"city":"Shanghai"},"id":"call_2"}}]},{"role":"user","parts":[{"functionResponse":{"name":"get_weather","id":"call_2","response":{"result":"cloudy"}}}]}]}`) + doc3 := ojson.NewDoc(topLevelTurn3) + _, _, changed3, err3 := prepareGeminiTopLevelReasoningReplayPayloadDoc( + context.Background(), + model, + cliproxyexecutor.Request{Model: model, Payload: topLevelTurn3}, + asymOpts, + doc3, + ) + if err3 != nil || !changed3 { + t.Fatalf("asymmetric top-level prepare turn 3: changed=%v err=%v", changed3, err3) + } + if gotSig1 := doc3.Get("contents.1.parts.0.thoughtSignature").String(); gotSig1 != "EmcKZQFpFH0THU+YNRY5uVwt/0bo2hFo1RmXsMhlX9FCg2n6GssBe5sMV37KB6OrgeKKEP9fxN261QX" { + t.Fatalf("turn 3 call_1 signature = %q, want preserved signature", gotSig1) + } + if gotSig2 := doc3.Get("contents.3.parts.0.thoughtSignature").String(); gotSig2 != "EsMTCsATARFNMg/XNVix5lDpkKaHR7Xg" { + t.Fatalf("turn 3 call_2 signature = %q, want second signature", gotSig2) + } + }) +} + func TestPrepareAntigravityGeminiReasoningReplayDegradesWhenReplayBreaksPairing(t *testing.T) { internalcache.ClearAntigravityReasoningReplayCache() t.Cleanup(internalcache.ClearAntigravityReasoningReplayCache) @@ -1982,3 +2132,174 @@ func TestPrepareAntigravityGeminiReasoningReplayDegradesWhenReplayBreaksPairing( t.Fatalf("out = %s, want original payload %s", out, payload) } } + +func TestAntigravityStreamChunksHaveTerminalCommitEvent(t *testing.T) { + cases := []struct { + name string + chunks [][]byte + want bool + }{ + { + name: "openai responses completed", + chunks: [][]byte{[]byte("event: response.completed\ndata: {\"type\":\"response.completed\"}\n\n")}, + want: true, + }, + { + name: "openai responses incomplete", + chunks: [][]byte{[]byte("event: response.incomplete\ndata: {\"type\":\"response.incomplete\",\"response\":{\"incomplete_details\":{\"reason\":\"max_output_tokens\"}}}\n\n")}, + want: true, + }, + { + name: "openai chat finish_reason stop", + chunks: [][]byte{[]byte("data: {\"choices\":[{\"index\":0,\"delta\":{},\"finish_reason\":\"stop\"}]}\n\n")}, + want: true, + }, + { + name: "openai chat null finish_reason", + chunks: [][]byte{[]byte("data: {\"choices\":[{\"index\":0,\"delta\":{\"content\":\"hi\"},\"finish_reason\":null}]}\n\n")}, + want: false, + }, + { + name: "claude message_stop", + chunks: [][]byte{[]byte("event: message_stop\ndata: {\"type\":\"message_stop\"}\n\n")}, + want: true, + }, + { + name: "bare done frame only", + chunks: [][]byte{[]byte("data: [DONE]\n\n")}, + want: false, + }, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + if got := antigravityStreamChunksHaveTerminalCommitEvent(tc.chunks); got != tc.want { + t.Fatalf("antigravityStreamChunksHaveTerminalCommitEvent() = %v, want %v", got, tc.want) + } + }) + } +} + +func TestAntigravityReasoningReplayAccumulatorAssemblesVertexStreamingPartialArgs(t *testing.T) { + internalcache.ClearAntigravityReasoningReplayCache() + t.Cleanup(internalcache.ClearAntigravityReasoningReplayCache) + + const model = "gemini-3.8-flash" + asymOpts := cliproxyexecutor.Options{ + SourceFormat: sdktranslator.FormatOpenAIResponse, + ResponseFormat: sdktranslator.FormatOpenAIResponse, + Headers: http.Header{"X-Session-Id": []string{"sess-vertex-partial-args"}}, + } + + turn1 := []byte(`{"contents":[{"role":"user","parts":[{"text":"Weather in New York?"}]}]}`) + doc1 := ojson.NewDoc(turn1) + scope1, replayReq1, _, err1 := prepareGeminiTopLevelReasoningReplayPayloadDoc( + context.Background(), + model, + cliproxyexecutor.Request{Model: model, Payload: turn1}, + asymOpts, + doc1, + ) + if err1 != nil || !scope1.valid() { + t.Fatalf("prepare turn 1: scope=%+v err=%v", scope1, err1) + } + + acc := newAntigravityReasoningReplayAccumulator(scope1, replayReq1) + acc.ObserveSSELine([]byte(`data: {"candidates":[{"index":0,"content":{"role":"model","parts":[{"functionCall":{"id":"call_ny","name":"get_weather","partialArgs":[{"jsonPath":"$.city","stringValue":"New ","willContinue":true}],"willContinue":true}}]}}]}`)) + acc.ObserveSSELine([]byte(`data: {"candidates":[{"index":0,"content":{"role":"model","parts":[{"text":"","thoughtSignature":"EmcKZQFpFH0THU+YNRY5uVwt/0bo2hFo1RmXsMhlX9FCg2n6GssBe5sMV37KB6OrgeKKEP9fxN261QX"}]}}]}`)) + acc.ObserveSSELine([]byte(`data: {"candidates":[{"index":0,"content":{"role":"model","parts":[{"functionCall":{"partialArgs":[{"jsonPath":"$.city","stringValue":"York"}],"willContinue":false}}]},"finishReason":"STOP"}]}`)) + acc.Commit(context.Background()) + + turn2 := []byte(`{"contents":[{"role":"user","parts":[{"text":"Weather in New York?"}]},{"role":"model","parts":[{"thoughtSignature":"skip_thought_signature_validator","functionCall":{"name":"get_weather","args":{"city":"New York"},"id":"call_ny"}}]},{"role":"user","parts":[{"functionResponse":{"name":"get_weather","id":"call_ny","response":{"result":"sunny"}}}]}]}`) + doc2 := ojson.NewDoc(turn2) + _, _, changed2, err2 := prepareGeminiTopLevelReasoningReplayPayloadDoc( + context.Background(), + model, + cliproxyexecutor.Request{Model: model, Payload: turn2}, + asymOpts, + doc2, + ) + if err2 != nil || !changed2 { + t.Fatalf("prepare turn 2 after partialArgs stream: changed=%v err=%v", changed2, err2) + } + if gotSig := doc2.Get("contents.1.parts.0.thoughtSignature").String(); gotSig != "EmcKZQFpFH0THU+YNRY5uVwt/0bo2hFo1RmXsMhlX9FCg2n6GssBe5sMV37KB6OrgeKKEP9fxN261QX" { + t.Fatalf("replayed signature = %q, want native signature from partialArgs stream", gotSig) + } +} + +func TestNonVertexSanitizesReplayedVertexBareTinkSignature(t *testing.T) { + // Verify that a Vertex bare-Tink thoughtSignature replayed from shared KV is + // downgraded to skip_thought_signature_validator when post-replay non-Vertex + // sanitization runs (as in GeminiExecutor and AIStudioExecutor). + const vertexBareTinkSig = "AY89a18f0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" + internalcache.ClearAntigravityReasoningReplayCache() + const model = "gemini-3.8-flash" + asymOpts := cliproxyexecutor.Options{ + SourceFormat: sdktranslator.FormatOpenAIResponse, + ResponseFormat: sdktranslator.FormatOpenAIResponse, + Headers: http.Header{"X-Session-Id": []string{"sess-vertex-to-gemini"}}, + } + turn1 := []byte(`{"contents":[{"role":"user","parts":[{"text":"Call get_weather"}]}]}`) + doc1 := ojson.NewDoc(turn1) + scope1, replayReq1, _, err1 := prepareGeminiTopLevelReasoningReplayPayloadDoc( + context.Background(), + model, + cliproxyexecutor.Request{Model: model, Payload: turn1}, + asymOpts, + doc1, + ) + if err1 != nil || !scope1.valid() { + t.Fatalf("prepare turn 1: scope=%+v err=%v", scope1, err1) + } + vertexResp := []byte(`{"candidates":[{"content":{"role":"model","parts":[{"thoughtSignature":"` + vertexBareTinkSig + `","functionCall":{"name":"get_weather","args":{"city":"Tokyo"},"id":"call_tink"}}]},"finishReason":"STOP"}]}`) + cacheAntigravityReasoningReplayFromResponse(context.Background(), scope1, replayReq1, vertexResp) + + turn2 := []byte(`{"contents":[{"role":"user","parts":[{"text":"Call get_weather"}]},{"role":"model","parts":[{"thoughtSignature":"skip_thought_signature_validator","functionCall":{"name":"get_weather","args":{"city":"Tokyo"},"id":"call_tink"}}]},{"role":"user","parts":[{"functionResponse":{"name":"get_weather","id":"call_tink","response":{"result":"clear"}}}]}]}`) + nonVertexDoc := ojson.NewDoc(turn2) + _, _, changed2, err2 := prepareGeminiTopLevelReasoningReplayPayloadDoc( + context.Background(), + model, + cliproxyexecutor.Request{Model: model, Payload: turn2}, + asymOpts, + nonVertexDoc, + ) + if err2 != nil || !changed2 { + t.Fatalf("prepare turn 2: changed=%v err=%v", changed2, err2) + } + internalsignature.SanitizeNonVertexGeminiRequestThoughtSignaturesDoc(nonVertexDoc, "contents") + if got := nonVertexDoc.Get("contents.1.parts.0.thoughtSignature").String(); got != "skip_thought_signature_validator" { + t.Fatalf("non-Vertex post-replay sanitized signature = %q, want skip_thought_signature_validator", got) + } +} + +func TestCountTokensReasoningReplayPeeksWithoutReservingTombstone(t *testing.T) { + internalcache.ClearAntigravityReasoningReplayCache() + t.Cleanup(internalcache.ClearAntigravityReasoningReplayCache) + + const model = "gemini-3.8-flash" + opts := cliproxyexecutor.Options{ + SourceFormat: sdktranslator.FormatOpenAIResponse, + ResponseFormat: sdktranslator.FormatOpenAIResponse, + Headers: http.Header{"X-Session-Id": []string{"sess-count-tokens-no-tombstone"}}, + } + payload := []byte(`{"contents":[{"role":"user","parts":[{"text":"Count tokens only"}]}]}`) + doc := ojson.NewDoc(payload) + req := cliproxyexecutor.Request{ + Model: model, + Payload: payload, + Metadata: map[string]any{"action": "countTokens"}, + } + scope, replayReq, changed, err := prepareGeminiTopLevelReasoningReplayPayloadDoc(context.Background(), model, req, opts, doc) + if err != nil || changed || len(replayReq) != 0 { + t.Fatalf("countTokens prepare = scope:%+v len(replayReq):%d changed:%v err:%v", scope, len(replayReq), changed, err) + } + // A subsequent real generation turn must still be able to cache best-effort + // without being blocked by an unclosed countTokens reservation. + item := []byte(`{"type":"thought_signature","contentIndex":0,"partIndex":0,"targetOccurrence":0,"thoughtSignature":"EmcKZQFpFH0THU+YNRY5uVwt/0bo2hFo1RmXsMhlX9FCg2n6GssBe5sMV37KB6OrgeKKEP9fxN261QX"}`) + if !internalcache.CacheAntigravityReasoningReplayItems(model, scope.sessionKey, [][]byte{item}) { + t.Fatal("failed to cache replay item after countTokens peek") + } + got, ok := internalcache.GetAntigravityReasoningReplayItems(model, scope.sessionKey) + if !ok || len(got) != 1 { + t.Fatalf("GetAntigravityReasoningReplayItems after countTokens peek = %v, ok=%v", got, ok) + } +} diff --git a/internal/runtime/executor/claude_thinking_replay_test.go b/internal/runtime/executor/claude_thinking_replay_test.go index 9968b8e22a..546a1a3a0c 100644 --- a/internal/runtime/executor/claude_thinking_replay_test.go +++ b/internal/runtime/executor/claude_thinking_replay_test.go @@ -33,17 +33,17 @@ func claudeReplayTestAuth(baseURL string) *cliproxyauth.Auth { func claudeReplayTestRequest(payload []byte, sessionID string, isCompat bool, source sdktranslator.Format) (cliproxyexecutor.Request, cliproxyexecutor.Options) { return cliproxyexecutor.Request{ - Model: "claude-synthetic-4772", - Payload: payload, - Metadata: map[string]any{ - claudeReplayResolvedModelInfoKey: ®istry.ModelInfo{IsCompat: isCompat}, - }, - }, cliproxyexecutor.Options{ - SourceFormat: source, - Metadata: map[string]any{ - cliproxyexecutor.ExecutionSessionMetadataKey: sessionID, - }, - } + Model: "claude-synthetic-4772", + Payload: payload, + Metadata: map[string]any{ + claudeReplayResolvedModelInfoKey: ®istry.ModelInfo{IsCompat: isCompat}, + }, + }, cliproxyexecutor.Options{ + SourceFormat: source, + Metadata: map[string]any{ + cliproxyexecutor.ExecutionSessionMetadataKey: sessionID, + }, + } } func TestClaudeThinkingReplayEnabledRequiresCompatClaudeAPIKey(t *testing.T) { diff --git a/internal/runtime/executor/codex_stream_bootstrap_buffering_test.go b/internal/runtime/executor/codex_stream_bootstrap_buffering_test.go index 965f5b0ad6..6ab82d5fcf 100644 --- a/internal/runtime/executor/codex_stream_bootstrap_buffering_test.go +++ b/internal/runtime/executor/codex_stream_bootstrap_buffering_test.go @@ -46,12 +46,12 @@ func codexTestAuth(baseURL string) *cliproxyauth.Auth { func codexTestRequest() (cliproxyexecutor.Request, cliproxyexecutor.Options) { return cliproxyexecutor.Request{ - Model: "gpt-5.6-terra", - Payload: []byte(`{"model":"gpt-5.6-terra","input":"hello"}`), - }, cliproxyexecutor.Options{ - SourceFormat: sdktranslator.FromString("openai-response"), - Stream: true, - } + Model: "gpt-5.6-terra", + Payload: []byte(`{"model":"gpt-5.6-terra","input":"hello"}`), + }, cliproxyexecutor.Options{ + SourceFormat: sdktranslator.FromString("openai-response"), + Stream: true, + } } // codexSSEServer streams the supplied event payloads as an HTTP 200 SSE response. @@ -92,11 +92,11 @@ func codexWebsocketServer(t *testing.T, frames ...string) *httptest.Server { func codexWebsocketRequest() (cliproxyexecutor.Request, cliproxyexecutor.Options) { return cliproxyexecutor.Request{ - Model: "gpt-5.6-terra", - Payload: []byte(`{"model":"gpt-5.6-terra","input":[{"type":"message","role":"user","content":"hello"}]}`), - }, cliproxyexecutor.Options{ - SourceFormat: sdktranslator.FromString("openai-response"), - } + Model: "gpt-5.6-terra", + Payload: []byte(`{"model":"gpt-5.6-terra","input":[{"type":"message","role":"user","content":"hello"}]}`), + }, cliproxyexecutor.Options{ + SourceFormat: sdktranslator.FromString("openai-response"), + } } // drainChunks collects every payload and the first error from a stream result. diff --git a/internal/runtime/executor/gemini_executor.go b/internal/runtime/executor/gemini_executor.go index f832fb54e7..9fdacdb8f0 100644 --- a/internal/runtime/executor/gemini_executor.go +++ b/internal/runtime/executor/gemini_executor.go @@ -176,6 +176,19 @@ func (e *GeminiExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, r if action != "countTokens" { helps.EnsureGeminiTrailingUserContentDoc(doc, "contents") } + replayScope := antigravityReasoningReplayScope{} + var replayRequestPayload []byte + if antigravityUsesReasoningReplayCache(baseModel) { + var changedReplay bool + var errReplay error + replayScope, replayRequestPayload, changedReplay, errReplay = prepareGeminiTopLevelReasoningReplayPayloadDoc(ctx, baseModel, req, opts, doc) + if errReplay != nil { + return resp, errReplay + } + if changedReplay { + internalsignature.SanitizeNonVertexGeminiRequestThoughtSignaturesDoc(doc, "contents") + } + } baseURL := resolveGeminiBaseURL(auth) url := fmt.Sprintf("%s/%s/models/%s:%s", baseURL, glAPIVersion, baseModel, action) if opts.Alt != "" && action != "countTokens" { @@ -231,6 +244,9 @@ func (e *GeminiExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, r b, _ := io.ReadAll(httpResp.Body) helps.AppendAPIResponseChunk(ctx, e.cfg, b) helps.LogWithRequestID(ctx).Debugf("request error, error status: %d, error message: %s", httpResp.StatusCode, helps.SummarizeErrorBody(httpResp.Header.Get("Content-Type"), b)) + if errClear := clearAntigravityReasoningReplayOnInvalidSignature(ctx, replayScope, httpResp.StatusCode, b); errClear != nil { + logAntigravityReasoningReplayDegraded(replayScope, "invalidate", errClear) + } err = statusErr{code: httpResp.StatusCode, msg: string(b)} return resp, err } @@ -240,6 +256,9 @@ func (e *GeminiExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, r return resp, err } helps.AppendAPIResponseChunk(ctx, e.cfg, data) + if action != "countTokens" { + cacheAntigravityReasoningReplayFromResponse(ctx, replayScope, replayRequestPayload, data) + } reporter.ObserveResponseModel(data) if blockMsg, blocked := geminiPromptBlockErrorMessage(data); blocked { err = statusErr{code: http.StatusBadGateway, msg: blockMsg} @@ -309,6 +328,19 @@ func (e *GeminiExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.A capGeminiMaxOutputTokensDoc(doc, baseModel) internalsignature.SanitizeNonVertexGeminiRequestThoughtSignaturesDoc(doc, "contents") helps.EnsureGeminiBoundaryUserContentDoc(doc, "contents") + replayScope := antigravityReasoningReplayScope{} + var replayRequestPayload []byte + if antigravityUsesReasoningReplayCache(baseModel) { + var changedReplay bool + var errReplay error + replayScope, replayRequestPayload, changedReplay, errReplay = prepareGeminiTopLevelReasoningReplayPayloadDoc(ctx, baseModel, req, opts, doc) + if errReplay != nil { + return nil, errReplay + } + if changedReplay { + internalsignature.SanitizeNonVertexGeminiRequestThoughtSignaturesDoc(doc, "contents") + } + } baseURL := resolveGeminiBaseURL(auth) url := fmt.Sprintf("%s/%s/models/%s:%s", baseURL, glAPIVersion, baseModel, "streamGenerateContent") @@ -365,9 +397,13 @@ func (e *GeminiExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.A if errClose := httpResp.Body.Close(); errClose != nil { log.Errorf("gemini executor: close response body error: %v", errClose) } + if errClear := clearAntigravityReasoningReplayOnInvalidSignature(ctx, replayScope, httpResp.StatusCode, b); errClear != nil { + logAntigravityReasoningReplayDegraded(replayScope, "invalidate", errClear) + } err = statusErr{code: httpResp.StatusCode, msg: string(b)} return nil, err } + replayAccumulator := newAntigravityReasoningReplayAccumulator(replayScope, replayRequestPayload) out := make(chan cliproxyexecutor.StreamChunk, 1) go func() { defer close(out) @@ -396,6 +432,13 @@ func (e *GeminiExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.A }() var param any var sawFinishReason bool + var replayCommitted bool + commitReplay := func() { + if !replayCommitted && replayAccumulator != nil && replayAccumulator.terminal && streamErr == nil && helps.ApplyPatchTranslationError(param) == nil { + replayAccumulator.Commit(ctx) + replayCommitted = true + } + } var pendingFilterPayload []byte var pendingFilterMsg string helps.InitializeApplyPatchStream(ctx, to, responseFormat, req.Model, helps.ApplyPatchOriginalRequest(req, opts), body, ¶m) @@ -405,6 +448,9 @@ func (e *GeminiExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.A helps.CaptureTranslatedClaudeStreamError(responseFormat, lines, &streamErr, func(msg string) error { return statusErr{code: http.StatusBadGateway, msg: msg} }) + if replayAccumulator != nil && replayAccumulator.terminal && !replayCommitted && antigravityStreamChunksHaveTerminalCommitEvent(lines) { + commitReplay() + } for i := range lines { select { case out <- cliproxyexecutor.StreamChunk{Payload: lines[i]}: @@ -420,6 +466,9 @@ func (e *GeminiExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.A for scanner.Scan() { line := scanner.Bytes() helps.AppendAPIResponseChunk(ctx, e.cfg, line) + if replayAccumulator != nil { + replayAccumulator.ObserveSSELine(line) + } reporter.ObserveResponseModel(line) rawPayload := helps.JSONPayload(line) if len(rawPayload) > 0 && len(pendingFilterPayload) > 0 { @@ -517,6 +566,7 @@ func (e *GeminiExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.A helps.CaptureTranslatedClaudeStreamError(responseFormat, lines, &streamErr, func(msg string) error { return statusErr{code: http.StatusBadGateway, msg: msg} }) + commitReplay() for i := range lines { select { case out <- cliproxyexecutor.StreamChunk{Payload: lines[i]}: @@ -879,6 +929,15 @@ func (e *GeminiExecutor) CountTokens(ctx context.Context, auth *cliproxyauth.Aut helps.SetStringIfDifferentDoc(doc, "model", baseModel) internalsignature.SanitizeNonVertexGeminiRequestThoughtSignaturesDoc(doc, "contents") helps.EnsureGeminiLeadingUserContentDoc(doc, "contents") + if antigravityUsesReasoningReplayCache(baseModel) { + changedReplay, errReplay := prepareGeminiTopLevelCountTokensReasoningReplayPayloadDoc(ctx, baseModel, req, opts, doc) + if errReplay != nil { + return cliproxyexecutor.Response{}, errReplay + } + if changedReplay { + internalsignature.SanitizeNonVertexGeminiRequestThoughtSignaturesDoc(doc, "contents") + } + } baseURL := resolveGeminiBaseURL(auth) url := fmt.Sprintf("%s/%s/models/%s:%s", baseURL, glAPIVersion, baseModel, "countTokens") diff --git a/internal/runtime/executor/gemini_vertex_executor.go b/internal/runtime/executor/gemini_vertex_executor.go index bcc774cfcf..43d096350e 100644 --- a/internal/runtime/executor/gemini_vertex_executor.go +++ b/internal/runtime/executor/gemini_vertex_executor.go @@ -456,7 +456,6 @@ func (e *GeminiVertexExecutor) executeWithServiceAccount(ctx context.Context, au fixGeminiImageAspectRatioDoc(baseModel, doc) helps.SetStringIfDifferentDoc(doc, "model", baseModel) - helps.StripVertexOpenAIResponsesToolCallIDsDoc(doc, from.String()) internalsignature.SanitizeGeminiRequestThoughtSignaturesDoc(doc, "contents") } @@ -471,6 +470,18 @@ func (e *GeminiVertexExecutor) executeWithServiceAccount(ctx context.Context, au helps.DropGeminiTrailingThoughtOnlyContentDoc(doc, "contents") helps.EnsureGeminiTrailingUserContentDoc(doc, "contents") } + replayScope := antigravityReasoningReplayScope{} + var replayRequestPayload []byte + if !isImagenModel(baseModel) && antigravityUsesReasoningReplayCache(baseModel) { + var errReplay error + replayScope, replayRequestPayload, _, errReplay = prepareGeminiTopLevelReasoningReplayPayloadDoc(ctx, baseModel, req, opts, doc) + if errReplay != nil { + return resp, errReplay + } + } + if !isImagenModel(baseModel) { + helps.StripVertexOpenAIResponsesToolCallIDsDoc(doc, opts.SourceFormat.String()) + } baseURL := vertexBaseURL(location) url := fmt.Sprintf("%s/%s/projects/%s/locations/%s/publishers/google/models/%s:%s", baseURL, vertexAPIVersion, projectID, location, baseModel, action) if opts.Alt != "" && action != "countTokens" { @@ -534,6 +545,9 @@ func (e *GeminiVertexExecutor) executeWithServiceAccount(ctx context.Context, au b, _ := io.ReadAll(httpResp.Body) helps.AppendAPIResponseChunk(ctx, e.cfg, b) helps.LogWithRequestID(ctx).Debugf("request error, error status: %d, error message: %s", httpResp.StatusCode, helps.SummarizeErrorBody(httpResp.Header.Get("Content-Type"), b)) + if errClear := clearAntigravityReasoningReplayOnInvalidSignature(ctx, replayScope, httpResp.StatusCode, b); errClear != nil { + logAntigravityReasoningReplayDegraded(replayScope, "invalidate", errClear) + } err = statusErr{code: httpResp.StatusCode, msg: string(b)} return resp, err } @@ -543,6 +557,9 @@ func (e *GeminiVertexExecutor) executeWithServiceAccount(ctx context.Context, au return resp, errRead } helps.AppendAPIResponseChunk(ctx, e.cfg, data) + if action != "countTokens" && !isImagenModel(baseModel) { + cacheAntigravityReasoningReplayFromResponse(ctx, replayScope, replayRequestPayload, data) + } reporter.ObserveResponseModel(data) // For Imagen models, convert response to Gemini format before translation @@ -612,7 +629,6 @@ func (e *GeminiVertexExecutor) executeWithAPIKey(ctx context.Context, auth *clip requestedModel := helps.PayloadRequestedModel(opts, req.Model) requestPath := helps.PayloadRequestPath(opts) helps.SetStringIfDifferentDoc(doc, "model", baseModel) - helps.StripVertexOpenAIResponsesToolCallIDsDoc(doc, from.String()) internalsignature.SanitizeGeminiRequestThoughtSignaturesDoc(doc, "contents") action := getVertexAction(baseModel, false) @@ -626,6 +642,16 @@ func (e *GeminiVertexExecutor) executeWithAPIKey(ctx context.Context, auth *clip helps.DropGeminiTrailingThoughtOnlyContentDoc(doc, "contents") helps.EnsureGeminiTrailingUserContentDoc(doc, "contents") } + replayScope := antigravityReasoningReplayScope{} + var replayRequestPayload []byte + if !isImagenModel(baseModel) && antigravityUsesReasoningReplayCache(baseModel) { + var errReplay error + replayScope, replayRequestPayload, _, errReplay = prepareGeminiTopLevelReasoningReplayPayloadDoc(ctx, baseModel, req, opts, doc) + if errReplay != nil { + return resp, errReplay + } + } + helps.StripVertexOpenAIResponsesToolCallIDsDoc(doc, from.String()) // For API key auth, use simpler URL format without project/location. // The helper honors base URLs that already encode "/publishers/..." so @@ -690,6 +716,9 @@ func (e *GeminiVertexExecutor) executeWithAPIKey(ctx context.Context, auth *clip b, _ := io.ReadAll(httpResp.Body) helps.AppendAPIResponseChunk(ctx, e.cfg, b) helps.LogWithRequestID(ctx).Debugf("request error, error status: %d, error message: %s", httpResp.StatusCode, helps.SummarizeErrorBody(httpResp.Header.Get("Content-Type"), b)) + if errClear := clearAntigravityReasoningReplayOnInvalidSignature(ctx, replayScope, httpResp.StatusCode, b); errClear != nil { + logAntigravityReasoningReplayDegraded(replayScope, "invalidate", errClear) + } err = statusErr{code: httpResp.StatusCode, msg: string(b)} return resp, err } @@ -699,6 +728,9 @@ func (e *GeminiVertexExecutor) executeWithAPIKey(ctx context.Context, auth *clip return resp, errRead } helps.AppendAPIResponseChunk(ctx, e.cfg, data) + if action != "countTokens" && !isImagenModel(baseModel) { + cacheAntigravityReasoningReplayFromResponse(ctx, replayScope, replayRequestPayload, data) + } reporter.ObserveResponseModel(data) if blockMsg, blocked := geminiPromptBlockErrorMessage(data); blocked { err = statusErr{code: http.StatusBadGateway, msg: blockMsg} @@ -757,12 +789,21 @@ func (e *GeminiVertexExecutor) executeStreamWithServiceAccount(ctx context.Conte requestedModel := helps.PayloadRequestedModel(opts, req.Model) requestPath := helps.PayloadRequestPath(opts) helps.SetStringIfDifferentDoc(doc, "model", baseModel) - helps.StripVertexOpenAIResponsesToolCallIDsDoc(doc, from.String()) internalsignature.SanitizeGeminiRequestThoughtSignaturesDoc(doc, "contents") action := getVertexAction(baseModel, true) helps.DropGeminiTrailingThoughtOnlyContentDoc(doc, "contents") helps.EnsureGeminiBoundaryUserContentDoc(doc, "contents") + replayScope := antigravityReasoningReplayScope{} + var replayRequestPayload []byte + if !isImagenModel(baseModel) && antigravityUsesReasoningReplayCache(baseModel) { + var errReplay error + replayScope, replayRequestPayload, _, errReplay = prepareGeminiTopLevelReasoningReplayPayloadDoc(ctx, baseModel, req, opts, doc) + if errReplay != nil { + return nil, errReplay + } + } + helps.StripVertexOpenAIResponsesToolCallIDsDoc(doc, from.String()) baseURL := vertexBaseURL(location) url := fmt.Sprintf("%s/%s/projects/%s/locations/%s/publishers/google/models/%s:%s", baseURL, vertexAPIVersion, projectID, location, baseModel, action) // Imagen models don't support streaming, skip SSE params @@ -829,9 +870,13 @@ func (e *GeminiVertexExecutor) executeStreamWithServiceAccount(ctx context.Conte if errClose := httpResp.Body.Close(); errClose != nil { log.Errorf("vertex executor: close response body error: %v", errClose) } + if errClear := clearAntigravityReasoningReplayOnInvalidSignature(ctx, replayScope, httpResp.StatusCode, b); errClear != nil { + logAntigravityReasoningReplayDegraded(replayScope, "invalidate", errClear) + } return nil, statusErr{code: httpResp.StatusCode, msg: string(b)} } + replayAccumulator := newAntigravityReasoningReplayAccumulator(replayScope, replayRequestPayload) out := make(chan cliproxyexecutor.StreamChunk, 1) go func() { defer close(out) @@ -853,9 +898,16 @@ func (e *GeminiVertexExecutor) executeStreamWithServiceAccount(ctx context.Conte }() var terminalDelivered bool var sawFinishReason bool + var replayCommitted bool var pendingFilterLine []byte var pendingFilterMsg string var param any + commitReplay := func() { + if !replayCommitted && replayAccumulator != nil && replayAccumulator.terminal && streamErr == nil && helps.ApplyPatchTranslationError(param) == nil { + replayAccumulator.Commit(ctx) + replayCommitted = true + } + } helps.InitializeApplyPatchStream(ctx, to, responseFormat, req.Model, helps.ApplyPatchOriginalRequest(req, opts), body, ¶m) emitTranslatedLine := func(line []byte) bool { lines := helps.TranslateStreamWithClaudeInputTokens(ctx, to, responseFormat, req.Model, helps.ApplyPatchOriginalRequest(req, opts), body, bytes.Clone(line), ¶m, claudeInputTokens) @@ -863,6 +915,9 @@ func (e *GeminiVertexExecutor) executeStreamWithServiceAccount(ctx context.Conte helps.CaptureTranslatedClaudeStreamError(responseFormat, lines, &streamErr, func(msg string) error { return statusErr{code: http.StatusBadGateway, msg: msg} }) + if replayAccumulator != nil && replayAccumulator.terminal && !replayCommitted && antigravityStreamChunksHaveTerminalCommitEvent(lines) { + commitReplay() + } for i := range lines { select { case out <- cliproxyexecutor.StreamChunk{Payload: lines[i]}: @@ -878,6 +933,9 @@ func (e *GeminiVertexExecutor) executeStreamWithServiceAccount(ctx context.Conte for scanner.Scan() { line := scanner.Bytes() helps.AppendAPIResponseChunk(ctx, e.cfg, line) + if replayAccumulator != nil { + replayAccumulator.ObserveSSELine(line) + } reporter.ObserveResponseModel(line) rawPayload := helps.JSONPayload(line) if len(rawPayload) > 0 && len(pendingFilterLine) > 0 { @@ -970,6 +1028,7 @@ func (e *GeminiVertexExecutor) executeStreamWithServiceAccount(ctx context.Conte helps.CaptureTranslatedClaudeStreamError(responseFormat, lines, &streamErr, func(msg string) error { return statusErr{code: http.StatusBadGateway, msg: msg} }) + commitReplay() for i := range lines { select { case out <- cliproxyexecutor.StreamChunk{Payload: lines[i]}: @@ -1018,12 +1077,21 @@ func (e *GeminiVertexExecutor) executeStreamWithAPIKey(ctx context.Context, auth requestedModel := helps.PayloadRequestedModel(opts, req.Model) requestPath := helps.PayloadRequestPath(opts) helps.SetStringIfDifferentDoc(doc, "model", baseModel) - helps.StripVertexOpenAIResponsesToolCallIDsDoc(doc, from.String()) internalsignature.SanitizeGeminiRequestThoughtSignaturesDoc(doc, "contents") action := getVertexAction(baseModel, true) helps.DropGeminiTrailingThoughtOnlyContentDoc(doc, "contents") helps.EnsureGeminiBoundaryUserContentDoc(doc, "contents") + replayScope := antigravityReasoningReplayScope{} + var replayRequestPayload []byte + if !isImagenModel(baseModel) && antigravityUsesReasoningReplayCache(baseModel) { + var errReplay error + replayScope, replayRequestPayload, _, errReplay = prepareGeminiTopLevelReasoningReplayPayloadDoc(ctx, baseModel, req, opts, doc) + if errReplay != nil { + return nil, errReplay + } + } + helps.StripVertexOpenAIResponsesToolCallIDsDoc(doc, from.String()) // For API key auth, use simpler URL format without project/location url := vertexAPIKeyModelsURL(baseURL, baseModel, action) // Imagen models don't support streaming, skip SSE params @@ -1087,9 +1155,13 @@ func (e *GeminiVertexExecutor) executeStreamWithAPIKey(ctx context.Context, auth if errClose := httpResp.Body.Close(); errClose != nil { log.Errorf("vertex executor: close response body error: %v", errClose) } + if errClear := clearAntigravityReasoningReplayOnInvalidSignature(ctx, replayScope, httpResp.StatusCode, b); errClear != nil { + logAntigravityReasoningReplayDegraded(replayScope, "invalidate", errClear) + } return nil, statusErr{code: httpResp.StatusCode, msg: string(b)} } + replayAccumulator := newAntigravityReasoningReplayAccumulator(replayScope, replayRequestPayload) out := make(chan cliproxyexecutor.StreamChunk, 1) go func() { defer close(out) @@ -1111,9 +1183,16 @@ func (e *GeminiVertexExecutor) executeStreamWithAPIKey(ctx context.Context, auth }() var terminalDelivered bool var sawFinishReason bool + var replayCommitted bool var pendingFilterLine []byte var pendingFilterMsg string var param any + commitReplay := func() { + if !replayCommitted && replayAccumulator != nil && replayAccumulator.terminal && streamErr == nil && helps.ApplyPatchTranslationError(param) == nil { + replayAccumulator.Commit(ctx) + replayCommitted = true + } + } helps.InitializeApplyPatchStream(ctx, to, responseFormat, req.Model, helps.ApplyPatchOriginalRequest(req, opts), body, ¶m) emitTranslatedLine := func(line []byte) bool { lines := helps.TranslateStreamWithClaudeInputTokens(ctx, to, responseFormat, req.Model, helps.ApplyPatchOriginalRequest(req, opts), body, bytes.Clone(line), ¶m, claudeInputTokens) @@ -1121,6 +1200,9 @@ func (e *GeminiVertexExecutor) executeStreamWithAPIKey(ctx context.Context, auth helps.CaptureTranslatedClaudeStreamError(responseFormat, lines, &streamErr, func(msg string) error { return statusErr{code: http.StatusBadGateway, msg: msg} }) + if replayAccumulator != nil && replayAccumulator.terminal && !replayCommitted && antigravityStreamChunksHaveTerminalCommitEvent(lines) { + commitReplay() + } for i := range lines { select { case out <- cliproxyexecutor.StreamChunk{Payload: lines[i]}: @@ -1136,6 +1218,9 @@ func (e *GeminiVertexExecutor) executeStreamWithAPIKey(ctx context.Context, auth for scanner.Scan() { line := scanner.Bytes() helps.AppendAPIResponseChunk(ctx, e.cfg, line) + if replayAccumulator != nil { + replayAccumulator.ObserveSSELine(line) + } reporter.ObserveResponseModel(line) rawPayload := helps.JSONPayload(line) if len(rawPayload) > 0 && len(pendingFilterLine) > 0 { @@ -1228,6 +1313,7 @@ func (e *GeminiVertexExecutor) executeStreamWithAPIKey(ctx context.Context, auth helps.CaptureTranslatedClaudeStreamError(responseFormat, lines, &streamErr, func(msg string) error { return statusErr{code: http.StatusBadGateway, msg: msg} }) + commitReplay() for i := range lines { select { case out <- cliproxyexecutor.StreamChunk{Payload: lines[i]}: @@ -1269,13 +1355,18 @@ func (e *GeminiVertexExecutor) countTokensWithServiceAccount(ctx context.Context fixGeminiImageAspectRatioDoc(baseModel, doc) _ = doc.Set("model", baseModel) - helps.StripVertexOpenAIResponsesToolCallIDsDoc(doc, from.String()) respCtx := context.WithValue(ctx, "alt", opts.Alt) _ = doc.Delete("tools") _ = doc.Delete("generationConfig") _ = doc.Delete("safetySettings") internalsignature.SanitizeGeminiRequestThoughtSignaturesDoc(doc, "contents") helps.EnsureGeminiLeadingUserContentDoc(doc, "contents") + if !isImagenModel(baseModel) && antigravityUsesReasoningReplayCache(baseModel) { + if _, errReplay := prepareGeminiTopLevelCountTokensReasoningReplayPayloadDoc(ctx, baseModel, req, opts, doc); errReplay != nil { + return cliproxyexecutor.Response{}, errReplay + } + } + helps.StripVertexOpenAIResponsesToolCallIDsDoc(doc, from.String()) baseURL := vertexBaseURL(location) url := fmt.Sprintf("%s/%s/projects/%s/locations/%s/publishers/google/models/%s:%s", baseURL, vertexAPIVersion, projectID, location, baseModel, "countTokens") @@ -1373,13 +1464,18 @@ func (e *GeminiVertexExecutor) countTokensWithAPIKey(ctx context.Context, auth * fixGeminiImageAspectRatioDoc(baseModel, doc) _ = doc.Set("model", baseModel) - helps.StripVertexOpenAIResponsesToolCallIDsDoc(doc, from.String()) respCtx := context.WithValue(ctx, "alt", opts.Alt) _ = doc.Delete("tools") _ = doc.Delete("generationConfig") _ = doc.Delete("safetySettings") internalsignature.SanitizeGeminiRequestThoughtSignaturesDoc(doc, "contents") helps.EnsureGeminiLeadingUserContentDoc(doc, "contents") + if !isImagenModel(baseModel) && antigravityUsesReasoningReplayCache(baseModel) { + if _, errReplay := prepareGeminiTopLevelCountTokensReasoningReplayPayloadDoc(ctx, baseModel, req, opts, doc); errReplay != nil { + return cliproxyexecutor.Response{}, errReplay + } + } + helps.StripVertexOpenAIResponsesToolCallIDsDoc(doc, from.String()) // For API key auth, use simpler URL format without project/location url := vertexAPIKeyModelsURL(baseURL, baseModel, "countTokens")