Skip to content

Conversation

@duyhungtnn
Copy link
Collaborator

@duyhungtnn duyhungtnn commented Oct 16, 2025

Fix #269

This pull request introduces a robust, reusable retry utility for asynchronous operations, applies it to critical networking and scheduling logic, and adds comprehensive tests to ensure reliability. The main improvements are the addition of a generic promiseRetriable function, refactoring of network and evaluation scheduling code to use this utility, and enhanced test coverage for both the new utility and its integrations.

Core Retry Utility:

  • Added a generic promiseRetriable function in PromiseRetriable.ts that provides configurable retry logic with support for linear and constant backoff strategies, and a customizable retry predicate. This includes the RetryPolicy and ShouldRetryFn types.

Networking Improvements:

  • Refactored postInternal in post.ts to use promiseRetriable, enabling automatic retries (up to 3 times) on deployment-related 499 errors (ClientClosedRequestException). The core POST logic was moved to a private _postInternal function. [1] [2]

Scheduler Improvements:

  • Refactored EvaluationTask to use promiseRetriable for fetching evaluations, simplifying retry logic and ensuring retries only occur while the task is running. Removed manual retry counting and rescheduling logic. [1] [2] [3]

Testing Enhancements:

  • Added comprehensive tests for promiseRetriable covering success, failure, delay, backoff strategies, and early termination scenarios in PromiseRetriable.spec.ts.
  • Added tests for postInternal to verify retry behavior on 499 errors in post.spec.ts.
  • Added a test for ApiClientImpl to ensure it calls postInternal correctly in ApiClientImpl.spec.ts.
  • Improved cleanup and reliability in EvaluationTask.spec.ts by ensuring task.stop() is always called and mocks are restored. [1] [2]

Error Handling:

  • Updated error handling in BKTClientImpl to ignore errors from the storage layer when logging evaluation failures, preventing unhandled promise rejections. [1] [2]

