State Lifecycle Architecture Lens
Cognitive Mode: Perspective (Quality Overlay)
Primary Question: "How is state corruption prevented?"
Focus: Field Contracts, Validation Gates, Resume Safety, State Mutation Control
When to Use
- Need to understand state management architecture
- Documenting field lifecycle contracts
- Analyzing resume and checkpoint safety
- User invokes
/autoskillit:arch-lens-state-lifecycle or /autoskillit:make-arch-diag state
Critical Constraints
NEVER:
- Modify any source code files
- Show business logic details
- Focus on data content (focus on mutation rules)
ALWAYS:
- Focus on STATE MUTATION RULES
- Show field lifecycle categories
- Document validation gates
- Include resume detection strategy
- BEFORE creating any diagram, LOAD the
/autoskillit:mermaid skill using the Skill tool - this is MANDATORY
Analysis Workflow
Step 1: Launch Parallel Exploration Subagents
Spawn Explore subagents to investigate:
State Schema
- Find state/context definitions
- Identify typed state fields
- Look for: State classes, Context objects, state schemas, typed dictionaries
Field Categories
- Find field mutation patterns
- Identify immutable vs mutable fields
- Look for: immutable fields, readonly, lifecycle annotations, const fields
Validation Gates
- Find state validation code
- Identify gate patterns
- Look for: validate_*, gate, check_*, guard, assert, state validators
Resume Detection
- Find resume/checkpoint code
- Identify resume detection strategy
- Look for: resume, checkpoint, restore, detect state, load checkpoint
State Updates
- Find state mutation code
- Identify update patterns
- Look for: update methods, setState, mutation functions, state setters
Contract Enforcement
- Find contract validation
- Identify violation detection
- Look for: contract checking, violation detection, enforcement mechanisms
Step 2: Categorize Fields
| Category |
Description |
Fields |
| INIT_ONLY |
Set once, never modify |
{fields} |
| INIT_PRESERVE |
Keep on resume |
{fields} |
| MUTABLE |
Can change freely |
{fields} |
| APPEND_ONLY |
Can only grow |
{fields} |
| DERIVED |
Computed, not stored |
{fields} |
CRITICAL - Analyze Read/Write Direction:
For EVERY state field and storage location:
- Read patterns: Who READS this field? When?
- Write patterns: Who WRITES this field? When?
- Read-after-write: Is the written value ever READ back by the system?
Distinguish clearly:
- State fields (read/write): System both writes AND reads back for decisions
- Checkpoint storage (read/write): Written during execution, read on resume
- Audit logs (write-only): System writes but never reads back for logic
- Debug artifacts (write-only): Written for humans, not read by system
Step 3: Map Validation Flow
Document:
- Gate order (which runs first)
- Failure modes
- Resume vs fresh start differences
Step 4: Create the Diagram
Use flowchart with:
Direction: TB for contract enforcement flow
Subgraphs:
- Lifecycles (field categories)
- Validation Gates
- State Wrapper (mutation mechanism)
- Resume Detection
- Phase Jump Routing
Node Styling:
detector class: INIT_ONLY fields (red - critical)
gap class: INIT_PRESERVE fields (yellow - warning)
phase class: MUTABLE fields (purple)
handler class: APPEND_ONLY fields (orange)
stateNode class: Validation gates
output class: State wrapper/accessor
cli class: Resume detection tiers
Step 5: Write Output
Write the diagram to: temp/arch-lens-state-lifecycle/arch_diag_state_lifecycle_{YYYY-MM-DD_HHMMSS}.md (relative to the current working directory)
After writing the diagram file, emit a structured output line:
diagram_path = {absolute_path_to_diagram_file}
Output Template
# State Lifecycle Diagram: {System Name}
**Lens:** State Lifecycle (Contract Overlay)
**Question:** How is state corruption prevented?
**Date:** {YYYY-MM-DD}
**Scope:** {What was analyzed}
## Field Lifecycle Categories
| Category | Description | Example Fields |
|----------|-------------|----------------|
| INIT_ONLY | Never modify after init | {fields} |
| INIT_PRESERVE | Keep on resume | {fields} |
| MUTABLE | Free to change | {fields} |
| APPEND_ONLY | Can only grow | {fields} |
## State Lifecycle Diagram
```mermaid
%%{init: {'flowchart': {'nodeSpacing': 50, 'rankSpacing': 60, 'curve': 'basis'}}}%%
flowchart TB
%% CLASS DEFINITIONS %%
classDef cli fill:#1a237e,stroke:#7986cb,stroke-width:2px,color:#fff;
classDef stateNode fill:#004d40,stroke:#4db6ac,stroke-width:2px,color:#fff;
classDef handler fill:#e65100,stroke:#ffb74d,stroke-width:2px,color:#fff;
classDef phase fill:#6a1b9a,stroke:#ba68c8,stroke-width:2px,color:#fff;
classDef detector fill:#b71c1c,stroke:#ef5350,stroke-width:2px,color:#fff;
classDef output fill:#00695c,stroke:#4db6ac,stroke-width:2px,color:#fff;
classDef gap fill:#ff6f00,stroke:#ffa726,stroke-width:2px,color:#000;
subgraph Lifecycles ["FIELD LIFECYCLE CATEGORIES"]
direction TB
INIT_ONLY["INIT_ONLY<br/>━━━━━━━━━━<br/>id, request_id<br/>NEVER modify"]
INIT_PRESERVE["INIT_PRESERVE<br/>━━━━━━━━━━<br/>is_resuming<br/>Keep on resume"]
MUTABLE["MUTABLE<br/>━━━━━━━━━━<br/>current_state<br/>Freely change"]
APPEND_ONLY["APPEND_ONLY<br/>━━━━━━━━━━<br/>errors, history<br/>Only grow"]
end
subgraph Gates ["VALIDATION GATES"]
direction TB
GATE1["validate_required<br/>━━━━━━━━━━<br/>FAIL-FAST"]
GATE2["validate_lifecycle<br/>━━━━━━━━━━<br/>Contract check"]
GATE3["resume_safety<br/>━━━━━━━━━━<br/>Preserve check"]
end
subgraph Wrapper ["STATE WRAPPER"]
direction TB
ACCESSOR["StateAccessor<br/>━━━━━━━━━━<br/>Tracks mutations"]
MERGE["Merge Updates<br/>━━━━━━━━━━<br/>Auto-include"]
end
subgraph Resume ["RESUME DETECTION"]
direction TB
TIER1["Tier 1: Explicit<br/>━━━━━━━━━━<br/>flag=true"]
TIER2["Tier 2: Heuristic<br/>━━━━━━━━━━<br/>State exists"]
TIER3["Tier 3: Fresh<br/>━━━━━━━━━━<br/>No indicators"]
end
%% FLOW %%
INIT_ONLY --> GATE1
INIT_PRESERVE --> GATE2
MUTABLE --> GATE2
APPEND_ONLY --> GATE2
GATE1 --> GATE2
GATE2 --> GATE3
GATE3 --> ACCESSOR
ACCESSOR --> MERGE
MERGE --> TIER1
TIER1 --> TIER2
TIER2 --> TIER3
%% CLASS ASSIGNMENTS %%
class INIT_ONLY detector;
class INIT_PRESERVE gap;
class MUTABLE phase;
class APPEND_ONLY handler;
class GATE1,GATE2,GATE3 stateNode;
class ACCESSOR,MERGE output;
class TIER1,TIER2,TIER3 cli;
Color Legend:
| Color |
Category |
Description |
| Red |
INIT_ONLY |
Never modify (critical) |
| Yellow |
INIT_PRESERVE |
Preserved on resume |
| Purple |
MUTABLE |
Freely modifiable |
| Orange |
APPEND_ONLY |
Can only grow |
| Teal |
Gates |
Validation gates |
| Dark Teal |
Wrapper |
State mutation mechanism |
| Dark Blue |
Detection |
Resume detection tiers |
State Lifecycle Contract Rules
| Lifecycle |
Fresh Start |
Resume |
Violation Detection |
| INIT_ONLY |
Cannot modify |
Cannot modify |
{detection} |
| INIT_PRESERVE |
Can modify |
Cannot modify |
{detection} |
| MUTABLE |
Can modify |
Can modify |
Never fails |
| APPEND_ONLY |
Can append |
Can append |
{detection} |
Resume Detection Strategy
| Tier |
Check |
Result |
| 1 |
Explicit flag |
{what happens} |
| 2 |
Heuristic |
{what happens} |
| 3 |
Fresh start |
{what happens} |
---
## Pre-Diagram Checklist
Before creating the diagram, verify:
- [ ] LOADED `/autoskillit:mermaid` skill using the Skill tool
- [ ] Using ONLY classDef styles from the mermaid skill (no invented colors)
- [ ] Diagram will include a color legend table
---
## Related Skills
- `/autoskillit:make-arch-diag` - Parent skill for lens selection
- `/autoskillit:mermaid` - MUST BE LOADED before creating diagram
- `/autoskillit:arch-lens-process-flow` - For state machine view
- `/autoskillit:arch-lens-error-resilience` - For validation failure handling
1---2name: arch-lens-state-lifecycle3description: Create State Lifecycle architecture diagram showing field contracts, validation gates, and resume safety. Contract overlay lens answering "How is state corruption prevented?"4---5
6# State Lifecycle Architecture Lens
7
8**Cognitive Mode:** Perspective (Quality Overlay)
9**Primary Question:** "How is state corruption prevented?"
10**Focus:** Field Contracts, Validation Gates, Resume Safety, State Mutation Control
11
12## When to Use
13
14- Need to understand state management architecture
15- Documenting field lifecycle contracts
16- Analyzing resume and checkpoint safety
17- User invokes `/autoskillit:arch-lens-state-lifecycle` or `/autoskillit:make-arch-diag state`
18
19## Critical Constraints
20
21**NEVER:**
22- Modify any source code files
23- Show business logic details
24- Focus on data content (focus on mutation rules)
25
26**ALWAYS:**
27- Focus on STATE MUTATION RULES
28- Show field lifecycle categories
29- Document validation gates
30- Include resume detection strategy
31- BEFORE creating any diagram, LOAD the `/autoskillit:mermaid` skill using the Skill tool - this is MANDATORY
32
33---
34
35## Analysis Workflow
36
37### Step 1: Launch Parallel Exploration Subagents
38
39Spawn Explore subagents to investigate:
40
41**State Schema**
42- Find state/context definitions
43- Identify typed state fields
44- Look for: State classes, Context objects, state schemas, typed dictionaries
45
46**Field Categories**
47- Find field mutation patterns
48- Identify immutable vs mutable fields
49- Look for: immutable fields, readonly, lifecycle annotations, const fields
50
51**Validation Gates**
52- Find state validation code
53- Identify gate patterns
54- Look for: validate_*, gate, check_*, guard, assert, state validators
55
56**Resume Detection**
57- Find resume/checkpoint code
58- Identify resume detection strategy
59- Look for: resume, checkpoint, restore, detect state, load checkpoint
60
61**State Updates**
62- Find state mutation code
63- Identify update patterns
64- Look for: update methods, setState, mutation functions, state setters
65
66**Contract Enforcement**
67- Find contract validation
68- Identify violation detection
69- Look for: contract checking, violation detection, enforcement mechanisms
70
71### Step 2: Categorize Fields
72
73| Category | Description | Fields |
74|----------|-------------|--------|
75| INIT_ONLY | Set once, never modify | {fields} |
76| INIT_PRESERVE | Keep on resume | {fields} |
77| MUTABLE | Can change freely | {fields} |
78| APPEND_ONLY | Can only grow | {fields} |
79| DERIVED | Computed, not stored | {fields} |
80
81**CRITICAL - Analyze Read/Write Direction:**
82For EVERY state field and storage location:
83- **Read patterns**: Who READS this field? When?
84- **Write patterns**: Who WRITES this field? When?
85- **Read-after-write**: Is the written value ever READ back by the system?
86
87Distinguish clearly:
88- **State fields (read/write)**: System both writes AND reads back for decisions
89- **Checkpoint storage (read/write)**: Written during execution, read on resume
90- **Audit logs (write-only)**: System writes but never reads back for logic
91- **Debug artifacts (write-only)**: Written for humans, not read by system
92
93### Step 3: Map Validation Flow
94
95Document:
96- Gate order (which runs first)
97- Failure modes
98- Resume vs fresh start differences
99
100### Step 4: Create the Diagram
101
102Use flowchart with:
103
104**Direction:** `TB` for contract enforcement flow
105
106**Subgraphs:**
107- Lifecycles (field categories)
108- Validation Gates
109- State Wrapper (mutation mechanism)
110- Resume Detection
111- Phase Jump Routing
112
113**Node Styling:**
114- `detector` class: INIT_ONLY fields (red - critical)
115- `gap` class: INIT_PRESERVE fields (yellow - warning)
116- `phase` class: MUTABLE fields (purple)
117- `handler` class: APPEND_ONLY fields (orange)
118- `stateNode` class: Validation gates
119- `output` class: State wrapper/accessor
120- `cli` class: Resume detection tiers
121
122### Step 5: Write Output
123
124Write the diagram to: `temp/arch-lens-state-lifecycle/arch_diag_state_lifecycle_{YYYY-MM-DD_HHMMSS}.md` (relative to the current working directory)
125
126After writing the diagram file, emit a structured output line:
127
128```
129diagram_path = {absolute_path_to_diagram_file}
130```
131
132---
133
134## Output Template
135
136```markdown
137# State Lifecycle Diagram: {System Name}
138
139**Lens:** State Lifecycle (Contract Overlay)
140**Question:** How is state corruption prevented?
141**Date:** {YYYY-MM-DD}
142**Scope:** {What was analyzed}
143
144## Field Lifecycle Categories
145
146| Category | Description | Example Fields |
147|----------|-------------|----------------|
148| INIT_ONLY | Never modify after init | {fields} |
149| INIT_PRESERVE | Keep on resume | {fields} |
150| MUTABLE | Free to change | {fields} |
151| APPEND_ONLY | Can only grow | {fields} |
152
153## State Lifecycle Diagram
154
155```mermaid
156%%{init: {'flowchart': {'nodeSpacing': 50, 'rankSpacing': 60, 'curve': 'basis'}}}%%
157flowchart TB
158 %% CLASS DEFINITIONS %%
159 classDef cli fill:#1a237e,stroke:#7986cb,stroke-width:2px,color:#fff;
160 classDef stateNode fill:#004d40,stroke:#4db6ac,stroke-width:2px,color:#fff;
161 classDef handler fill:#e65100,stroke:#ffb74d,stroke-width:2px,color:#fff;
162 classDef phase fill:#6a1b9a,stroke:#ba68c8,stroke-width:2px,color:#fff;
163 classDef detector fill:#b71c1c,stroke:#ef5350,stroke-width:2px,color:#fff;
164 classDef output fill:#00695c,stroke:#4db6ac,stroke-width:2px,color:#fff;
165 classDef gap fill:#ff6f00,stroke:#ffa726,stroke-width:2px,color:#000;
166
167 subgraph Lifecycles ["FIELD LIFECYCLE CATEGORIES"]
168 direction TB
169 INIT_ONLY["INIT_ONLY<br/>━━━━━━━━━━<br/>id, request_id<br/>NEVER modify"]
170 INIT_PRESERVE["INIT_PRESERVE<br/>━━━━━━━━━━<br/>is_resuming<br/>Keep on resume"]
171 MUTABLE["MUTABLE<br/>━━━━━━━━━━<br/>current_state<br/>Freely change"]
172 APPEND_ONLY["APPEND_ONLY<br/>━━━━━━━━━━<br/>errors, history<br/>Only grow"]
173 end
174
175 subgraph Gates ["VALIDATION GATES"]
176 direction TB
177 GATE1["validate_required<br/>━━━━━━━━━━<br/>FAIL-FAST"]
178 GATE2["validate_lifecycle<br/>━━━━━━━━━━<br/>Contract check"]
179 GATE3["resume_safety<br/>━━━━━━━━━━<br/>Preserve check"]
180 end
181
182 subgraph Wrapper ["STATE WRAPPER"]
183 direction TB
184 ACCESSOR["StateAccessor<br/>━━━━━━━━━━<br/>Tracks mutations"]
185 MERGE["Merge Updates<br/>━━━━━━━━━━<br/>Auto-include"]
186 end
187
188 subgraph Resume ["RESUME DETECTION"]
189 direction TB
190 TIER1["Tier 1: Explicit<br/>━━━━━━━━━━<br/>flag=true"]
191 TIER2["Tier 2: Heuristic<br/>━━━━━━━━━━<br/>State exists"]
192 TIER3["Tier 3: Fresh<br/>━━━━━━━━━━<br/>No indicators"]
193 end
194
195 %% FLOW %%
196 INIT_ONLY --> GATE1
197 INIT_PRESERVE --> GATE2
198 MUTABLE --> GATE2
199 APPEND_ONLY --> GATE2
200
201 GATE1 --> GATE2
202 GATE2 --> GATE3
203 GATE3 --> ACCESSOR
204 ACCESSOR --> MERGE
205
206 MERGE --> TIER1
207 TIER1 --> TIER2
208 TIER2 --> TIER3
209
210 %% CLASS ASSIGNMENTS %%
211 class INIT_ONLY detector;
212 class INIT_PRESERVE gap;
213 class MUTABLE phase;
214 class APPEND_ONLY handler;
215 class GATE1,GATE2,GATE3 stateNode;
216 class ACCESSOR,MERGE output;
217 class TIER1,TIER2,TIER3 cli;
218```
219
220**Color Legend:**
221| Color | Category | Description |
222|-------|----------|-------------|
223| Red | INIT_ONLY | Never modify (critical) |
224| Yellow | INIT_PRESERVE | Preserved on resume |
225| Purple | MUTABLE | Freely modifiable |
226| Orange | APPEND_ONLY | Can only grow |
227| Teal | Gates | Validation gates |
228| Dark Teal | Wrapper | State mutation mechanism |
229| Dark Blue | Detection | Resume detection tiers |
230
231## State Lifecycle Contract Rules
232
233| Lifecycle | Fresh Start | Resume | Violation Detection |
234|-----------|-------------|--------|---------------------|
235| INIT_ONLY | Cannot modify | Cannot modify | {detection} |
236| INIT_PRESERVE | Can modify | Cannot modify | {detection} |
237| MUTABLE | Can modify | Can modify | Never fails |
238| APPEND_ONLY | Can append | Can append | {detection} |
239
240## Resume Detection Strategy
241
242| Tier | Check | Result |
243|------|-------|--------|
244| 1 | Explicit flag | {what happens} |
245| 2 | Heuristic | {what happens} |
246| 3 | Fresh start | {what happens} |
247```
248
249---
250
251## Pre-Diagram Checklist
252
253Before creating the diagram, verify:
254
255- [ ] LOADED `/autoskillit:mermaid` skill using the Skill tool
256- [ ] Using ONLY classDef styles from the mermaid skill (no invented colors)
257- [ ] Diagram will include a color legend table
258
259---
260
261## Related Skills
262
263- `/autoskillit:make-arch-diag` - Parent skill for lens selection
264- `/autoskillit:mermaid` - MUST BE LOADED before creating diagram
265- `/autoskillit:arch-lens-process-flow` - For state machine view
266- `/autoskillit:arch-lens-error-resilience` - For validation failure handling