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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
//! Content-Based Modified Files Detection
//!
//! This module implements a Symbol-centric approach to detecting which files
//! were modified during mutation execution.
//!
//! # Design Philosophy
//!
//! ## Web Service Analogy
//!
//! Consider the difference between two web application designs:
//!
//! ```text
//! ❌ Anti-pattern (Raw HTML storage):
//! DB: { user_id: 1, html: "<div>Name: John</div>" }
//! Update: html.replace("John", "Jane")
//! Problem: Data and presentation are conflated
//!
//! ✅ Proper design (Entity + Template):
//! DB: { user_id: 1, name: "John" }
//! View: template.render(user)
//! Update: user.name = "Jane" → re-render
//! ```
//!
//! ## Application to Ryo
//!
//! The same principle applies to Ryo's architecture:
//!
//! ```text
//! ❌ FileSpan-based (current problematic approach):
//! SymbolRegistry: SymbolId → FileSpan (file position)
//! Problem: Symbol (data) mixed with File position (presentation)
//!
//! ✅ Content-based (this module's approach):
//! Entity: SymbolPath → PureItem (AST content)
//! View: SymbolPath → FilePath (deterministic derivation)
//! Update: PureItem change → regenerate affected files only
//! ```
//!
//! # Core Principles
//!
//! 1. **Symbol = Entity**: `SymbolId → PureItem` is the true data
//! 2. **File = View**: FilePath is deterministically derived from SymbolPath
//! 3. **FileSpan = Unnecessary**: A parsing artifact, meaningless post-mutation
//! 4. **Change Tracking = Symbol-level**: Track SymbolId via MutationEvent
//!
//! # SymbolPath → FilePath Derivation Rules
//!
//! ## Library Crates (lib.rs)
//!
//! ```text
//! SymbolPath FilePath
//! ────────────────────────────────────────────────────────
//! my_crate → src/lib.rs
//! my_crate::Item → src/lib.rs
//! my_crate::module::Item → src/module.rs
//! my_crate::module::sub::Item → src/module/sub.rs
//! my_crate::<impl Foo> → src/lib.rs
//! my_crate::module::<impl Foo> → src/module.rs
//! ```
//!
//! ## Binary Crates (main.rs) - Uses `main::` Prefix
//!
//! Binary symbols use the `main::` prefix to distinguish from library symbols:
//!
//! ```text
//! SymbolPath FilePath
//! ────────────────────────────────────────────────────────
//! main::my_app → src/main.rs
//! main::my_app::Item → src/main.rs
//! main::my_app::cli::Args → src/cli.rs
//! main::my_app::cli::cmd::Run → src/cli/cmd.rs
//! main::my_app::<impl Config> → src/main.rs
//! ```
//!
//! This distinction is critical for bin-only crates (no lib.rs) where all symbols
//! must resolve to main.rs and its sub-modules.
//!
//! ## Workspace Crates (crates/xxx/)
//!
//! ```text
//! SymbolPath FilePath
//! ────────────────────────────────────────────────────────
//! my_crate → crates/my-crate/src/lib.rs
//! my_crate::models::User → crates/my-crate/src/models.rs
//! main::my_app → crates/my-app/src/main.rs
//! main::my_app::cli → crates/my-app/src/cli.rs
//! ```
//!
//! # Data Flow
//!
//! ```text
//! execute_v2()
//! ↓
//! MutationEvent emitted
//! ├─ SymbolAdded { path }
//! ├─ SymbolModified { id }
//! └─ SymbolRemoved { path }
//! ↓
//! collect_modified_symbols(events, registry) → Vec<SymbolId>
//! ↓
//! symbols_to_files(symbols, registry, workspace_root)
//! → HashSet<WorkspaceFilePath> (derived, NOT from FileSpan)
//! ↓
//! FileDumper::dump_files(affected_files) ← only changed files
//! ↓
//! modified_files (accurate)
//! ```
//!
//! # Edge Cases
//!
//! ## Binary Entry (main.rs) and Bin-Only Crates
//!
//! ### The Problem
//!
//! In a mixed crate (both lib.rs and main.rs), how do we distinguish between:
//! - `my_crate::Config` in lib.rs
//! - `my_crate::Config` in main.rs
//!
//! ### The Solution: `main::` Prefix
//!
//! Binary symbols use the `main::` prefix in their SymbolPath:
//!
//! ```text
//! Library symbol: my_crate::Config → src/lib.rs
//! Binary symbol: main::my_crate::Config → src/main.rs
//! ```
//!
//! This prefix is applied during initial file loading by `SymbolPath::module_path_str()`:
//!
//! ```text
//! WorkspaceFilePath("src/lib.rs") → SymbolPath("my_crate")
//! WorkspaceFilePath("src/main.rs") → SymbolPath("main::my_crate")
//! ```
//!
//! ### Bin-Only Crates (No lib.rs)
//!
//! Bin-only crates have ONLY main.rs, no lib.rs. All symbols get the `main::` prefix:
//!
//! ```text
//! // File: src/main.rs
//! pub enum Status { Active, Inactive }
//! fn main() { ... }
//!
//! // SymbolPaths:
//! main::my_app (crate root)
//! main::my_app::Status (enum)
//! main::my_app::Status::Active (variant)
//! main::my_app::main (function)
//! ```
//!
//! The file resolution chain correctly handles this:
//! 1. `symbol_path_to_file()` extracts actual crate name: "my_app"
//! 2. Looks up CrateInfo from CargoMetadataProvider
//! 3. `resolve_candidates_with_crate_info()` checks entry_points
//! 4. Finds only Bin target (no Lib target) → returns ["src/main.rs"]
//!
//! ### Why This Works Without Adhoc Logic
//!
//! The system is fully declarative:
//! - **File → Symbol**: `module_path_str()` applies `main::` for main.rs
//! - **Symbol → File**: `resolve_candidates_with_crate_info()` uses CargoMetadataProvider
//! - **No path guessing**: All decisions based on Cargo.toml metadata
//!
//! No adhoc bin-only detection or lib.rs → main.rs path mapping is needed.
//!
//! ## New Symbols (AddItem)
//!
//! Problem: New symbols have no FileSpan
//! Solution: Derive FilePath from SymbolPath (FileSpan not needed)
//!
//! ```text
//! AddItem { target: "crate::models", content: "pub struct User {}" }
//! → SymbolPath: crate::models::User
//! → FilePath: src/models.rs (derived)
//! ```
//!
//! ## Symbol Move (RenameIdent across modules)
//!
//! Solution:
//! 1. Derive old_file from old_path → regenerate
//! 2. Derive new_file from new_path → regenerate
use crateMutationEvent;
use ;
/// Collect modified SymbolIds from mutation events.
///
/// This function extracts all symbol IDs that were affected by mutations,
/// which will be used to determine which files need to be regenerated.
///
/// # Arguments
///
/// * `events` - The mutation events emitted during execution
/// * `registry` - The symbol registry to look up paths
///
/// # Returns
///
/// A deduplicated vector of affected SymbolIds
// REMOVED: symbols_to_files() and symbol_path_to_file()
//
// These functions are replaced by RegistryGenerator.generate_affected()
// which determines file paths based on the generator's file layout strategy
// rather than inferring from SymbolPath structure.