Refactored fetchEvaluationsInternal to accept options for shouldTrackFailure and timeoutMillis, enabling failure tracking only on the last allowed retry. Updated EvaluationTask to set shouldTrackFailure accordingly and added tests to verify correct behavior for failure tracking during retries.
@duyhungtnn duyhungtnn requested a review from Copilot October 16, 2025 09:39
Removed '(merged)' from the test suite name to accurately reflect the test context in EvaluationTask.spec.ts.
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR refactors evaluation fetching to support conditional failure tracking during retries and adds tests to verify that tracking only occurs on the last allowed retry.

  • Introduces an options object to fetchEvaluationsInternal to control shouldTrackFailure and timeoutMillis.
  • Updates EvaluationTask to flag only the last retry for failure tracking and exposes getRetryCount for assertions.
  • Adds unit tests for conditional tracking and improves test cleanup.

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
test/internal/scheduler/EvaluationTask.spec.ts Adds tests validating shouldTrackFailure behavior and tracking calls; adjusts suite name and test cleanup.
src/internal/scheduler/EvaluationTask.ts Passes shouldTrackFailure based on last retry, adds isLastTry and getRetryCount.
src/BKTClient.ts Refactors fetchEvaluations to pass options; fetchEvaluationsInternal now accepts options and conditionally tracks failures.

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Refactored the fetchEvaluationsInternal method in BKTClientImpl to handle default options more explicitly and avoid mutation. Added comments to clarify the purpose of getRetryCount in EvaluationTask.
@duyhungtnn duyhungtnn requested a review from Copilot October 16, 2025 09:55
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Adjusted spacing in constructor and method calls for consistency. Set a default value for the options parameter in fetchEvaluationsInternal.
Added catch blocks to suppress errors from the storage layer when logging evaluation events, ensuring that these errors do not affect the main execution flow.
@duyhungtnn duyhungtnn requested a review from Copilot October 16, 2025 10:07
@duyhungtnn duyhungtnn self-assigned this Oct 16, 2025
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Simplified the way options are passed and destructured in fetchEvaluationsInternal for clarity and conciseness. Also reset the task variable to undefined in EvaluationTask.spec.ts after cleanup.
@duyhungtnn duyhungtnn requested a review from Copilot October 16, 2025 10:14
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Simplified destructuring of the options parameter by removing the unnecessary nullish coalescing operator. This change improves code clarity.
Updated the mock implementation in EvaluationTask.spec.ts to use parameter types from BKTClientImpl.fetchEvaluationsInternal, improving type safety and maintainability in the test.
@duyhungtnn duyhungtnn requested a review from cre8ivejp October 16, 2025 10:20
Introduces a futureRetriable function for retrying async operations with configurable retry policy and error filtering. Includes comprehensive unit tests covering success, delay, max retries, and conditional retry logic.
Renamed FutureRetriable.ts and its test to PromiseRetriable.ts, updating all references and function names accordingly. Refactored postInternal to use promiseRetriable for retry logic on deployment-related 499 errors, improving clarity and consistency in naming.
@duyhungtnn duyhungtnn marked this pull request as draft October 16, 2025 15:44
Introduces a 'backoffStrategy' option to the RetryPolicy, supporting 'linear' (default) and 'constant' delay strategies for retry attempts. Updates implementation to calculate delay per attempt and adds tests for both linear and constant backoff behaviors.
Introduces unit tests for ApiClientImpl to verify it calls postInternal with correct parameters, and for postInternal to ensure it retries on 499 Client Closed Request errors. These tests improve coverage and validate error handling and request logic.
Simplifies fetchEvaluationsInternal by removing the shouldTrackFailure option and always tracking failures. Refactors EvaluationTask to use a generic promiseRetriable utility for retries, removing custom retry state and logic. Updates tests to remove now-obsolete failure tracking and retry count tests.
@duyhungtnn duyhungtnn requested a review from Copilot October 17, 2025 03:02
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@duyhungtnn duyhungtnn changed the title feat: add conditional failure tracking to evaluation fetch retries feat: auto retry on deployment-related 499 errors Oct 17, 2025
Added 'backoffStrategy' to the retry policy in postInternal and ensured promiseRetriable is only called when EvaluationTask is running. This improves control over retry behavior and prevents unnecessary retries when the task is not active.
@duyhungtnn duyhungtnn requested a review from Copilot October 17, 2025 03:14
@duyhungtnn duyhungtnn marked this pull request as ready for review October 17, 2025 03:15
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Added logic to EvaluationTask to prevent retrying if the polling interval is less than or equal to the retry interval. Updated tests to verify that retries are skipped when the polling interval is short enough.
@duyhungtnn duyhungtnn requested a review from Copilot October 17, 2025 03:47
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Replaces runAllTimersAsync with advanceTimersByTimeAsync(1000) to more precisely control timer advancement during retry delay in postInternal tests. Also adds a comment for future cancellation support in PromiseRetriable.
- Fix scheduler stopping when pollingInterval <= retryInterval on error

- Fix scheduler stopping after successful fetch with retryCount === 0

- Add tests to verify continuous scheduling behavior
Extended the test to verify that the timer triggers multiple times by checking the request count after running pending timers twice.
@duyhungtnn duyhungtnn requested a review from Copilot October 21, 2025 08:46
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Adds console.debug statements to log errors from the storage layer in fetchEvaluations trackFailure and trackSuccess handlers. Also fixes a comment formatting issue in PromiseRetriable.ts.
Updated logging in fetchEvaluations to use console.error instead of console.debug for storage layer errors, making error visibility more prominent.
Copy link
Member

@cre8ivejp cre8ivejp left a comment

Choose a reason for hiding this comment

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

Thank you!

@cre8ivejp cre8ivejp merged commit e924bff into main Oct 22, 2025
4 checks passed
@cre8ivejp cre8ivejp deleted the fix/ignore-error-log-when-in-retry-process branch October 22, 2025 03:38
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.

feat: auto retry on deployment-related 499 errors

3 participants