# Feature: Chunk State Persistence

## Name
Chunk State Persistence

## Description
Persistence operations for saving and loading chunk state via F03 Flight Database. Enables system recovery after restarts and maintains chunk state across processing sessions. Serializes internal _chunks dictionary to persistent storage.

## Component APIs Implemented

### `save_chunk_state(flight_id: str) -> bool`
Persists all chunk state for a flight.
- Serializes ChunkHandles for specified flight
- Calls F03.save_chunk_state()
- Returns success status

### `load_chunk_state(flight_id: str) -> bool`
Loads chunk state for a flight from persistent storage.
- Calls F03.load_chunk_states()
- Deserializes and populates internal _chunks dictionary
- Returns success status

## External Tools and Services
- **F03 Flight Database**: `save_chunk_state()`, `load_chunk_states()`

## Internal Methods

### `_serialize_chunks(flight_id: str) -> Dict`
Converts ChunkHandles to serializable dictionary format.

### `_deserialize_chunks(data: Dict) -> Dict[str, ChunkHandle]`
Reconstructs ChunkHandles from serialized data.

### `_filter_chunks_by_flight(flight_id: str) -> List[ChunkHandle]`
Filters internal _chunks dictionary by flight_id.

### `_merge_loaded_chunks(chunks: Dict[str, ChunkHandle])`
Merges loaded chunks into internal state without overwriting existing.

## Unit Tests

### Test: save_chunk_state_calls_f03
- Create chunks for flight_A
- Call save_chunk_state(flight_A)
- Assert F03.save_chunk_state() called with serialized data

### Test: save_chunk_state_filters_by_flight
- Create chunks for flight_A and flight_B
- Call save_chunk_state(flight_A)
- Assert only flight_A chunks serialized

### Test: save_chunk_state_success
- Create chunks
- Mock F03.save_chunk_state() returns success
- Assert save_chunk_state() returns True

### Test: save_chunk_state_failure
- Create chunks
- Mock F03.save_chunk_state() returns failure
- Assert save_chunk_state() returns False

### Test: load_chunk_state_populates_internal_dict
- Mock F03.load_chunk_states() returns chunk data
- Call load_chunk_state(flight_A)
- Assert internal _chunks dictionary populated

### Test: load_chunk_state_success
- Mock F03.load_chunk_states() returns success
- Assert load_chunk_state() returns True

### Test: load_chunk_state_failure
- Mock F03.load_chunk_states() returns failure
- Assert load_chunk_state() returns False

### Test: load_chunk_state_empty_flight
- Mock F03.load_chunk_states() returns empty
- Call load_chunk_state(flight_A)
- Assert returns True (no error)
- Assert no chunks loaded

### Test: serialize_preserves_all_fields
- Create chunk with all fields populated (anchor, frames, status)
- Serialize and deserialize
- Assert all fields match original

### Test: deserialize_handles_missing_optional_fields
- Create serialized data with optional fields missing
- Deserialize
- Assert ChunkHandle created with default values

## Integration Tests

### Test: save_load_roundtrip
1. Create chunks with various states (active, anchored, merged)
2. save_chunk_state()
3. Clear internal state
4. load_chunk_state()
5. Verify all chunks restored with correct state

### Test: persistence_across_restart
1. Create flight with chunks
2. Process frames, anchor some chunks
3. save_chunk_state()
4. Simulate restart (new instance)
5. load_chunk_state()
6. Verify can continue processing

### Test: partial_state_recovery
1. Create chunks for multiple flights
2. save_chunk_state(flight_A)
3. Clear internal state
4. load_chunk_state(flight_A)
5. Verify only flight_A chunks loaded

