Files
Netcatty/infrastructure/ai/cattyRequestTooLargeRetry.ts
T
陈大猫 42c69902e8 fix(ai): strip Anthropic signed thinking when context prefix mutates (#4073) (#4074)
* fix(ai): strip Anthropic signed thinking when context prefix mutates (#4073)

Anthropic preserved-thinking signatures bind to the exact conversation
prefix. Step compression/prune and durable summary keep-tail were
replaying later signed blocks after mutating earlier tool_results,
causing stable 400 Invalid signature errors on long tool chats.

Strip signed reasoning when prepareStep didAdjust, skip signatures on
durable-summary tails, and one-shot retry after matching 400.

* fix(ai): propagate Anthropic signature stream errors with progress-aware retry (#4074)

P1-1: rethrow signature failures from cattyStreamProcessor error chunks
(and reader catch) via createCattyAnthropicSignatureRetryError so the
turn-driver strip-and-retry path can see them.

P1-2: mirror the 413 hadToolProgress path on signature retry — rebuild
from latest session, retain completed tool results, new assistant
message, and enable write replay before stripping/retrying.

* fix(ai): preserve plain-object Anthropic signature messages in retry wrapper (#4074)

* fix(ai): only strip Anthropic signed thinking when provider allows removal (#4074)

DeepSeek Anthropic-compat requires thinking blocks with replayed tool calls;
gate durable-summary and prepareStep strips behind providerAllowsAnthropicSignedThinkingRemoval. Keep 400 invalid-signature strip-and-retry unchanged.

* chore: retrigger CI after runner shutdown cancel on lint-and-test (#4074)
2026-10-30 21:36:19 -07:00

81 lines
3.0 KiB
TypeScript

export type CattyRequestTooLargeRetryError = Error & {
cattyHadToolProgress?: boolean;
statusCode?: number;
status?: number;
responseBody?: string;
};
export type CattyAnthropicSignatureRetryError = Error & {
cattyHadToolProgress?: boolean;
statusCode?: number;
status?: number;
responseBody?: string;
};
/**
* Mirror errorClassifier.extractMessage so plain SDK / SSE error objects
* with a string `.message` keep that text (not `[object Object]`).
*/
function extractRetryMessage(error: unknown, fallback: string): string {
if (error instanceof Error) return error.message || fallback;
if (typeof error === 'string') return error;
if (error && typeof error === 'object' && 'message' in error) {
const m = (error as { message: unknown }).message;
if (typeof m === 'string' && m.length > 0) return m;
}
return fallback;
}
export function createCattyRequestTooLargeRetryError(
error: unknown,
hadToolProgress: boolean,
): CattyRequestTooLargeRetryError {
const message = extractRetryMessage(error, 'Request too large');
const retryError = new Error(message) as CattyRequestTooLargeRetryError;
retryError.name = 'CattyRequestTooLargeRetryError';
retryError.cause = error;
retryError.cattyHadToolProgress = hadToolProgress;
retryError.statusCode = 413;
if (error && typeof error === 'object') {
const source = error as Record<string, unknown>;
if (typeof source.status === 'number') retryError.status = source.status;
if (typeof source.responseBody === 'string') retryError.responseBody = source.responseBody;
}
return retryError;
}
/**
* Wrap an Anthropic thinking-signature failure so the turn-driver retry path
* can see prior tool progress, while keeping message/body/status intact for
* `isInvalidAnthropicThinkingSignatureError`.
*/
export function createCattyAnthropicSignatureRetryError(
error: unknown,
hadToolProgress: boolean,
): CattyAnthropicSignatureRetryError {
const message = extractRetryMessage(error, 'Invalid signature in thinking block');
const retryError = new Error(message) as CattyAnthropicSignatureRetryError;
retryError.name = 'CattyAnthropicSignatureRetryError';
retryError.cause = error;
retryError.cattyHadToolProgress = hadToolProgress;
if (error && typeof error === 'object') {
const source = error as Record<string, unknown>;
if (typeof source.statusCode === 'number') retryError.statusCode = source.statusCode;
else if (typeof source.status === 'number') retryError.statusCode = source.status;
else retryError.statusCode = 400;
if (typeof source.status === 'number') retryError.status = source.status;
if (typeof source.responseBody === 'string') retryError.responseBody = source.responseBody;
} else {
retryError.statusCode = 400;
}
return retryError;
}
export function hadToolProgressBeforeRequestTooLarge(error: unknown): boolean {
return !!(
error &&
typeof error === 'object' &&
(error as { cattyHadToolProgress?: boolean }).cattyHadToolProgress
);
}