refactor(llm-retry): simplify policy history validation

This commit is contained in:
Turtle
2026-07-26 01:16:41 +08:00
parent fcd355bf45
commit 19a1aec89e
12 changed files with 186 additions and 663 deletions

View File

@@ -11,7 +11,6 @@ import type { Agent, RequestError, RequestErrorDecision } from '@deepseek-ai/dsh
import type { LlmFailure, ResolvedRetryPolicy } from '@deepseek-ai/dsh-llm'
import type { SessionEvent } from '@deepseek-ai/dsh-session'
import { providerForClosedStep } from './history.ts'
import { retryPolicyKey } from './policy-key.ts'
declare module '@deepseek-ai/dsh-session' {
interface SessionEventMap {
@@ -84,6 +83,19 @@ function localDelay(config: ResolvedRetryPolicy, retry: number, random: () => nu
return Math.min(exponential * jitter, config.maxDelayMs)
}
function retryPolicyKey(policy: ResolvedRetryPolicy): string {
return policy.mode === 'always'
? JSON.stringify([policy.mode, policy.initialDelayMs, policy.maxDelayMs, policy.jitterRatio])
: JSON.stringify([
policy.mode,
policy.maxRetries,
[...policy.retryableCodes].sort(),
policy.initialDelayMs,
policy.maxDelayMs,
policy.jitterRatio,
])
}
function cancellableDelay(delayMs: number, signal: AbortSignal): Promise<boolean> {
if (signal.aborted) return Promise.resolve(false)
return new Promise((resolve) => {

View File

@@ -3,9 +3,9 @@
import type { Context } from 'cordis'
import type { Session, SessionEvent } from '@deepseek-ai/dsh-session'
import type { LlmFailure } from '@deepseek-ai/dsh-llm'
import { MAX_TIMER_DELAY_MS } from '@deepseek-ai/dsh-timeout'
import type { InvariantFailure, InvariantInstaller } from '@deepseek-ai/dsh-invariants'
import { providerForClosedStep } from './history.ts'
import { parseRetryPolicyKey } from './policy-key.ts'
import type {} from './index.ts'
const PACKAGE_NAME = '@deepseek-ai/dsh-llm-retry'
@@ -54,11 +54,10 @@ function validateRetry(
fail('llm/retry retry must be a positive safe integer')
}
if (typeof provider !== 'string' || provider.length === 0) {
fail('llm/retry provider must be non-empty string')
fail('llm/retry provider must be a non-empty string')
}
const keyedPolicy = parseRetryPolicyKey(policyKey)
if (keyedPolicy === undefined) {
fail('llm/retry policyKey must encode a canonical resolved policy')
if (typeof policyKey !== 'string' || policyKey.length === 0) {
fail('llm/retry policyKey must be a non-empty string')
}
switch (mode) {
case 'normal': {
@@ -66,29 +65,17 @@ function validateRetry(
if (!Number.isSafeInteger(maxRetries) || maxRetries < 1 || retry > maxRetries) {
fail(`llm/retry retry ${retry} must not exceed a positive safe maxRetries ${maxRetries}`)
}
if (keyedPolicy.mode !== 'normal') {
fail(`llm/retry mode normal must match policyKey mode ${keyedPolicy.mode}`)
}
if (keyedPolicy.maxRetries !== maxRetries) {
fail(`llm/retry maxRetries ${maxRetries} must match policyKey`)
}
if (!keyedPolicy.retryableCodes.includes(failure.code)) {
fail(`llm/retry failure code ${failure.code} must be eligible under policyKey`)
}
break
}
case 'always':
if (keyedPolicy.mode !== 'always') {
fail(`llm/retry mode always must match policyKey mode ${keyedPolicy.mode}`)
}
if ('maxRetries' in event.data) fail('llm/retry always mode must omit maxRetries')
break
default:
fail(`llm/retry mode must be normal or always, got ${String(mode)}`)
}
if (typeof delayMs !== 'number' || !Number.isFinite(delayMs)
|| delayMs < 0 || delayMs > keyedPolicy.maxDelayMs) {
fail(`llm/retry delayMs must be a finite number within policyKey range 0..${keyedPolicy.maxDelayMs}`)
|| delayMs < 0 || delayMs > MAX_TIMER_DELAY_MS) {
fail(`llm/retry delayMs must be a finite number within 0..${MAX_TIMER_DELAY_MS}`)
}
const turnStartIndex = history.findLastIndex(prior =>

View File

@@ -1,100 +0,0 @@
/** Canonical durable identity for resolved retry policies. @module @deepseek-ai/dsh-llm-retry/policy-key */
import type { ResolvedRetryBackoff, ResolvedRetryPolicy } from '@deepseek-ai/dsh-llm'
import { MAX_TIMER_DELAY_MS } from '@deepseek-ai/dsh-timeout'
function parseBackoff(
tuple: readonly unknown[],
offset: number,
): ResolvedRetryBackoff | undefined {
const initialDelayMs = tuple[offset]
const maxDelayMs = tuple[offset + 1]
const jitterRatio = tuple[offset + 2]
if (typeof initialDelayMs !== 'number' || !Number.isFinite(initialDelayMs)
|| initialDelayMs <= 0 || initialDelayMs > MAX_TIMER_DELAY_MS
|| typeof maxDelayMs !== 'number' || !Number.isFinite(maxDelayMs)
|| maxDelayMs <= 0 || maxDelayMs > MAX_TIMER_DELAY_MS
|| initialDelayMs > maxDelayMs
|| typeof jitterRatio !== 'number' || !Number.isFinite(jitterRatio)
|| jitterRatio < 0 || jitterRatio > 1) {
return undefined
}
return { initialDelayMs, maxDelayMs, jitterRatio }
}
/**
* Derive the canonical durable key for one fully resolved provider policy.
* Retryable-code order is normalized because eligibility uses set membership.
* @param policy - immutable policy captured from the serving registration.
* @returns canonical JSON tuple containing every behavior-affecting field.
*/
export function retryPolicyKey(policy: ResolvedRetryPolicy): string {
if (policy.mode === 'always') {
return JSON.stringify([
policy.mode,
policy.initialDelayMs,
policy.maxDelayMs,
policy.jitterRatio,
])
}
return JSON.stringify([
policy.mode,
policy.maxRetries,
[...policy.retryableCodes].sort(),
policy.initialDelayMs,
policy.maxDelayMs,
policy.jitterRatio,
])
}
/**
* Parse a producer-canonical policy key from durable input.
* @param value - untrusted persisted event field.
* @returns the resolved policy encoded by the key, or `undefined` for any non-canonical value.
*/
export function parseRetryPolicyKey(value: unknown): ResolvedRetryPolicy | undefined {
if (typeof value !== 'string' || value.length === 0) return undefined
let tuple: unknown
try {
tuple = JSON.parse(value) as unknown
} catch (_invalidPolicyKeyJson) {
return undefined
}
if (!Array.isArray(tuple)) return undefined
const items = tuple as readonly unknown[]
const mode = items[0]
let policy: ResolvedRetryPolicy
switch (mode) {
case 'always': {
if (items.length !== 4) return undefined
const backoff = parseBackoff(items, 1)
if (backoff === undefined) return undefined
policy = Object.freeze({ mode, ...backoff })
break
}
case 'normal': {
if (items.length !== 6) return undefined
const maxRetries = items[1]
const retryableCodes = items[2]
const backoff = parseBackoff(items, 3)
if (!Number.isSafeInteger(maxRetries) || (maxRetries as number) < 0
|| !Array.isArray(retryableCodes) || retryableCodes.length === 0
|| (retryableCodes as readonly unknown[])
.some(code => typeof code !== 'string' || code.length === 0)
|| new Set(retryableCodes).size !== retryableCodes.length
|| backoff === undefined) {
return undefined
}
policy = Object.freeze({
mode,
maxRetries: maxRetries as number,
retryableCodes: Object.freeze(retryableCodes as string[]),
...backoff,
})
break
}
default:
return undefined
}
return retryPolicyKey(policy) === value ? policy : undefined
}