Skip to content

feat(core): add UploadStateMachine for rate limiting (1/3)#1153

Closed
abueide wants to merge 2 commits intomasterfrom
feature/tapi-state-machine
Closed

feat(core): add UploadStateMachine for rate limiting (1/3)#1153
abueide wants to merge 2 commits intomasterfrom
feature/tapi-state-machine

Conversation

@abueide
Copy link
Contributor

@abueide abueide commented Mar 6, 2026

Summary

  • Adds UploadStateMachine for managing global rate limiting state (429 responses)
  • Adds RateLimitConfig, UploadStateData, HttpConfig types to types.ts
  • Adds validateRateLimitConfig in config-validation.ts with SDD-specified bounds
  • Core test suite (10 tests) with test helpers

Merge order

This PR merges first. PR #1152 and #1150 rebase onto master after this lands.

  1. feat(core): add UploadStateMachine for rate limiting (1/3) #1153 (this) — UploadStateMachine + rate limit types
  2. feat(core): add BackoffManager for transient error backoff (2/3) #1152 — BackoffManager + backoff types
  3. feat(core): add error classification and default HTTP config (3/3) #1150 — Error classification + defaultHttpConfig

Components

UploadStateMachine (135 lines)

State machine managing global rate limiting for 429 responses per the TAPI SDD.

Methods:

  • canUpload() — upload gate that respects rate limit wait times
  • handle429(retryAfterSeconds) — sets RATE_LIMITED state with configurable retry limits
  • reset() — clears rate limit state on successful upload
  • getGlobalRetryCount() — returns current retry count for X-Retry-Count header

States: READY | RATE_LIMITED

Persists state across app restarts via sovran store.

Config Validation (42 lines)

Validates and clamps rate limit configuration to safe ranges:

  • maxRetryInterval: 0.1s – 86,400s
  • maxRateLimitDuration: 60s – 604,800s
  • maxRetryCount: 1 – 100

Type Definitions (19 lines added to types.ts)

  • RateLimitConfig — rate limit settings from Settings CDN
  • UploadStateData — state machine persistence shape
  • HttpConfig — container on SegmentAPISettings

Test plan

  • 10 core tests covering canUpload gate, handle429 transitions, reset, max retry/duration limits, disabled config
  • devbox run test-unit passes
  • No unrelated diffs (git diff master -- .github/ scripts/ wiki/ is empty)

abueide added a commit that referenced this pull request Mar 6, 2026
Adds comprehensive extended test suites for edge cases and implementation
details that supplement the core tests in the feature PRs:

- UploadStateMachine.extended.test.ts: disabled config, multiple retries,
  getter tests, persistence tests
- BatchUploadManager.extended.test.ts: unique IDs, disabled config, getter
  tests, detailed exponential backoff algorithm tests, persistence tests
- SegmentDestination.extended.test.ts: state reset after success, legacy
  behavior, Retry-After header parsing

These tests provide thorough coverage of edge cases, backwards compatibility,
and implementation details without adding bulk to the core feature PRs.

Related to PRs: #1150, #1151, #1152, #1153

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Add the UploadStateMachine component for managing global rate limiting
state for 429 responses, along with supporting types and config validation.

Components:
- RateLimitConfig, UploadStateData, HttpConfig types
- validateRateLimitConfig with SDD-specified bounds
- UploadStateMachine with canUpload/handle429/reset/getGlobalRetryCount
- Core test suite (10 tests) and test helpers
- backoff/index.ts barrel export

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@abueide abueide force-pushed the feature/tapi-state-machine branch from 14a1c97 to 160a84c Compare March 6, 2026 22:14
@abueide abueide changed the title feat(core): add UploadStateMachine with rate limiting and tests (1a/4) feat(core): add UploadStateMachine for rate limiting (1/3) Mar 6, 2026
Improvements to UploadStateMachine:
- Add comprehensive JSDoc comments for all public methods
- Add input validation in handle429() for negative/large retryAfterSeconds
- Add logging when transitioning from RATE_LIMITED to READY
- Add edge case tests for negative, zero, and very large retry values
- Fix linting issues (template literal expressions, unsafe assignments)

All 13 tests pass. Ready for review.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@abueide
Copy link
Contributor Author

abueide commented Mar 10, 2026

Superseded by #1154

@abueide abueide closed this Mar 10, 2026
@abueide abueide deleted the feature/tapi-state-machine branch March 12, 2026 14:42
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.

1 participant