Commit bc6d1ac
authored
feat(fetch): add retry logic for idempotent API operations (#120)
* feat(fetch): add retry logic for idempotent API operations
Implement automatic retry with exponential backoff for GET/HEAD/OPTIONS requests:
- Add retry configuration env vars (GITLAB_API_RETRY_*)
- Reduce default timeout from 20s to 10s for faster failures
- Add structured TIMEOUT error code for AI client self-correction
- Add idempotent flag to EnhancedToolDefinition type
- Improve logging with request context (url, method, duration)
Retry behavior:
- GET requests retry by default (idempotent)
- POST/PUT/DELETE do NOT retry by default (not idempotent)
- Retries on: timeouts, network errors, 5xx responses, 429 rate limits
- Uses exponential backoff: 1s, 2s, 4s (configurable)
- Respects Retry-After header for 429 responses
Closes #119
* test(fetch): add resetDispatcherCache unit tests
* fix(fetch): cancel response body before retry to prevent socket leaks
- Call response.body?.cancel() before sleep in retry loop
- Use Jest fake timers in Retry-After test
- Pass retry:false in error-handling unit tests
* fix(fetch): exclude unreachable code from coverage
* fix(fetch): improve retry logic robustness and security
- Add URL redaction for logs to prevent secret leakage in upload paths
- Make network error detection case-insensitive for reliable retries
- Validate numeric env vars with Number.isFinite fallback to defaults
- Add download_* to isIdempotentOperation for correct timeout labels
- Update docstring to match actual retry behavior (GET/HEAD/OPTIONS)
- Use fake timers in all retry tests for deterministic execution
- Sync README and .env.example with new retry configuration
* test(fetch): add configuration edge case tests
Add tests for fetch configuration scenarios:
- SOCKS proxy detection and warning logging
- HTTP/HTTPS proxy dispatcher creation
- TLS verification skip warnings
- CA certificate loading success and error paths
- OAuth token context warnings
- URL redaction for invalid URLs and parse errors
* fix(test): expect URL-encoded redaction marker in query params
URL.searchParams.set() URL-encodes values, so [REDACTED] becomes
%5BREDACTED%5D in the logged URL.
* chore: test hook enforcement
* chore: test hook debug 2
* chore: test fixed hook
* fix(fetch): improve URL redaction and cap Retry-After delay
- Redact URL userinfo (username:password) in logs
- Fix upload secret regex to match any path segment, not just hex
- Cap Retry-After header to API_RETRY_MAX_DELAY_MS to prevent excessive waits
- Add validation that Retry-After is positive
- Add test for Retry-After cap behavior
- Update types.ts docs to include download_* in idempotent patterns
* test(handlers): add timeout error handling and idempotency tests
- Add tests for TIMEOUT structured error response
- Test idempotency detection for browse_*, list_*, get_*, download_* tools
- Test retryable flag based on tool type (idempotent vs non-idempotent)
- Test direct StructuredToolError passthrough
- Fix Retry-After cap test to use correct API_RETRY_MAX_DELAY_MS value (4000ms)
* fix: improve URL redaction security and config validation
- Fix catch fallback in redactUrlForLogging to not leak userinfo
- Add positive value validation for config timeout/retry settings
- Update test description to match actual behavior
* test: update URL redaction test for secure fallback behavior
* docs(fetch): update retry JSDoc and test descriptions
- List GET/HEAD/OPTIONS methods in retry option JSDoc
- Reference jest.resetModules in test file header
- Use mocked config value (400ms) in Retry-After cap test
* fix(fetch): preserve error stack trace in failure logs
* fix(fetch): add access_token and oauth_token to URL redaction
* feat(fetch): improve signal handling, error recovery, and HTTP-date parsing
- Wrap response.body?.cancel() in try-catch to handle disturbed body errors
- Merge caller-provided AbortSignal with internal timeout controller
- Support HTTP-date format in Retry-After header (RFC 7231)
- Use AbortSignal.any() on Node.js 20+ with fallback for older versions
* fix(fetch): distinguish caller abort from internal timeout in retry logic
- Use abort reason to identify internal timeout vs caller-initiated abort
- Propagate caller AbortError directly instead of converting to timeout
- Return false from isRetryableError for AbortError
- Document worst-case timing in enhancedFetch JSDoc
* test(fetch): caller abort handling and retry behavior tests
* test(fetch): update AbortError test expectation
* fix(fetch): make backoff sleep abort-aware and accept Retry-After: 0
- sleep() now accepts optional AbortSignal to cancel during backoff
- parseRetryAfter() accepts "0" as valid (immediate retry per RFC 7231)
- Added tests for both features
* test(config): add edge case tests for empty tool/action/param names
* chore(deps): update zod to 4.3.6 and @clack/prompts to 1.0.0-alpha.9
- zod: ^4.3.5 → ^4.3.6
- @clack/prompts: ^0.11.0 → 1.0.0-alpha.9
Breaking changes fixed:
- validate() now receives string | undefined, added nullish coalescing
- SpinnerResult requires additional methods (cancel, error, clear)
- @clack/prompts is now ESM-only, added Jest mock for tests
* test(fetch): AbortError and Retry-After edge cases
- Test AbortError with retry enabled returns false from isRetryableError
- Test pre-aborted signal in retry backoff loop
- Test invalid Retry-After header uses default backoff
- Test Retry-After: 0 for immediate retry
- Package.json: add test:help script with usage docs
* fix(fetch): cleanup abort listeners and use Symbol for timeout reason
- sleep(): remove abort listener on normal timeout completion
- doFetch(): store callerAbortHandler ref and cleanup on completion
- Use Symbol instead of string for TIMEOUT_REASON (true uniqueness)
* fix(fetch): ensure abort rejects with Error, redact end-of-path tokens
- getAbortError() helper wraps non-Error reasons in Error instance
- Hex token regex now matches tokens at end of URL path
* fix(fetch): accept Retry-After with leading zeros per RFC 7231
- Use regex /^\d+$/ instead of String(parseInt(x)) comparison
- RFC 7231 delta-seconds = 1*DIGIT allows "01", "001", etc.
* fix(fetch): preserve AbortError semantics in getAbortError()
- Set error.name = "AbortError" for non-AbortError instances
- Use DOMException for non-Error reasons to maintain abort semantics
- Allows downstream code to reliably identify abort errors1 parent b765cb1 commit bc6d1ac
File tree
20 files changed
+2044
-47
lines changed- src
- cli
- docker
- init
- utils
- tests
- __mocks__/@clack
- unit
- cli/install
- utils
20 files changed
+2044
-47
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
177 | 177 | | |
178 | 178 | | |
179 | 179 | | |
180 | | - | |
181 | | - | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
182 | 192 | | |
183 | 193 | | |
184 | 194 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
51 | | - | |
| 51 | + | |
52 | 52 | | |
53 | 53 | | |
54 | 54 | | |
| |||
517 | 517 | | |
518 | 518 | | |
519 | 519 | | |
520 | | - | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
521 | 525 | | |
522 | 526 | | |
523 | 527 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
38 | 38 | | |
39 | 39 | | |
40 | 40 | | |
| 41 | + | |
| 42 | + | |
41 | 43 | | |
42 | 44 | | |
43 | 45 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
47 | | - | |
48 | | - | |
| 47 | + | |
49 | 48 | | |
| 49 | + | |
| 50 | + | |
50 | 51 | | |
| 52 | + | |
51 | 53 | | |
52 | 54 | | |
53 | 55 | | |
| |||
59 | 61 | | |
60 | 62 | | |
61 | 63 | | |
62 | | - | |
| 64 | + | |
63 | 65 | | |
64 | 66 | | |
65 | 67 | | |
| |||
77 | 79 | | |
78 | 80 | | |
79 | 81 | | |
80 | | - | |
| 82 | + | |
81 | 83 | | |
82 | 84 | | |
83 | 85 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
148 | 148 | | |
149 | 149 | | |
150 | 150 | | |
151 | | - | |
| 151 | + | |
152 | 152 | | |
153 | 153 | | |
154 | 154 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
63 | 63 | | |
64 | 64 | | |
65 | 65 | | |
66 | | - | |
| 66 | + | |
67 | 67 | | |
68 | 68 | | |
69 | 69 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
146 | 146 | | |
147 | 147 | | |
148 | 148 | | |
149 | | - | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
150 | 169 | | |
151 | 170 | | |
152 | 171 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| 9 | + | |
| 10 | + | |
9 | 11 | | |
10 | 12 | | |
11 | 13 | | |
| |||
91 | 93 | | |
92 | 94 | | |
93 | 95 | | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
94 | 110 | | |
95 | 111 | | |
96 | 112 | | |
| |||
113 | 129 | | |
114 | 130 | | |
115 | 131 | | |
116 | | - | |
117 | | - | |
118 | | - | |
119 | | - | |
120 | | - | |
| 132 | + | |
121 | 133 | | |
122 | 134 | | |
123 | 135 | | |
124 | 136 | | |
125 | 137 | | |
126 | 138 | | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
127 | 150 | | |
128 | 151 | | |
129 | 152 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
36 | 44 | | |
37 | 45 | | |
38 | 46 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
133 | 133 | | |
134 | 134 | | |
135 | 135 | | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
136 | 147 | | |
137 | 148 | | |
138 | 149 | | |
| |||
141 | 152 | | |
142 | 153 | | |
143 | 154 | | |
144 | | - | |
| 155 | + | |
| 156 | + | |
145 | 157 | | |
146 | 158 | | |
147 | 159 | | |
| |||
849 | 861 | | |
850 | 862 | | |
851 | 863 | | |
| 864 | + | |
| 865 | + | |
| 866 | + | |
| 867 | + | |
| 868 | + | |
| 869 | + | |
| 870 | + | |
| 871 | + | |
| 872 | + | |
| 873 | + | |
| 874 | + | |
| 875 | + | |
| 876 | + | |
| 877 | + | |
| 878 | + | |
| 879 | + | |
| 880 | + | |
| 881 | + | |
| 882 | + | |
| 883 | + | |
| 884 | + | |
| 885 | + | |
| 886 | + | |
| 887 | + | |
| 888 | + | |
| 889 | + | |
| 890 | + | |
| 891 | + | |
| 892 | + | |
| 893 | + | |
| 894 | + | |
| 895 | + | |
| 896 | + | |
| 897 | + | |
| 898 | + | |
| 899 | + | |
| 900 | + | |
| 901 | + | |
| 902 | + | |
| 903 | + | |
| 904 | + | |
| 905 | + | |
852 | 906 | | |
853 | 907 | | |
854 | 908 | | |
| |||
0 commit comments