forge-reasoning 0.1.2

Reasoning tools for LLM debugging - checkpointing, hypotheses, contradictions
Documentation
# TDD Wave 5: Integration & API

**Date**: 2026-02-19  
**Focus**: Forge agent integration, service API, event streaming

---

## Wave 5 Tests Summary

| Test | Description | Status |
|------|-------------|--------|
| 41 | CheckpointService creation ||
| 42 | Multi-session support ||
| 43 | Auto-checkpointing ||
| 44 | Service restore ||
| 45 | Checkpoint streaming/events ||
| 46 | API commands ||
| 47 | Background persistence ||
| 48 | Checkpoint annotations ||
| 49 | Concurrent sessions ||
| 50 | Service metrics & health ||

**Total**: 10 new tests, all passing (50/50 cumulative)

---

## Implementation Details

### New Module: `service.rs`

High-level service API for Forge integration:

```rust
pub struct CheckpointService {
    storage: ThreadSafeStorage,
    sessions: RwLock<HashMap<SessionId, SessionInfo>>,
    subscribers: Mutex<HashMap<SessionId, Vec<Sender<CheckpointEvent>>>>,
    annotations: RwLock<HashMap<CheckpointId, Vec<CheckpointAnnotation>>>,
    running: RwLock<bool>,
}
```

### Service API

```rust
// Create service
let service = Arc::new(CheckpointService::new(storage));

// Create session
let session = service.create_session("my-session")?;

// Create checkpoint
let id = service.checkpoint(&session, "Description")?;

// Subscribe to events
let receiver = service.subscribe(&session)?;

// Execute commands
let result = service.execute(CheckpointCommand::List { session_id: session })?;

// Get metrics
let metrics = service.metrics()?;
```

### Event System

Real-time checkpoint events:

```rust
pub enum CheckpointEvent {
    Created { checkpoint_id, session_id, timestamp },
    Restored { checkpoint_id, session_id },
    Deleted { checkpoint_id, session_id },
    Compacted { session_id, remaining },
}
```

### Command Pattern

API commands for remote operation:

```rust
pub enum CheckpointCommand {
    Create { session_id, message, tags },
    List { session_id },
    Restore { session_id, checkpoint_id },
    Delete { checkpoint_id },
    Compact { session_id, keep_recent },
}
```

### Auto-Checkpointing

Configurable automatic checkpoints:

```rust
pub struct AutoCheckpointConfig {
    pub interval_seconds: u64,
    pub on_error: bool,
    pub on_tool_call: bool,
}

service.enable_auto_checkpoint(&session, AutoCheckpointConfig {
    interval_seconds: 300,
    on_error: true,
    on_tool_call: false,
})?;
```

---

## Key Design Decisions

### 1. Session-Based Architecture

- Each debugging session has isolated checkpoints
- Sessions are identified by UUID
- Service manages multiple concurrent sessions

### 2. Event-Driven Notifications

- mpsc channels for event streaming
- Per-session subscriptions
- Best-effort delivery (no blocking)

### 3. Command Pattern for API

- All operations go through `CheckpointCommand`
- Consistent `CommandResult` types
- Easy to serialize for WebSocket/HTTP API

### 4. Annotations as Metadata

- Checkpoints can have notes/annotations
- Severity levels (Info, Warning, Critical)
- Stored separately from checkpoint data

---

## Code Metrics

| File | Lines | Change |
|------|-------|--------|
| `src/service.rs` | ~450 | New |
| `tests/integration_tests.rs` | ~230 | New |

---

## Complete API Example

```rust
use forge_reasoning::*;
use std::sync::Arc;

#[tokio::main]
async fn main() -> Result<()> {
    // Create service
    let storage = ThreadSafeStorage::open("checkpoints.db")?;
    let service = Arc::new(CheckpointService::new(storage));
    
    // Create session
    let session = service.create_session("debug-session")?;
    
    // Subscribe to events
    let mut receiver = service.subscribe(&session)?;
    tokio::spawn(async move {
        while let Ok(event) = receiver.recv() {
            println!("Event: {:?}", event);
        }
    });
    
    // Enable auto-checkpointing
    service.enable_auto_checkpoint(&session, AutoCheckpointConfig {
        interval_seconds: 300,
        on_error: true,
        on_tool_call: true,
    })?;
    
    // Manual checkpoint
    let id = service.checkpoint(&session, "Important milestone")?;
    
    // Annotate
    service.annotate(&id, CheckpointAnnotation {
        note: "This fixed the bug".to_string(),
        severity: AnnotationSeverity::Critical,
        timestamp: Utc::now(),
    })?;
    
    // List checkpoints
    let cps = service.list_checkpoints(&session)?;
    println!("Total: {}", cps.len());
    
    // Metrics
    let metrics = service.metrics()?;
    println!("Active sessions: {}", metrics.active_sessions);
    
    Ok(())
}
```

---

## Integration Points

### Forge Agent Loop

```rust
// In agent loop
if should_checkpoint(&state) {
    let id = service.checkpoint(&session, "Auto-checkpoint")?;
}

// On error
if let Err(e) = operation() {
    if config.on_error {
        service.trigger_auto_checkpoint(&session, AutoTrigger::CodeModified)?;
    }
}
```

### WebSocket API (Future)

```rust
// Client sends command
{
    "command": "Create",
    "session_id": "...",
    "message": "Checkpoint from API",
    "tags": ["api", "remote"]
}

// Server responds
{
    "result": "Created",
    "checkpoint_id": "..."
}

// Server streams events
{
    "event": "Created",
    "checkpoint_id": "...",
    "timestamp": "2026-02-19T..."
}
```

---

## Project Complete!

| Wave | Focus | Tests | Lines |
|------|-------|-------|-------|
| Wave 1 | Core creation | 10 | ~700 |
| Wave 2 | Query methods | 10 | ~1,000 |
| Wave 3 | Persistence | 10 | ~1,600 |
| Wave 4 | Thread safety | 10 | ~2,300 |
| Wave 5 | Integration | 10 | ~2,800 |
| **Total** | | **50** | **~2,800** |

---

## Running the Tests

```bash
cd /home/feanor/Projects/forge/forge-reasoning
cargo test

# Specific test suites
cargo test --test checkpoint_tests      # Waves 1-3
cargo test --test thread_safety_tests   # Wave 4
cargo test --test integration_tests     # Wave 5
```

---

**TDD Complete - Temporal Checkpointing MVP Ready for Production!** ✅