Package com.tnsai.agents.resilience
Class AgentResilienceManager
java.lang.Object
com.tnsai.agents.resilience.AgentResilienceManager
Facade for agent resilience operations.
The AgentResilienceManager provides a unified interface for all resilience concerns including:
- Retry policies with exponential backoff
- Circuit breaker pattern for failure isolation
- Dead letter queue for failed operations
- Health state tracking
- Recovery decisions (degrade, restart, fallback)
- State snapshots for crash recovery
Note for harness auditors (see
tnsai-core/agent_docs/harness-audit.md): this facade is
intentionally not tagged with @modelAssumption. The
patterns it implements (exponential backoff, circuit-breaker with
half-open state, dead-letter queue, fail-fast on budget exhaustion)
are timeless network-engineering primitives, not LLM-version
workarounds. Provider-specific tuning (e.g. retry-on-503 thresholds
for a particular LLM API) lives in the RetryPolicy instances
passed in, which are where any model-version annotations belong.
Usage
AgentResilienceManager resilience = AgentResilienceManager.builder()
.agentId("agent-1")
.retryPolicy(RetryPolicy.defaultPolicy())
.restartPolicy(AgentRestartPolicy.defaultPolicy())
.build();
// Execute with resilience
String result = resilience.execute("search", () -> {
return searchService.search(query);
});
// Check health
if (resilience.getHealthState() == AgentHealthState.DEGRADED) {
// Handle degraded mode
}
- Author:
- TnsAI Team
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder for AgentResilienceManager. -
Method Summary
Modifier and TypeMethodDescriptionbuilder()Creates a new builder for AgentResilienceManager.voidcaptureSnapshot(MemoryStore memoryStore) Captures a state snapshot from the memory store.voidClears all snapshots.<T> TExecutes an operation with full resilience (retry + circuit breaker + fallback).<T> Texecute(String operationId, Callable<T> operation, RetryPolicy retryPolicy) Executes an operation with custom retry policy.<T> Texecute(String operationId, Callable<T> operation, RetryPolicy retryPolicy, FallbackStrategy<T> fallback) Executes an operation with full resilience configuration.Returns the dead letter queue for failed operations.Returns the default retry policy.intReturns the count of failed operations in DLQ.Returns the current health state.Returns the recovery manager.Returns the resilience executor.Returns the state recovery manager.handleFailure(String actionName, Exception exception) Handles a failed operation and determines recovery action.booleanChecks if the agent is in degraded mode.booleanChecks if the agent is healthy.booleanChecks if the agent is in cooldown period after failures.voidHandles a successful operation.voidrestoreLatestSnapshot(MemoryStore memoryStore) Restores the latest snapshot to the memory store.voidsetHealthState(AgentHealthState state) Sets the health state.
-
Method Details
-
builder
Creates a new builder for AgentResilienceManager.- Returns:
- new builder instance
-
execute
Executes an operation with full resilience (retry + circuit breaker + fallback).- Type Parameters:
T- return type- Parameters:
operationId- unique identifier for the operationoperation- the operation to execute- Returns:
- operation result
- Throws:
Exception- if operation fails after retries and no fallback
-
execute
public <T> T execute(String operationId, Callable<T> operation, RetryPolicy retryPolicy) throws Exception Executes an operation with custom retry policy.- Type Parameters:
T- return type- Parameters:
operationId- unique identifier for the operationoperation- the operation to executeretryPolicy- custom retry policy- Returns:
- operation result
- Throws:
Exception- if operation fails after retries
-
execute
public <T> T execute(String operationId, Callable<T> operation, RetryPolicy retryPolicy, FallbackStrategy<T> fallback) throws Exception Executes an operation with full resilience configuration.- Type Parameters:
T- return type- Parameters:
operationId- unique identifier for the operationoperation- the operation to executeretryPolicy- retry policy to usefallback- fallback strategy if operation fails- Returns:
- operation result
- Throws:
Exception- if operation fails after retries and fallback
-
getHealthState
Returns the current health state.- Returns:
- current health state
-
setHealthState
Sets the health state.- Parameters:
state- new health state
-
isHealthy
public boolean isHealthy()Checks if the agent is healthy.- Returns:
- true if healthy
-
isDegraded
public boolean isDegraded()Checks if the agent is in degraded mode.- Returns:
- true if degraded
-
onSuccess
public void onSuccess()Handles a successful operation. -
handleFailure
Handles a failed operation and determines recovery action.- Parameters:
actionName- name of the failed actionexception- the exception that occurred- Returns:
- recovery decision
-
isInCooldown
public boolean isInCooldown()Checks if the agent is in cooldown period after failures.- Returns:
- true if in cooldown
-
captureSnapshot
Captures a state snapshot from the memory store.- Parameters:
memoryStore- the memory store to snapshot
-
restoreLatestSnapshot
Restores the latest snapshot to the memory store.- Parameters:
memoryStore- the memory store to restore to
-
clearSnapshots
public void clearSnapshots()Clears all snapshots. -
getDeadLetterQueue
Returns the dead letter queue for failed operations.- Returns:
- dead letter queue
-
getFailedOperationCount
public int getFailedOperationCount()Returns the count of failed operations in DLQ.- Returns:
- failed operation count
-
getDefaultRetryPolicy
Returns the default retry policy.- Returns:
- default retry policy
-
getResilienceExecutor
Returns the resilience executor.- Returns:
- resilience executor
-
getRecoveryManager
Returns the recovery manager.- Returns:
- recovery manager
-
getStateRecoveryManager
Returns the state recovery manager.- Returns:
- state recovery manager
-