Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions gui/src/i18n/de.ts
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,7 @@ export const de: Record<TKey, string> = {
"logs.detail.attempt.recovery.opaqueBlobRejection": "Veralteter verschlüsselter Zustand verworfen",
"logs.detail.attempt.recovery.reasoningEffortDowngrade": "Reasoning-Aufwand reduziert",
"logs.detail.attempt.recovery.anthropicFastDowngrade": "Schnellmodus abgelehnt, erneut mit Standardtempo versucht",
"logs.detail.attempt.recovery.anthropicThinkingReplay": "Denk-Signatur abgelehnt, erneut ohne frühere Denkblöcke versucht",
"logs.detail.outcome.label": "Ergebnis",
"logs.detail.outcome.completed": "Abgeschlossen",
"logs.detail.outcome.failed": "Fehlgeschlagen",
Expand Down
1 change: 1 addition & 0 deletions gui/src/i18n/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,7 @@ export const en = {
"logs.detail.attempt.recovery.opaqueBlobRejection": "Stale encrypted state dropped",
"logs.detail.attempt.recovery.reasoningEffortDowngrade": "Reasoning effort downgraded",
"logs.detail.attempt.recovery.anthropicFastDowngrade": "Fast mode refused, retried at standard speed",
"logs.detail.attempt.recovery.anthropicThinkingReplay": "Thinking signature rejected, retried without earlier thinking blocks",
"logs.detail.outcome.label": "Outcome",
"logs.detail.outcome.completed": "Completed",
"logs.detail.outcome.failed": "Failed",
Expand Down
1 change: 1 addition & 0 deletions gui/src/i18n/fr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,7 @@ export const fr: Record<TKey, string> = {
"logs.detail.attempt.recovery.opaqueBlobRejection": "État chiffré obsolète abandonné",
"logs.detail.attempt.recovery.reasoningEffortDowngrade": "Effort de raisonnement réduit",
"logs.detail.attempt.recovery.anthropicFastDowngrade": "Mode rapide refusé, nouvel essai à vitesse standard",
"logs.detail.attempt.recovery.anthropicThinkingReplay": "Signature de réflexion refusée, nouvel essai sans les blocs de réflexion précédents",
"logs.detail.outcome.label": "Résultat",
"logs.detail.outcome.completed": "Terminé",
"logs.detail.outcome.failed": "Échec",
Expand Down
1 change: 1 addition & 0 deletions gui/src/i18n/ja.ts
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,7 @@ export const ja: Record<TKey, string> = {
"logs.detail.attempt.recovery.opaqueBlobRejection": "古い暗号化状態を破棄",
"logs.detail.attempt.recovery.reasoningEffortDowngrade": "推論強度を下げて再試行",
"logs.detail.attempt.recovery.anthropicFastDowngrade": "高速モードが拒否され、標準速度で再試行",
"logs.detail.attempt.recovery.anthropicThinkingReplay": "思考ブロックの署名が拒否され、以前の思考ブロックなしで再試行",
"logs.detail.outcome.label": "結果",
"logs.detail.outcome.completed": "完了",
"logs.detail.outcome.failed": "失敗",
Expand Down
1 change: 1 addition & 0 deletions gui/src/i18n/ko.ts
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,7 @@ export const ko: Record<TKey, string> = {
"logs.detail.attempt.recovery.opaqueBlobRejection": "만료된 암호화 상태 제거",
"logs.detail.attempt.recovery.reasoningEffortDowngrade": "추론 강도 하향",
"logs.detail.attempt.recovery.anthropicFastDowngrade": "빠른 모드 거부, 표준 속도로 재시도",
"logs.detail.attempt.recovery.anthropicThinkingReplay": "사고 서명 거부, 이전 사고 블록 없이 재시도",
"logs.detail.outcome.label": "결과",
"logs.detail.outcome.completed": "완료",
"logs.detail.outcome.failed": "실패",
Expand Down
1 change: 1 addition & 0 deletions gui/src/i18n/ru.ts
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,7 @@ export const ru: Record<TKey, string> = {
"logs.detail.attempt.recovery.opaqueBlobRejection": "Устаревшее зашифрованное состояние отброшено",
"logs.detail.attempt.recovery.reasoningEffortDowngrade": "Уровень рассуждения понижен",
"logs.detail.attempt.recovery.anthropicFastDowngrade": "Быстрый режим отклонён, повтор со стандартной скоростью",
"logs.detail.attempt.recovery.anthropicThinkingReplay": "Подпись блока рассуждений отклонена, повтор без прежних блоков рассуждений",
"logs.detail.outcome.label": "Итог",
"logs.detail.outcome.completed": "Завершено",
"logs.detail.outcome.failed": "Сбой",
Expand Down
1 change: 1 addition & 0 deletions gui/src/i18n/tr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -953,6 +953,7 @@ export const tr: Record<TKey, string> = {
"logs.detail.attempt.recovery.opaqueBlobRejection": "Eski şifreli durum bırakıldı",
"logs.detail.attempt.recovery.reasoningEffortDowngrade": "Akıl yürütme düzeyi düşürüldü",
"logs.detail.attempt.recovery.anthropicFastDowngrade": "Hızlı mod reddedildi, standart hızda yeniden denendi",
"logs.detail.attempt.recovery.anthropicThinkingReplay": "Düşünme imzası reddedildi, önceki düşünme blokları olmadan yeniden denendi",
"logs.detail.outcome.label": "Sonuç",
"logs.detail.outcome.completed": "Tamamlandı",
"logs.detail.outcome.failed": "Başarısız",
Expand Down
1 change: 1 addition & 0 deletions gui/src/i18n/vi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,7 @@ export const vi: Record<TKey, string> = {
"logs.detail.attempt.recovery.opaqueBlobRejection": "Đã bỏ trạng thái mã hóa cũ",
"logs.detail.attempt.recovery.reasoningEffortDowngrade": "Đã giảm mức suy luận",
"logs.detail.attempt.recovery.anthropicFastDowngrade": "Chế độ nhanh bị từ chối, thử lại ở tốc độ chuẩn",
"logs.detail.attempt.recovery.anthropicThinkingReplay": "Chữ ký khối suy nghĩ bị từ chối, thử lại không kèm các khối suy nghĩ trước",
"logs.detail.outcome.label": "Kết quả",
"logs.detail.outcome.completed": "Hoàn tất",
"logs.detail.outcome.failed": "Thất bại",
Expand Down
1 change: 1 addition & 0 deletions gui/src/i18n/zh-TW.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2494,6 +2494,7 @@ export const zhTW: Record<TKey, string> = {
"logs.detail.attempt.recovery.opaqueBlobRejection": "已捨棄過期加密狀態",
"logs.detail.attempt.recovery.reasoningEffortDowngrade": "已降低推理強度",
"logs.detail.attempt.recovery.anthropicFastDowngrade": "快速模式遭拒,改以標準速度重試",
"logs.detail.attempt.recovery.anthropicThinkingReplay": "思考簽章遭拒,改以不含先前思考區塊重試",
"logs.detail.outcome.label": "結果",
"logs.detail.outcome.completed": "已完成",
"logs.detail.outcome.failed": "失敗",
Expand Down
1 change: 1 addition & 0 deletions gui/src/i18n/zh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,7 @@ export const zh: Record<TKey, string> = {
"logs.detail.attempt.recovery.opaqueBlobRejection": "已丢弃过期加密状态",
"logs.detail.attempt.recovery.reasoningEffortDowngrade": "已降低推理强度",
"logs.detail.attempt.recovery.anthropicFastDowngrade": "快速模式被拒,按标准速度重试",
"logs.detail.attempt.recovery.anthropicThinkingReplay": "思考签名被拒,去掉先前思考块后重试",
"logs.detail.outcome.label": "结果",
"logs.detail.outcome.completed": "已完成",
"logs.detail.outcome.failed": "失败",
Expand Down
1 change: 1 addition & 0 deletions gui/src/pages/Logs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ const RECOVERY_KIND_KEYS = {
"opaque-blob-rejection": "logs.detail.attempt.recovery.opaqueBlobRejection",
"reasoning-effort-downgrade": "logs.detail.attempt.recovery.reasoningEffortDowngrade",
"anthropic-fast-downgrade": "logs.detail.attempt.recovery.anthropicFastDowngrade",
"anthropic-thinking-replay": "logs.detail.attempt.recovery.anthropicThinkingReplay",
} as const satisfies Record<AttemptRecoveryKind, string>;

/** Map a metric-unavailable reason to its i18n key. */
Expand Down
53 changes: 50 additions & 3 deletions src/adapters/anthropic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,40 @@ function isLikelyRealAnthropicThinkingSignature(signature: string | undefined):
return /^[A-Za-z0-9+/_=-]+$/.test(signature);
}

/**
* 400 recovery for "Invalid `signature` in `thinking` block" (`parsed.options.dropThinkingHistory`):
* remove every thinking/redacted_thinking block from the replayed history. Anthropic allows
* omitting them, except on the assistant turn of an open tool loop when thinking is on, so the
* return value says the LAST assistant turn lost its blocks while holding a tool_use and the
* caller must send this one call with thinking off.
*/
function dropThinkingFromMessages(messages: unknown[]): boolean {
let lostInLoop = false;
for (let i = 0; i < messages.length; i++) {
const message = messages[i] as { role?: unknown; content?: unknown } | null;
if (message?.role !== "assistant") continue;
lostInLoop = false;
if (!Array.isArray(message.content)) continue;
const kept = message.content.filter(block => {
const type = (block as { type?: unknown } | null)?.type;
return type !== "thinking" && type !== "redacted_thinking";
});
if (kept.length === message.content.length) continue;
if (kept.length === 0) {
messages.splice(i--, 1);
continue;
}
message.content = kept;
lostInLoop = kept.some(block => (block as { type?: unknown } | null)?.type === "tool_use");
}
return lostInLoop;
}

function turnThinkingOff(body: Record<string, unknown>, modelId: string): void {
if (supportsExplicitThinkingDisable(modelId)) body.thinking = { type: "disabled" };
else delete body.thinking;
}

/**
* Bridge error fidelity (web-search/images loops): extract a display-safe summary from an
* Anthropic JSON error envelope so `Provider error <status>` carries the upstream reason.
Expand Down Expand Up @@ -960,7 +994,10 @@ function orphanToolResultContent(msg: OcxToolResultMessage): string | unknown[]
function messagesToAnthropicFormat(
parsed: OcxParsedRequest,
toolNames: { toWire: (name: string) => string },
): { system: string | undefined; messages: unknown[] } {
): { system: string | undefined; messages: unknown[]; thinkingDroppedInLoop: boolean } {
// True when the LAST assistant turn held a tool_use and lost its thinking blocks to the
// dropThinkingHistory recovery; the caller then sends the call with thinking off.
let thinkingDroppedInLoop = false;
// One allocator for the whole request: a tool_result must resolve to the SAME wire id its
// call got, and two distinct raw ids must never collapse into one. Conforming ids are claimed
// first so a rewritten id can never squat on an id another call legitimately owns.
Expand Down Expand Up @@ -1008,12 +1045,17 @@ function messagesToAnthropicFormat(
const preface: unknown[] = [];
const toolUses: unknown[] = [];
const toolUseIds: string[] = [];
let droppedThinking = false;
for (const part of aMsg.content) {
if (part.type === "text") {
const text = (part as OcxTextContent).text;
if (text) preface.push({ type: "text", text });
} else if (part.type === "thinking") {
const t = part as OcxThinkingContent;
if (parsed.options.dropThinkingHistory) {
droppedThinking = true;
continue;
}
// Redacted blocks replay verbatim FIRST (they preceded the visible thinking block
// in the original stream order preserved by the bridge envelope).
for (const data of t.redacted ?? []) {
Expand Down Expand Up @@ -1041,6 +1083,7 @@ function messagesToAnthropicFormat(
// Anthropic treats text/thinking after tool_use as ending the tool turn, which makes
// earlier tool_use ids look unpaired (#620 / common multi-step history shape).
const content = [...preface, ...toolUses];
thinkingDroppedInLoop = droppedThinking && toolUses.length > 0;
if (content.length === 0) break;
messages.push({ role: "assistant", content });
if (toolUseIds.length > 0) {
Expand Down Expand Up @@ -1098,7 +1141,7 @@ function messagesToAnthropicFormat(
messages.push({ role: "user", content: "(continue)" });
}

return { system, messages };
return { system, messages, thinkingDroppedInLoop };
}

function toolsToAnthropicFormat(parsed: OcxParsedRequest, toolNames: { toWire: (name: string) => string }): unknown[] | undefined {
Expand Down Expand Up @@ -1218,6 +1261,9 @@ export function createAnthropicAdapter(provider: OcxProviderConfig, cacheRetenti
if (!Array.isArray(body.messages)) {
throw new Error("validated Anthropic source messages missing");
}
if (parsed.options.dropThinkingHistory && dropThinkingFromMessages(body.messages)) {
turnThinkingOff(body, parsed.modelId);
}
await normalizeAnthropicImages(body.messages, { tierBias: incoming?.imageTierBias ?? 0 });
enforceAnthropicImageLimits(body.messages);

Expand Down Expand Up @@ -1277,7 +1323,7 @@ export function createAnthropicAdapter(provider: OcxProviderConfig, cacheRetenti
};
}

const { system, messages } = messagesToAnthropicFormat(parsed, toolNames);
const { system, messages, thinkingDroppedInLoop } = messagesToAnthropicFormat(parsed, toolNames);
// Before image normalization, so the framing block is present for every downstream pass.
if (isAgentRouterEndpoint(provider.baseUrl)) applyAgentRouterLanguageFraming(messages);
// Primary image layer: resize/re-encode to fit Anthropic limits without dropping
Expand Down Expand Up @@ -1365,6 +1411,7 @@ export function createAnthropicAdapter(provider: OcxProviderConfig, cacheRetenti
delete body.temperature;
delete body.top_p;
}
if (thinkingDroppedInLoop) turnThinkingOff(body, parsed.modelId);

const textFormat = parsed.options.textFormat;
if (textFormat?.type === "json_schema" && textFormat.schema) {
Expand Down
1 change: 1 addition & 0 deletions src/lib/request-failure-attribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ const STATUS_CONFIRMED_RECOVERY_KINDS: Readonly<Partial<Record<AttemptRecoveryKi
"opaque-blob-rejection": 400,
"reasoning-effort-downgrade": 400,
"anthropic-fast-downgrade": 400,
"anthropic-thinking-replay": 400,
});

function refinedFourHundredCause(
Expand Down
3 changes: 3 additions & 0 deletions src/lib/request-failure-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,9 @@ const RECOVERY_KIND_CAUSE = {
// Anthropic refused `speed: "fast"` (no usage credits, org not enabled, model outside the
// lane); the same turn succeeds once the parameter is dropped.
"anthropic-fast-downgrade": "parameter-rejected",
// Anthropic rejected a replayed thinking-block signature (minted by another model or account);
// the same turn succeeds once the history thinking blocks are dropped.
"anthropic-thinking-replay": "ciphertext-refusal",
} as const satisfies Record<AttemptRecoveryKind, RequestFailureCause>;

export function causeForRecoveryKind(kind: AttemptRecoveryKind): RequestFailureCause {
Expand Down
20 changes: 20 additions & 0 deletions src/server/responses/adapter-dispatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ import {
CONSOLE_GO_UPLOAD_RETRY_DELAY_MS,
reasoningEffortRejectionText,
anthropicFastRefused,
anthropicThinkingSignatureRejected,
} from "./core-opaque-recovery";
import { shouldAttemptImageTierRetry } from "../image-retry";
import {
Expand Down Expand Up @@ -423,6 +424,7 @@ export async function prepareAdapterExchange(
// adapter, and imageTierBias — once armed — rides EVERY subsequent rebuild so a
// 413→429 rotation cannot silently undo the tightening.
let imageRetryAttempted = false;
const thinkingReplayGuard: { attempted: boolean } = { attempted: false };
const opaqueBlobRecoveryGuard: OpaqueBlobRecoveryGuard = { attempted: false };
// Console Go answers a transient 400 "Invalid upload request." for bodies it accepts
// moments later; at most one byte-identical replay is allowed per request.
Expand Down Expand Up @@ -1062,6 +1064,24 @@ export async function prepareAdapterExchange(
upstreamResponse = result;
continue recovery;
}
// Anthropic 400 "Invalid `signature` in `thinking` block": a replayed thinking block was signed
// by another model or account. Resend once without the history thinking blocks (spiral guard:
// single attempt); the adapter turns thinking off for an open tool loop.
if (await anthropicThinkingSignatureRejected(
upstreamResponse,
transportState.activeAdapter.name,
thinkingReplayGuard.attempted,
upstream.signal,
)) {
thinkingReplayGuard.attempted = true;
parsed.options.dropThinkingHistory = true;
invalidateSameTargetRequest();
try { void upstreamResponse.body?.cancel().catch(() => {}); } catch { /* already consumed/closed */ }
const result = await rebuildAndRefetch("anthropic-thinking-replay");
if ("failed" in result) return result.failed;
upstreamResponse = result;
continue recovery;
}
// Console Go (opencode-zen / opencode-go) intermittently rejects a body it accepts seconds
// later with 400 invalid_request_error / "Invalid upload request." Replay the
// byte-identical request once after the exact gateway rejection.
Expand Down
24 changes: 24 additions & 0 deletions src/server/responses/core-opaque-recovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,30 @@ export async function anthropicFastRefused(
}
}

/**
* Anthropic answers 400 "messages.N.content.M: Invalid `signature` in `thinking` block" when a
* replayed thinking block was signed by a different model or account than the one now serving the
* turn. A signature carries no readable origin and the raw Messages lane forwards the caller's
* history byte for byte, so the proxy cannot tell beforehand; it resends once without the history
* thinking blocks. Anthropic's own docs allow omitting them; the adapter handles an open tool loop.
*/
const THINKING_SIGNATURE_REJECTION = /Invalid `signature` in `(?:redacted_)?thinking` block/;

export async function anthropicThinkingSignatureRejected(
response: Response,
adapterName: string,
alreadyAttempted: boolean,
signal: AbortSignal,
): Promise<boolean> {
if (alreadyAttempted || adapterName !== "anthropic" || response.status !== 400) return false;
try {
const body = await readBoundedResponseBody(response.clone(), { signal });
return body.displaySafe && !body.truncated && THINKING_SIGNATURE_REJECTION.test(body.text);
} catch {
return false;
}
}

export async function opaqueBlobRejectionBodyForRecovery(
response: Response,
outboundBody: string | undefined,
Expand Down
2 changes: 2 additions & 0 deletions src/types/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,8 @@ export interface OcxRequestOptions {
serviceTier?: string;
/** Final outbound tier action, resolved after the provider/model wire is settled. */
tierDecision?: TierDecision;
/** Internal: set by the 400 "Invalid `signature` in `thinking` block" recovery; the Anthropic adapter drops history thinking blocks. */
dropThinkingHistory?: boolean;
/** Internal B0 observation inputs; adapters combine these with the wire they actually serialize. */
tierObservation?: TierObservationContext;
presencePenalty?: number;
Expand Down
1 change: 1 addition & 0 deletions src/usage/telemetry-contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export const ATTEMPT_RECOVERY_KIND_ROSTER = Object.freeze([
"empty-completion",
"reasoning-effort-downgrade",
"anthropic-fast-downgrade",
"anthropic-thinking-replay",
] as const);

export type AttemptRecoveryKind = typeof ATTEMPT_RECOVERY_KIND_ROSTER[number];
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/test-layout-expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -1237,6 +1237,7 @@
"responses-account-change-scrub.test.ts": "responses",
"responses-account-label.test.ts": "responses",
"responses-anthropic-fast-downgrade.test.ts": "responses",
"responses-anthropic-thinking-replay.test.ts": "responses",
"responses-azure-opaque-blob-recovery.test.ts": "responses",
"responses-bare-echo-helper-fence.test.ts": "responses",
"responses-canonical-only-top-level-fields.test.ts": "responses",
Expand Down
Loading
Loading