ff_sdk/lib.rs
1//! FlowFabric Worker SDK — public API for worker authors.
2//!
3//! This crate depends on `ff-script` for the Lua-function types, Lua error
4//! kinds (`ScriptError`), and retry helpers (`is_retryable_kind`,
5//! `kind_to_stable_str`). Consumers using `ff-sdk` do not need to import
6//! `ff-script` directly for normal worker operations, but can if they need
7//! the `ScriptError` or retry types.
8//!
9//! # Quick start
10//!
11//! The production claim path is
12//! [`FlowFabricWorker::claim_from_grant`]: obtain a
13//! [`ClaimGrant`] from `ff_scheduler::Scheduler::claim_for_worker`
14//! (the scheduler enforces budget, quota, and capability checks),
15//! then hand it to the SDK. `claim_next` is gated behind the
16//! default-off `direct-valkey-claim` feature and bypasses admission
17//! control — fine for benchmarks, not production.
18//!
19//! ```rust,ignore
20//! use ff_sdk::{FlowFabricWorker, WorkerConfig};
21//! use ff_core::backend::BackendConfig;
22//! use ff_core::types::{LaneId, Namespace, WorkerId, WorkerInstanceId};
23//!
24//! #[tokio::main]
25//! async fn main() -> Result<(), ff_sdk::SdkError> {
26//! let config = WorkerConfig {
27//! backend: BackendConfig::valkey("localhost", 6379),
28//! worker_id: WorkerId::new("my-worker"),
29//! worker_instance_id: WorkerInstanceId::new("my-worker-instance-1"),
30//! namespace: Namespace::new("default"),
31//! lanes: vec![LaneId::new("main")],
32//! capabilities: Vec::new(),
33//! lease_ttl_ms: 30_000,
34//! claim_poll_interval_ms: 1_000,
35//! max_concurrent_tasks: 1,
36//! partition_config: None,
37//! };
38//!
39//! let worker = FlowFabricWorker::connect(config).await?;
40//! let lane = LaneId::new("main");
41//!
42//! // In a real deployment `grant` is obtained from the
43//! // scheduler's `claim_for_worker` RPC/helper; it carries the
44//! // execution id, capability match, and admission result.
45//! # let grant: ff_sdk::ClaimGrant = unimplemented!();
46//! let task = worker.claim_from_grant(lane, grant).await?;
47//! println!("claimed: {}", task.execution_id());
48//! // Process task...
49//! task.complete(Some(b"done".to_vec())).await?;
50//! Ok(())
51//! }
52//! ```
53//!
54//! # Migration: `direct-valkey-claim` → scheduler-issued grants
55//!
56//! The `direct-valkey-claim` cargo feature — which gates
57//! [`FlowFabricWorker::claim_next`] — is **deprecated** in favour of
58//! the pair of scheduler-issued grant entry points:
59//!
60//! * [`FlowFabricWorker::claim_from_grant`] — fresh claims. Use
61//! `ff_scheduler::Scheduler::claim_for_worker` to obtain the
62//! [`ClaimGrant`], then hand it to the SDK.
63//! * [`FlowFabricWorker::claim_from_resume_grant`] — resumed claims
64//! for an `attempt_interrupted` execution. Wraps a
65//! [`ResumeGrant`].
66//!
67//! `claim_next` bypasses budget and quota admission control; the
68//! grant-based path does not. See each method's rustdoc for the
69//! exact migration recipe.
70//!
71//! `ClaimGrant` / `ResumeGrant` / `ReclaimGrant` (and the
72//! `ClaimPolicy` / `ResumeToken` wire types on the scheduler-owner
73//! side) are re-exported from `ff-sdk` (#283) so consumers do not need
74//! a direct `ff-scheduler` dep just to type these signatures.
75//!
76//! [`ClaimGrant`]: crate::ClaimGrant
77//! [`ResumeGrant`]: crate::ResumeGrant
78
79// v0.12 PR-6: `admin` is always compiled. The one Valkey-typed helper
80// (`rotate_waitpoint_hmac_secret_all_partitions`, which takes a
81// `&ferriskey::Client`) stays item-gated behind `valkey-default`;
82// everything else in the module is transport-agnostic (reqwest +
83// ff-core contracts + serde).
84pub mod admin;
85pub mod config;
86pub mod engine_error;
87#[cfg(any(
88 feature = "layer-tracing",
89 feature = "layer-ratelimit",
90 feature = "layer-metrics",
91 feature = "layer-circuit-breaker",
92))]
93pub mod layer;
94// v0.12 PR-6: `snapshot` is always compiled. Every method routes
95// through `self.backend_ref().*` — no direct ferriskey usage.
96pub mod snapshot;
97// v0.12 PR-2: `task` is always compiled. Items that depend on
98// ferriskey / ff-backend-valkey / ff-core streaming+suspension types
99// are gated at the item level inside `task.rs`; the module itself is
100// reachable under `default-features = false, features = ["sqlite"]`
101// so consumers can name `ClaimedTask` as a type.
102pub mod task;
103// v0.12 PR-6: Valkey-specific connect preamble extracted from
104// `FlowFabricWorker::connect`. Gated behind `valkey-default` because
105// the body is all ferriskey `client.cmd(...)` + `client.hgetall(...)`.
106#[cfg(feature = "valkey-default")]
107pub(crate) mod valkey_preamble;
108// RFC-023 Phase 1a (§4.4 item 10): `worker` is always compiled.
109// The ferriskey-dependent methods inside it are `valkey-default`-
110// gated at the item level; the module itself no longer is, so
111// `FlowFabricWorker::connect_with` + the trait-forwarder accessors
112// are reachable under `default-features = false, features = ["sqlite"]`.
113pub mod worker;
114
115// Re-exports for convenience.
116//
117// v0.12 PR-6: the transport-agnostic admin surface (HTTP REST client +
118// request/response shapes + `PartitionRotationOutcome`) is reachable
119// under `--no-default-features --features sqlite`.
120// `rotate_waitpoint_hmac_secret_all_partitions` stays `valkey-default`-
121// gated — it takes a `&ferriskey::Client` and calls the
122// `ff_rotate_waitpoint_hmac_secret` FCALL directly. Option 1 of the
123// PR-6 plan: keep the helper in `admin.rs` item-gated to minimize
124// consumer blast radius; relocation to `ff-backend-valkey` is v0.13
125// scope if ever justified.
126pub use admin::{
127 FlowFabricAdminClient, IssueReclaimGrantRequest, IssueReclaimGrantResponse,
128 PartitionRotationOutcome, RotateWaitpointSecretRequest, RotateWaitpointSecretResponse,
129};
130#[cfg(feature = "valkey-default")]
131pub use admin::rotate_waitpoint_hmac_secret_all_partitions;
132// RFC-023 Phase 1a: re-export `SqliteBackend` so consumers using
133// `ff-sdk = { default-features = false, features = ["sqlite"] }` can
134// name it as `ff_sdk::SqliteBackend` without pinning the
135// `ff-backend-sqlite` crate directly. Also keeps the dep graph
136// `ff-backend-sqlite` edge reachable from ff-sdk's public API.
137#[cfg(feature = "sqlite")]
138pub use ff_backend_sqlite::SqliteBackend;
139pub use config::WorkerConfig;
140pub use engine_error::{
141 BugKind, ConflictKind, ContentionKind, EngineError, StateKind, ValidationKind,
142};
143// #88: backend-agnostic transport error surface. Consumers that
144// previously matched on `ferriskey::ErrorKind` via `valkey_kind()`
145// now match on `BackendErrorKind` via `backend_kind()`.
146pub use ff_core::engine_error::{BackendError, BackendErrorKind};
147// `FailOutcome` is ff-core-native (Stage 1a move); re-export
148// unconditionally so consumers can name `ff_sdk::FailOutcome` even
149// under `--no-default-features`.
150pub use ff_core::backend::FailOutcome;
151// `ResumeSignal` is also ff-core-native (Stage 0 move).
152pub use ff_core::backend::ResumeSignal;
153#[cfg(feature = "valkey-default")]
154pub use task::{
155 read_stream, tail_stream, tail_stream_with_visibility, AppendFrameOutcome, ClaimedTask,
156 Signal, SignalOutcome, StreamCursor, StreamFrames, SuspendedHandle, TrySuspendOutcome,
157 MAX_TAIL_BLOCK_MS, STREAM_READ_HARD_CAP,
158};
159// RFC-015 stream-durability-mode public surface. Re-exported so
160// consumers can name `ff_sdk::StreamMode` etc. alongside the older
161// `AppendFrameOutcome` / `StreamCursor` re-exports. Gated on
162// `valkey-default` because the worker surface that uses them is too.
163#[cfg(feature = "valkey-default")]
164pub use ff_core::backend::{
165 PatchKind, StreamMode, SummaryDocument, TailVisibility, SUMMARY_NULL_SENTINEL,
166};
167// RFC-013 Stage 1d — typed suspend surface lives in `ff_core::contracts`
168// and is reachable via `ff_sdk::*` too. `SuspendOutcome` path is
169// preserved via this re-export so `ff_sdk::SuspendOutcome` still
170// compiles.
171#[cfg(feature = "valkey-default")]
172pub use ff_core::contracts::{
173 CompositeBody, CountKind, IdempotencyKey, ResumeCondition, ResumePolicy, ResumeTarget,
174 SignalMatcher, SuspendArgs, SuspendOutcome, SuspendOutcomeDetails, SuspensionReasonCode,
175 SuspensionRequester, TimeoutBehavior, WaitpointBinding,
176};
177// #283 — Claim-flow wire types. Consumers typing `claim_from_grant` /
178// `claim_from_resume_grant` / `claim_from_reclaim_grant` signatures (or
179// wrapping `EngineBackend::claim_for_worker`) can name these as
180// `ff_sdk::ClaimGrant` etc. without pinning `ff-scheduler` directly.
181// `Scheduler` itself is intentionally not re-exported: implementing a
182// scheduler is specialized and stays behind the `ff-scheduler` dep.
183pub use ff_core::backend::{ClaimPolicy, ResumeToken};
184pub use ff_core::contracts::{ClaimGrant, ReclaimGrant, ResumeGrant};
185// RFC-023 Phase 1a (§4.4 item 10): re-export always reachable. The
186// Valkey-specific methods (`connect`, `claim_*`, `deliver_signal`)
187// are item-level cfg-gated; under sqlite-only features the type
188// exposes `connect_with`, `backend`, `completion_backend`, `config`,
189// and `partition_config`.
190pub use worker::FlowFabricWorker;
191
192/// SDK error type.
193#[derive(Debug, thiserror::Error)]
194pub enum SdkError {
195 /// Backend transport error. Previously wrapped `ferriskey::Error`
196 /// directly (#88); now carries a backend-agnostic
197 /// [`BackendError`] so consumers match on
198 /// [`BackendErrorKind`] instead of ferriskey's native taxonomy.
199 /// The ferriskey → [`BackendError`] mapping lives in
200 /// `ff_backend_valkey::backend_error::backend_error_from_ferriskey`.
201 #[error("backend: {0}")]
202 Backend(#[from] BackendError),
203
204 /// Backend error with additional context (e.g. call-site label).
205 /// Previously `ValkeyContext { source: ferriskey::Error }` (#88).
206 #[error("backend: {context}: {source}")]
207 BackendContext {
208 #[source]
209 source: BackendError,
210 context: String,
211 },
212
213 /// FlowFabric engine error — typed sum over Lua error codes + transport
214 /// faults. See [`EngineError`] for the variant-granularity contract.
215 /// Replaces the previous `Script(ScriptError)` carrier (#58.6).
216 ///
217 /// `Box`ed to keep `SdkError`'s stack footprint small: the richest
218 /// variant (`ConflictKind::DependencyAlreadyExists { existing:
219 /// EdgeSnapshot }`) is ~200 bytes. Boxing keeps `Result<T, SdkError>`
220 /// at the same width every other variant pays.
221 #[error("engine: {0}")]
222 Engine(Box<EngineError>),
223
224 /// Configuration error. `context` identifies the call site / logical
225 /// operation (e.g. `"describe_execution: exec_core"`, `"admin_client"`).
226 /// `field` names the specific offending field when the error is
227 /// field-scoped (e.g. `Some("public_state")`), or `None` for
228 /// whole-object validation (e.g. `"at least one lane is required"`).
229 /// `message` carries dynamic detail: source-error rendering, the
230 /// offending raw value, etc.
231 #[error("{}", fmt_config(.context, .field.as_deref(), .message))]
232 Config {
233 context: String,
234 field: Option<String>,
235 message: String,
236 },
237
238 /// Worker is at its configured `max_concurrent_tasks` capacity —
239 /// the caller should retry later. Returned by
240 /// [`FlowFabricWorker::claim_from_grant`] and
241 /// [`FlowFabricWorker::claim_from_reclaim_grant`] when the
242 /// concurrency semaphore is saturated. Distinct from `Ok(None)`:
243 /// a `ClaimGrant`/`ResumeGrant` represents real work already
244 /// selected by the scheduler, so silently dropping it would waste
245 /// the grant and let the grant TTL elapse. Surfacing the
246 /// saturation lets the caller release the grant (or wait +
247 /// retry).
248 ///
249 /// # Classification
250 ///
251 /// * [`SdkError::is_retryable`] returns `true` — saturation is
252 /// transient: any in-flight task's
253 /// complete/fail/cancel/drop releases a permit. Retry after
254 /// milliseconds, not a retry loop with backoff for a backend
255 /// transport failure.
256 /// * [`SdkError::backend_kind`] returns `None` — this is not a
257 /// backend transport error, so there is no
258 /// [`BackendErrorKind`] to inspect. Callers that fan out on
259 /// `backend_kind()` should match `WorkerAtCapacity` explicitly
260 /// (or use `is_retryable()`).
261 ///
262 /// [`FlowFabricWorker::claim_from_grant`]: crate::FlowFabricWorker::claim_from_grant
263 /// [`FlowFabricWorker::claim_from_reclaim_grant`]: crate::FlowFabricWorker::claim_from_reclaim_grant
264 #[error("worker at capacity: max_concurrent_tasks reached")]
265 WorkerAtCapacity,
266
267 /// HTTP transport error from the admin REST surface. Carries
268 /// the underlying `reqwest::Error` via `#[source]` so callers
269 /// can inspect `is_timeout()` / `is_connect()` / etc. for
270 /// finer-grained retry logic. Distinct from
271 /// [`SdkError::Backend`] — this fires on the HTTP/JSON surface,
272 /// not on the Lua/Valkey hot path.
273 #[error("http: {context}: {source}")]
274 Http {
275 #[source]
276 source: reqwest::Error,
277 context: String,
278 },
279
280 /// The admin REST endpoint returned a non-2xx response.
281 ///
282 /// Fields surface the server-side `ErrorBody` JSON shape
283 /// (`{ error, kind?, retryable? }`) as structured values so
284 /// cairn-fabric and other consumers can match without
285 /// re-parsing the body:
286 ///
287 /// * `status` — HTTP status code.
288 /// * `message` — the `error` string from the JSON body (or
289 /// the raw body if it didn't parse as JSON).
290 /// * `kind` — server-supplied Valkey `ErrorKind` label for 5xxs
291 /// backed by a transport error; `None` for 4xxs.
292 /// * `retryable` — server-supplied hint; `None` for 4xxs.
293 /// * `raw_body` — the full response body, preserved for logging
294 /// when the JSON shape doesn't match.
295 #[error("admin api: {status}: {message}")]
296 AdminApi {
297 status: u16,
298 message: String,
299 kind: Option<String>,
300 retryable: Option<bool>,
301 raw_body: String,
302 },
303}
304
305/// Renders `SdkError::Config` as `config: <context>[.<field>]: <message>`.
306/// The `field` slot is omitted when `None` (whole-object validation).
307fn fmt_config(context: &str, field: Option<&str>, message: &str) -> String {
308 match field {
309 Some(f) => format!("config: {context}.{f}: {message}"),
310 None => format!("config: {context}: {message}"),
311 }
312}
313
314/// Lift a native `ferriskey::Error` into [`SdkError::Backend`] via
315/// [`ff_backend_valkey::backend_error_from_ferriskey`] (#88). Keeps
316/// `?`-propagation ergonomic at FCALL/transport call sites while
317/// the public variant stays backend-agnostic.
318#[cfg(feature = "valkey-default")]
319impl From<ferriskey::Error> for SdkError {
320 fn from(err: ferriskey::Error) -> Self {
321 Self::Backend(ff_backend_valkey::backend_error_from_ferriskey(&err))
322 }
323}
324
325/// Build an [`SdkError::BackendContext`] from a native
326/// `ferriskey::Error` and a call-site label, preserving the
327/// backend-agnostic shape on the public surface (#88).
328#[cfg(feature = "valkey-default")]
329pub(crate) fn backend_context(
330 err: ferriskey::Error,
331 context: impl Into<String>,
332) -> SdkError {
333 SdkError::BackendContext {
334 source: ff_backend_valkey::backend_error_from_ferriskey(&err),
335 context: context.into(),
336 }
337}
338
339/// Preserves the ergonomic `?`-propagation from FCALL sites that
340/// return `Result<_, ScriptError>`. Routes through `EngineError`'s
341/// typed classification so every call site gets the same
342/// variant-level detail without hand-written conversion.
343impl From<ff_script::error::ScriptError> for SdkError {
344 fn from(err: ff_script::error::ScriptError) -> Self {
345 // ff-script's `From<ScriptError> for EngineError` owns the
346 // mapping table (#58.6). See `ff_script::engine_error_ext`.
347 Self::Engine(Box::new(EngineError::from(err)))
348 }
349}
350
351impl From<EngineError> for SdkError {
352 fn from(err: EngineError) -> Self {
353 Self::Engine(Box::new(err))
354 }
355}
356
357impl SdkError {
358 /// Returns the classified [`BackendErrorKind`] if this error
359 /// carries a backend transport fault. Covers the direct
360 /// [`SdkError::Backend`] / [`SdkError::BackendContext`] variants
361 /// and `Engine(EngineError::Transport { .. })` via the
362 /// ScriptError-aware downcast in `ff_script::engine_error_ext`.
363 ///
364 /// Renamed from `valkey_kind` in #88 — the previous return type
365 /// `Option<ferriskey::ErrorKind>` leaked ferriskey into every
366 /// consumer doing retry classification.
367 pub fn backend_kind(&self) -> Option<BackendErrorKind> {
368 match self {
369 Self::Backend(be) => Some(be.kind()),
370 Self::BackendContext { source, .. } => Some(source.kind()),
371 #[cfg(feature = "valkey-default")]
372 Self::Engine(e) => ff_script::engine_error_ext::valkey_kind(e)
373 .map(ff_backend_valkey::classify_ferriskey_kind),
374 #[cfg(not(feature = "valkey-default"))]
375 Self::Engine(_) => None,
376 // HTTP/admin-surface errors carry no backend fault;
377 // the admin path never touches the backend directly from
378 // the SDK side. Use `AdminApi.kind` for the server-supplied
379 // label when present.
380 Self::Config { .. }
381 | Self::WorkerAtCapacity
382 | Self::Http { .. }
383 | Self::AdminApi { .. } => None,
384 }
385 }
386
387 /// Whether this error is safely retryable by a caller. For backend
388 /// transport variants, delegates to
389 /// [`BackendErrorKind::is_retryable`]. For `Engine` errors, returns
390 /// `true` iff the typed classification is
391 /// `ErrorClass::Retryable`. `Config` errors are never retryable.
392 pub fn is_retryable(&self) -> bool {
393 match self {
394 Self::Backend(be) | Self::BackendContext { source: be, .. } => {
395 be.kind().is_retryable()
396 }
397 Self::Engine(e) => {
398 matches!(
399 ff_script::engine_error_ext::class(e),
400 ff_core::error::ErrorClass::Retryable
401 )
402 }
403 // WorkerAtCapacity is retryable: the saturation is transient
404 // and clears as soon as a concurrent task completes.
405 Self::WorkerAtCapacity => true,
406 // HTTP transport: timeouts and connect failures are
407 // retryable (transient network state); body-decode or
408 // request-build errors are terminal (caller must fix
409 // the code). `reqwest::Error` exposes both predicates.
410 Self::Http { source, .. } => source.is_timeout() || source.is_connect(),
411 // Admin API errors: trust the server's `retryable` hint
412 // when present; otherwise fall back to the HTTP-standard
413 // retryable-status set (429, 502, 503, 504). 5xxs without
414 // a hint are conservatively non-retryable — the caller can
415 // override with `AdminApi.status`-based logic if needed.
416 // 502 covers reverse-proxy transients (ALB/nginx returning
417 // Bad Gateway when ff-server restarts mid-request).
418 Self::AdminApi {
419 status, retryable, ..
420 } => retryable.unwrap_or(matches!(*status, 429 | 502 | 503 | 504)),
421 Self::Config { .. } => false,
422 }
423 }
424}
425
426#[cfg(all(test, feature = "valkey-default"))]
427mod tests {
428 use super::*;
429 use ferriskey::ErrorKind;
430 use ff_script::error::ScriptError;
431
432 fn mk_fk_err(kind: ErrorKind) -> ferriskey::Error {
433 ferriskey::Error::from((kind, "synthetic"))
434 }
435
436 #[test]
437 fn backend_kind_direct_and_context() {
438 assert_eq!(
439 SdkError::from(mk_fk_err(ErrorKind::IoError)).backend_kind(),
440 Some(BackendErrorKind::Transport)
441 );
442 assert_eq!(
443 crate::backend_context(mk_fk_err(ErrorKind::BusyLoadingError), "connect")
444 .backend_kind(),
445 Some(BackendErrorKind::BusyLoading)
446 );
447 }
448
449 #[test]
450 fn backend_kind_delegates_through_engine_transport() {
451 let err = SdkError::from(ScriptError::Valkey(mk_fk_err(ErrorKind::ClusterDown)));
452 assert_eq!(err.backend_kind(), Some(BackendErrorKind::Cluster));
453 }
454
455 #[test]
456 fn backend_kind_none_for_lua_and_config() {
457 assert_eq!(
458 SdkError::from(ScriptError::LeaseExpired).backend_kind(),
459 None
460 );
461 assert_eq!(
462 SdkError::Config {
463 context: "worker_config".into(),
464 field: Some("bearer_token".into()),
465 message: "bad host".into(),
466 }
467 .backend_kind(),
468 None
469 );
470 }
471
472 #[test]
473 fn is_retryable_transport() {
474 // Transport-bucketed kinds (IoError, FatalSend/Receive,
475 // ProtocolDesync) are retryable under the #88 classifier.
476 assert!(SdkError::from(mk_fk_err(ErrorKind::IoError)).is_retryable());
477 // Auth-bucketed kinds are terminal.
478 assert!(!SdkError::from(mk_fk_err(ErrorKind::AuthenticationFailed)).is_retryable());
479 // Protocol-bucketed kinds (ResponseError, ParseError, TypeError,
480 // InvalidClientConfig, etc.) are terminal.
481 assert!(!SdkError::from(mk_fk_err(ErrorKind::ResponseError)).is_retryable());
482 }
483
484 #[test]
485 fn is_retryable_engine_delegates_to_class() {
486 // NoEligibleExecution is classified Retryable via EngineError::class().
487 assert!(SdkError::from(ScriptError::NoEligibleExecution).is_retryable());
488 // StaleLease is Terminal.
489 assert!(!SdkError::from(ScriptError::StaleLease).is_retryable());
490 // Transport(Valkey(IoError)) is Retryable via class() delegation.
491 assert!(
492 SdkError::from(ScriptError::Valkey(mk_fk_err(ErrorKind::IoError))).is_retryable()
493 );
494 }
495
496 /// Regression (#98): `SdkError::Config` carries `context`, optional
497 /// `field`, and `message` separately so consumers can pattern-match on
498 /// the offending field without parsing the Display string. Test covers
499 /// both the field-scoped and whole-object renderings.
500 #[test]
501 fn config_structured_fields_render_and_match() {
502 let with_field = SdkError::Config {
503 context: "admin_client".into(),
504 field: Some("bearer_token".into()),
505 message: "is empty or all-whitespace".into(),
506 };
507 assert_eq!(
508 with_field.to_string(),
509 "config: admin_client.bearer_token: is empty or all-whitespace"
510 );
511 assert!(matches!(
512 &with_field,
513 SdkError::Config { field: Some(f), .. } if f == "bearer_token"
514 ));
515
516 let whole_object = SdkError::Config {
517 context: "worker_config".into(),
518 field: None,
519 message: "at least one lane is required".into(),
520 };
521 assert_eq!(
522 whole_object.to_string(),
523 "config: worker_config: at least one lane is required"
524 );
525 assert!(matches!(
526 &whole_object,
527 SdkError::Config { field: None, .. }
528 ));
529 }
530
531 #[test]
532 fn is_retryable_config_false() {
533 assert!(
534 !SdkError::Config {
535 context: "worker_config".into(),
536 field: None,
537 message: "at least one lane is required".into(),
538 }
539 .is_retryable()
540 );
541 }
542
543 #[test]
544 fn is_retryable_admin_api_uses_server_hint_when_present() {
545 let err = SdkError::AdminApi {
546 status: 429,
547 message: "throttled".into(),
548 kind: None,
549 retryable: Some(false),
550 raw_body: String::new(),
551 };
552 assert!(!err.is_retryable());
553
554 let err = SdkError::AdminApi {
555 status: 500,
556 message: "valkey timeout".into(),
557 kind: Some("IoError".into()),
558 retryable: Some(true),
559 raw_body: String::new(),
560 };
561 assert!(err.is_retryable());
562 }
563
564 #[test]
565 fn is_retryable_admin_api_falls_back_to_standard_retryable_statuses() {
566 // 502 covers ALB/nginx Bad Gateway transients on ff-server
567 // restart — same retry-is-safe as 503/504 because rotation
568 // is idempotent server-side.
569 for s in [429u16, 502, 503, 504] {
570 let err = SdkError::AdminApi {
571 status: s,
572 message: "x".into(),
573 kind: None,
574 retryable: None,
575 raw_body: String::new(),
576 };
577 assert!(err.is_retryable(), "status {s} should be retryable");
578 }
579 for s in [400u16, 401, 403, 404, 500] {
580 let err = SdkError::AdminApi {
581 status: s,
582 message: "x".into(),
583 kind: None,
584 retryable: None,
585 raw_body: String::new(),
586 };
587 assert!(!err.is_retryable(), "status {s} should NOT be retryable without hint");
588 }
589 }
590
591 #[test]
592 fn valkey_kind_none_for_admin_surface() {
593 let err = SdkError::AdminApi {
594 status: 500,
595 message: "x".into(),
596 kind: Some("IoError".into()),
597 retryable: Some(true),
598 raw_body: String::new(),
599 };
600 assert_eq!(err.backend_kind(), None);
601 }
602}