Data Substrate Analysis
Analyzes the fundamental units of data and state management patterns.
Process
- Locate type files — Find types.py, schema.py, models.py, state.py
- Classify typing — Strict (Pydantic), structural (TypedDict), loose (dict)
- Analyze mutation — In-place modification vs. copy-on-write
- Document serialization — json(), dict(), pickle, custom methods
Typing Strategy Classification
Detection Patterns
| Strategy |
Indicators |
Files to Check |
| Pydantic |
BaseModel, Field(), validator |
models.py, schema.py |
| Dataclass |
@dataclass, field() |
types.py, models.py |
| TypedDict |
TypedDict, Required[], NotRequired[] |
types.py |
| NamedTuple |
NamedTuple, typing.NamedTuple |
types.py |
| Loose |
Dict[str, Any], plain dict |
Throughout |
Analysis Questions
- Are boundaries validated (API ingress/egress)?
- Is nesting depth reasonable (<3 levels)?
- Are optional fields explicit or implicit None?
- Version migration path (Pydantic V1 → V2)?
Immutability Analysis
Mutable Patterns (Risk Indicators)
# In-place list modification
state.messages.append(msg)
state.history.extend(new_items)
# Direct dict mutation
state['key'] = value
state.update(new_data)
# Object attribute mutation
state.status = 'complete'
Immutable Patterns (Safer)
# Pydantic copy
new_state = state.model_copy(update={'key': value})
# Dataclass replace
new_state = replace(state, messages=[*state.messages, msg])
# Spread operator style
new_state = {**state, 'key': value}
# Frozen dataclass
@dataclass(frozen=True)
class State: ...
Serialization Strategy
Common Patterns
| Method |
Code Pattern |
Trade-offs |
| Pydantic JSON |
.model_dump_json() |
Type-safe, automatic |
| Pydantic Dict |
.model_dump() |
For internal use |
| Dataclass |
asdict(obj) |
Manual, no validation |
| Custom |
to_dict(), from_dict() |
Full control |
| Pickle |
pickle.dumps() |
Fast, fragile, security risk |
| JSON |
json.dumps(obj, default=...) |
Requires encoder |
Questions to Answer
- Is serialization implicit (automatic) or explicit (manual)?
- How are nested objects handled?
- Is deserialization validated?
- What happens with unknown fields?
Output Template
## Data Substrate Analysis: [Framework Name]
### Typing Strategy
- **Primary Approach**: [Pydantic/Dataclass/TypedDict/Loose]
- **Key Files**: [List of files]
- **Nesting Depth**: [Shallow/Medium/Deep]
- **Validation**: [At boundaries/Everywhere/None]
### Core Primitives
| Type | Location | Purpose | Mutability |
|------|----------|---------|------------|
| Message | schema.py:L15 | Chat message | Immutable |
| State | state.py:L42 | Agent state | Mutable ⚠️ |
| Result | types.py:L78 | Tool output | Immutable |
### Mutation Analysis
- **Pattern**: [In-place/Copy-on-write/Mixed]
- **Risk Areas**: [List of mutable state locations]
- **Concurrency Safe**: [Yes/No/Partial]
### Serialization
- **Method**: [Pydantic/Custom/JSON]
- **Implicit/Explicit**: [Description]
- **Round-trip Tested**: [Yes/No/Unknown]
Integration
- Prerequisite:
codebase-mapping to identify type files
- Feeds into:
comparative-matrix for typing decisions
- Related:
resilience-analysis for error handling in serialization
1---2name: data-substrate-analysis3description: Analyze fundamental data primitives, type systems, and state management patterns in a codebase. Use when (1) evaluating typing strategies (Pydantic vs TypedDict vs loose dicts), (2) assessing immutability and mutation patterns, (3) understanding serialization approaches, (4) documenting state shape and lifecycle, or (5) comparing data modeling approaches across frameworks.4---56# Data Substrate Analysis78Analyzes the fundamental units of data and state management patterns.910## Process11121. **Locate type files** — Find types.py, schema.py, models.py, state.py132. **Classify typing** — Strict (Pydantic), structural (TypedDict), loose (dict)143. **Analyze mutation** — In-place modification vs. copy-on-write154. **Document serialization** — json(), dict(), pickle, custom methods1617## Typing Strategy Classification1819### Detection Patterns2021| Strategy | Indicators | Files to Check |22|----------|-----------|----------------|23| **Pydantic** | `BaseModel`, `Field()`, `validator` | models.py, schema.py |24| **Dataclass** | `@dataclass`, `field()` | types.py, models.py |25| **TypedDict** | `TypedDict`, `Required[]`, `NotRequired[]` | types.py |26| **NamedTuple** | `NamedTuple`, `typing.NamedTuple` | types.py |27| **Loose** | `Dict[str, Any]`, plain `dict` | Throughout |2829### Analysis Questions3031- Are boundaries validated (API ingress/egress)?32- Is nesting depth reasonable (<3 levels)?33- Are optional fields explicit or implicit None?34- Version migration path (Pydantic V1 → V2)?3536## Immutability Analysis3738### Mutable Patterns (Risk Indicators)3940```python41# In-place list modification42state.messages.append(msg)43state.history.extend(new_items)4445# Direct dict mutation46state['key'] = value47state.update(new_data)4849# Object attribute mutation50state.status = 'complete'51```5253### Immutable Patterns (Safer)5455```python56# Pydantic copy57new_state = state.model_copy(update={'key': value})5859# Dataclass replace60new_state = replace(state, messages=[*state.messages, msg])6162# Spread operator style63new_state = {**state, 'key': value}6465# Frozen dataclass66@dataclass(frozen=True)67class State: ...68```6970## Serialization Strategy7172### Common Patterns7374| Method | Code Pattern | Trade-offs |75|--------|-------------|------------|76| Pydantic JSON | `.model_dump_json()` | Type-safe, automatic |77| Pydantic Dict | `.model_dump()` | For internal use |78| Dataclass | `asdict(obj)` | Manual, no validation |79| Custom | `to_dict()`, `from_dict()` | Full control |80| Pickle | `pickle.dumps()` | Fast, fragile, security risk |81| JSON | `json.dumps(obj, default=...)` | Requires encoder |8283### Questions to Answer8485- Is serialization implicit (automatic) or explicit (manual)?86- How are nested objects handled?87- Is deserialization validated?88- What happens with unknown fields?8990## Output Template9192```markdown93## Data Substrate Analysis: [Framework Name]9495### Typing Strategy96- **Primary Approach**: [Pydantic/Dataclass/TypedDict/Loose]97- **Key Files**: [List of files]98- **Nesting Depth**: [Shallow/Medium/Deep]99- **Validation**: [At boundaries/Everywhere/None]100101### Core Primitives102103| Type | Location | Purpose | Mutability |104|------|----------|---------|------------|105| Message | schema.py:L15 | Chat message | Immutable |106| State | state.py:L42 | Agent state | Mutable ⚠️ |107| Result | types.py:L78 | Tool output | Immutable |108109### Mutation Analysis110- **Pattern**: [In-place/Copy-on-write/Mixed]111- **Risk Areas**: [List of mutable state locations]112- **Concurrency Safe**: [Yes/No/Partial]113114### Serialization115- **Method**: [Pydantic/Custom/JSON]116- **Implicit/Explicit**: [Description]117- **Round-trip Tested**: [Yes/No/Unknown]118```119120## Integration121122- **Prerequisite**: `codebase-mapping` to identify type files123- **Feeds into**: `comparative-matrix` for typing decisions124- **Related**: `resilience-analysis` for error handling in serialization