Skip to content

refactor(core): Implement explicit retry policy handling in exceptions - #710

Merged
haiphucnguyen merged 3 commits into
mainfrom
feature/improvements
Sep 9, 2026
Merged

haiphucnguyen merged 3 commits into
mainfrom
feature/improvements

Conversation

@haiphucnguyen

Copy link
Copy Markdown
Collaborator
  • Introduce a retryPolicy property to AskimoException to define transient vs. terminal errors.
  • Standardize error classification across the codebase using ExceptionMapper.
  • Define specific user exceptions, such as UnsupportedSamplingException, with immediate retry policies.
  • Refactor streaming message paths to rely on the centralized ExceptionMapper for retry decision making.
  • Improve resilience by correctly differentiating between transient network and context errors.

- Introduce a `retryPolicy` property to `AskimoException` to define transient vs. terminal errors.
- Standardize error classification across the codebase using `ExceptionMapper`.
- Define specific user exceptions, such as `UnsupportedSamplingException`, with immediate retry policies.
- Refactor streaming message paths to rely on the centralized `ExceptionMapper` for retry decision making.
- Improve resilience by correctly differentiating between transient network and context errors.
Copilot AI lite review requested due to automatic review settings September 9, 2026 02:44

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The streaming empty-HTTP-response detection and terminal rendering path have concrete correctness/performance issues (cause-chain matching and redundant exception mapping/logging) that should be fixed before approval.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

Implements an explicit retry-policy model on AskimoException and refactors streaming error handling to use centralized exception classification (ExceptionMapper) for consistent retry decisions and localized terminal messaging.

Changes:

  • Added RetryPolicy and AskimoException.retryPolicy, with concrete policies on key user exceptions (network/rate-limit/timeout/context/tool-call/sampling).
  • Refactored classifyStreamingError and streaming retry side-effects to rely on ExceptionMapper, returning the classified exception for deterministic side-effect dispatch.
  • Expanded streaming error classifier tests to validate retry policy behavior and ensure network/tool/system errors render localized keys when i18n bundles aren’t loaded.
File summaries
File Description
shared/src/test/kotlin/io/askimo/core/providers/StreamingErrorClassifierTest.kt Adds coverage for retry-policy classification and localized terminal messaging keys across common streaming failures.
shared/src/main/kotlin/io/askimo/core/providers/ModelCapabilitiesCache.kt Extracts context-length detection into isContextLengthMessage for reuse by ExceptionMapper.
shared/src/main/kotlin/io/askimo/core/providers/ChatClientExtensions.kt Centralizes streaming error classification via ExceptionMapper, returns classified exception on retryable path, and updates retry loop logic accordingly.
shared/src/main/kotlin/io/askimo/core/exception/UserException.kt Assigns explicit retry policies to relevant user exceptions and introduces UnsupportedSamplingException.
shared/src/main/kotlin/io/askimo/core/exception/ExceptionMapper.kt Extends type/message mapping (network + context-length reuse + sampling) to drive consistent classification.
shared/src/main/kotlin/io/askimo/core/exception/AskimoException.kt Introduces RetryPolicy and adds retryPolicy to the exception base type with a safe default.
Review details
  • Files reviewed: 6/6 changed files
  • Comments generated: 2
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

There are confirmed correctness issues in the new retry/side-effect flow (cache mutation even when retries are exhausted) and locale-dependent context-length matching that can break classification.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Review details

Suppressed comments (2)

Previously missed (2) — in code that hasn't changed since the last review.

shared/src/main/kotlin/io/askimo/core/providers/ModelCapabilitiesCache.kt:516

  • isContextLengthMessage uses message.lowercase() which is locale-dependent; in some locales (e.g. Turkish) this can break case-insensitive matching and lead to misclassification. Use locale-stable lowercasing.
    shared/src/main/kotlin/io/askimo/core/exception/ExceptionHandler.kt:35
  • The comment says re-running classification would re-log an "unmapped exception" warning, but ExceptionMapper logs the unmapped chain as an error. Adjust wording to avoid implying the wrong log level/message.
  • Files reviewed: 7/7 changed files
  • Comments generated: 2
  • Review effort level: Lite

Comment on lines +227 to +230
// Context window exceeded (checked before generic 400 to avoid misclassification).
// Reuses the exact same pattern as `Throwable.isContextLengthError()` (the outer
// context-retry loop in ChatClientExtensions) so the two never drift apart.
isContextLengthMessage(combinedMessage) ->

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

There are documentation inconsistencies in changed code (stale reference to a removed API and an incorrect “no logging side-effects” claim) that should be corrected before approval.

Review details

Suppressed comments (2)

Previously missed (1) — in code that hasn't changed since the last review.

shared/src/main/kotlin/io/askimo/core/exception/UserException.kt:206

  • The KDoc still references ChatClientExtensions.isMalformedToolCallError, but that helper was removed in this refactor, so the documentation now points to a non-existent API. Update the reference to the current retry/classification path (e.g. sendStreamingMessageWithCallback / ExceptionMapper).

shared/src/main/kotlin/io/askimo/core/providers/ChatClientExtensions.kt:91

  • classifyStreamingError's KDoc says it has "no side-effects (no logging)", but the implementation calls ExceptionHandler.handle(...) for terminal cases, which logs. Either avoid rendering/logging here (return the exception and render at the call site) or adjust the KDoc to reflect the current behavior.
 * No side-effects (no logging, no cache mutations) — those stay at the call site so this
 * stays fully unit-testable. Delegates to [ExceptionMapper] for classification and retry
 * policy, except two cases needing context the mapper doesn't have:
 * - [InsufficientContextException]'s message is already fully rendered.
 * - The empty-HTTP-response case needs `provider`/`model` to build its hint.
  • Files reviewed: 7/7 changed files
  • Comments generated: 0 new
  • Review effort level: Lite

@haiphucnguyen
haiphucnguyen merged commit 87903e0 into main Sep 9, 2026
10 of 11 checks passed
@haiphucnguyen
haiphucnguyen deleted the feature/improvements branch September 9, 2026 03:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants