1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//! Shared state for all command handlers
//!
//! Provides thread-local storage for the current archive that is shared
//! across all handler modules (archive, file, etc.)
//!
//! # ⚠️ TEMPORARY WORKAROUND IMPLEMENTATION
//!
//! **Status:** This is the ACTUAL state management implementation currently in use.
//!
//! **Why This Exists:** `unilang::ExecutionContext` does not yet support passing custom
//! state to command handlers (see TODOs in main.rs:42, repl.rs:79). This thread-local
//! storage provides a workaround to share archive state across handler function calls.
//!
//! **Architectural Intent:** The proper design uses `state::ArchiveState` (`Arc<RwLock<>>`)
//! passed through `ExecutionContext`. See state.rs module docs for full explanation.
//!
//! **Trade-offs of Thread-Local Approach:**
//! - ✅ Works with current unilang API constraints
//! - ✅ Simple, no lifetime/borrow complexity
//! - ⚠️ Thread-isolated (each thread has separate state)
//! - ⚠️ Global mutable state (less explicit than passing through context)
//! - ⚠️ Architectural debt vs. specification design
//!
//! **Migration Path:** When `ExecutionContext` gains state support, migrate handlers to
//! use `state::ArchiveState` and delete this module entirely.
//!
//! ## Architectural Validation (2025-01-25)
//!
//! Comprehensive exploration of unilang codebase confirms this approach is correct:
//!
//! - **`ExecutionContext` Status**: Currently empty placeholder struct with no fields
//! - **Unilang Philosophy**: Explicitly stateless by design per pipeline.rs docs
//! - **Thread-Local Blessing**: genfile's main.rs:42 and repl.rs:79 TODOs acknowledge
//! this is the intended workaround until `ExecutionContext` API evolves
//! - **Use Case Fit**: Single-threaded REPL doesn't require cross-thread state sharing
//!
//! **Conclusion**: This thread-local pattern is the recommended approach given current
//! unilang API constraints. The dual state system (state.rs unused + `shared_state.rs`
//! active) represents well-managed architectural debt with clear migration path.
use RefCell;
use TemplateArchive;
thread_local!