Package com.tnsai.state


package com.tnsai.state
State persistence and checkpointing for agent recovery.

This package provides mechanisms for saving and restoring agent state, enabling fault tolerance and recovery in long-running agent systems.

Checkpointing Architecture

 ┌─────────────────────────────────────────────────────────────┐
 │                      Checkpointer                            │
 │                     (Interface)                              │
 └──────────────────────────┬──────────────────────────────────┘
                            │
    ┌───────┬───────┬───────┼───────┬───────┬───────┐
    │       │       │       │       │       │       │
    ▼       ▼       ▼       ▼       ▼       ▼       ▼
 InMemory  File  SQLite  PostgreSQL MongoDB  Redis (Custom)
 

Available Implementations

ImplementationUse CasePersistence
InMemoryCheckpointerDevelopment/TestingNo (volatile)
FileCheckpointerSingle-node, simple deploymentsYes (JSON files)
invalid reference
SQLiteCheckpointer
Embedded, single-nodeYes (SQLite DB)
invalid reference
PostgreSQLCheckpointer
Production, multi-nodeYes (PostgreSQL)
invalid reference
MongoDBCheckpointer
Document-based, scalableYes (MongoDB)
RedisCheckpointerDistributed, high-perfYes (tnsai-distributed-redis)

State Components

ComponentDescriptionPersisted Data
CheckpointPoint-in-time snapshotFull agent state
ThreadStateExecution thread stateMessages, context
AgentStateAgent configurationBeliefs, desires, etc.

Usage Example


 // Create checkpointer
 Checkpointer checkpointer = new InMemoryCheckpointer();

 // Save checkpoint
 Checkpoint checkpoint = Checkpoint.builder()
     .threadId("thread-123")
     .state(agentState)
     .build();
 checkpointer.save(checkpoint);

 // Restore checkpoint
 Optional<Checkpoint> restored = checkpointer.load("thread-123");
 if (restored.isPresent()) {
     agent.restore(restored.get());
 }

 // List checkpoints
 List<String> threads = checkpointer.listThreads();

 // Delete old checkpoints
 checkpointer.delete("thread-123");
 

Recovery Scenarios

  • Process restart: Load latest checkpoint and resume
  • Failure recovery: Rollback to last known good state
  • Debugging: Inspect state at specific points
  • Long conversations: Persist and resume later

Distributed Checkpointing

For distributed systems, see tnsai-distributed-redis which provides Redis-backed checkpointing with distributed locks.

See Also: