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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
from omegaconf import OmegaConf, DictConfig, ListConfig
from typing import Set, Any, Optional, Union
import json
from pathlib import Path


class AccessTrackedConfig:
    """
    Wrapper for OmegaConf to track accessed parameters.
    Only saves configuration items that were actually accessed during execution.
    """
    
    _original_cfg_snapshot: Optional[OmegaConf] = None
    
    def __init__(self, cfg: Union[DictConfig, ListConfig], parent: 'AccessTrackedConfig' = None, key_path: str = ""):
        object.__setattr__(self, '_cfg', cfg)
        object.__setattr__(self, '_parent', parent)
        object.__setattr__(self, '_key_path', key_path)
        object.__setattr__(self, '_local_accessed', set())
        object.__setattr__(self, '_children', {})
        
        if parent is None:
            AccessTrackedConfig._original_cfg_snapshot = OmegaConf.create(
                OmegaConf.to_container(cfg, resolve=True)
            )
    
    def _is_list_config(self) -> bool:
        """Check if underlying config is a ListConfig"""
        return isinstance(self._cfg, ListConfig)
    
    def _is_dict_config(self) -> bool:
        """Check if underlying config is a DictConfig"""
        return isinstance(self._cfg, DictConfig)
    
    def __getattr__(self, name: str) -> Any:
        if name.startswith('_'):
            return object.__getattribute__(self, name)
        
        self._local_accessed.add(name)
        # Use safe access: for hasattr() semantics, raise AttributeError on missing keys
        try:
            value = self._cfg[name]
        except Exception:
            raise AttributeError(f"Config has no attribute '{name}'")
        
        if OmegaConf.is_config(value):
            new_path = f"{self._key_path}.{name}" if self._key_path else name
            if name not in self._children:
                self._children[name] = AccessTrackedConfig(value, parent=self, key_path=new_path)
            return self._children[name]
        
        return value
    
    def __getitem__(self, key) -> Any:
        """Support both dict-style and list-style access"""
        if isinstance(key, int):
            # List-style access
            self._local_accessed.add(f"[{key}]")
            value = self._cfg[key]
            if OmegaConf.is_config(value):
                new_path = f"{self._key_path}[{key}]" if self._key_path else f"[{key}]"
                cache_key = f"[{key}]"
                if cache_key not in self._children:
                    self._children[cache_key] = AccessTrackedConfig(value, parent=self, key_path=new_path)
                return self._children[cache_key]
            return value
        else:
            # Dict-style access
            return self.__getattr__(key)
    
    def __setattr__(self, name: str, value: Any):
        if name.startswith('_'):
            object.__setattr__(self, name, value)
        else:
            self._local_accessed.add(name)
            self._cfg[name] = value
            # Invalidate child cache if exists
            if name in self._children:
                del self._children[name]

    def __setitem__(self, key, value: Any):
        """Support both dict-style and list-style setting"""
        if isinstance(key, int):
            self._local_accessed.add(f"[{key}]")
            self._cfg[key] = value
            cache_key = f"[{key}]"
            if cache_key in self._children:
                del self._children[cache_key]
        else:
            self._local_accessed.add(key)
            self._cfg[key] = value
            if key in self._children:
                del self._children[key]
    
    def __contains__(self, key) -> bool:
        """Support 'in' operator - tracks the key check as an access"""
        if isinstance(key, int):
            self._local_accessed.add(f"[{key}]")
        else:
            self._local_accessed.add(key)
        return key in self._cfg
    
    def __len__(self) -> int:
        """Return number of keys/items"""
        return len(self._cfg)
    
    def __iter__(self):
        """Support iteration for both DictConfig and ListConfig"""
        if self._is_list_config():
            # For ListConfig, iterate over indices and return values
            for i in range(len(self._cfg)):
                self._local_accessed.add(f"[{i}]")
            return iter(self._cfg)
        else:
            # For DictConfig, iterate over keys
            for key in self._cfg.keys():
                self._local_accessed.add(key)
            return iter(self._cfg)
    
    def __repr__(self) -> str:
        """String representation"""
        if self._is_list_config():
            return f"AccessTrackedConfig({self._key_path or 'root'}, list_len={len(self._cfg)})"
        return f"AccessTrackedConfig({self._key_path or 'root'}, keys={list(self._cfg.keys())})"
    
    def __str__(self) -> str:
        """String representation"""
        return OmegaConf.to_yaml(self._cfg)
    
    def __bool__(self) -> bool:
        """Boolean evaluation - True if config has any keys/items"""
        return len(self._cfg) > 0
    
    def __eq__(self, other) -> bool:
        """Equality comparison"""
        if isinstance(other, AccessTrackedConfig):
            return self._cfg == other._cfg
        elif OmegaConf.is_config(other):
            return self._cfg == other
        elif isinstance(other, (dict, list)):
            return OmegaConf.to_container(self._cfg, resolve=True) == other
        return False
    
    def keys(self):
        """Return config keys (required for dict unpacking)
        Tracks all keys as accessed. Only works for DictConfig.
        """
        if self._is_list_config():
            raise TypeError("ListConfig does not support keys()")
        for key in self._cfg.keys():
            self._local_accessed.add(key)
        return self._cfg.keys()
    
    def values(self):
        """Return config values (tracks all keys as accessed)"""
        if self._is_list_config():
            for i in range(len(self._cfg)):
                self._local_accessed.add(f"[{i}]")
                yield self[i]
        else:
            for key in self._cfg.keys():
                self._local_accessed.add(key)
                yield self.get(key)
    
    def items(self):
        """Return config items (tracks all keys as accessed)"""
        if self._is_list_config():
            raise TypeError("ListConfig does not support items()")
        for key in self._cfg.keys():
            self._local_accessed.add(key)
            yield key, self.get(key)
    
    def get(self, key: str, default: Any = None) -> Any:
        """Get value with default fallback"""
        self._local_accessed.add(key)
        value = self._cfg.get(key, default)
        
        if value is not default and OmegaConf.is_config(value):
            new_path = f"{self._key_path}.{key}" if self._key_path else key
            if key not in self._children:
                self._children[key] = AccessTrackedConfig(value, parent=self, key_path=new_path)
            return self._children[key]
        
        return value
    
    def update(self, other: Any = None, **kwargs):
        """Update config with values from another dict/config"""
        if self._is_list_config():
            raise TypeError("ListConfig does not support update()")
        
        if other is not None:
            # Handle different input types
            if isinstance(other, AccessTrackedConfig):
                other = OmegaConf.to_container(other._cfg, resolve=True)
            elif OmegaConf.is_config(other):
                other = OmegaConf.to_container(other, resolve=True)
            elif hasattr(other, 'items'):
                # Dict-like object
                other = dict(other.items())
            elif hasattr(other, '__iter__'):
                # Iterable of key-value pairs
                other = dict(other)
            else:
                raise TypeError(f"Cannot update from {type(other)}")
            
            for key, value in other.items():
                self._local_accessed.add(key)
                self._cfg[key] = value
                # Invalidate child cache if exists
                if key in self._children:
                    del self._children[key]
        
        for key, value in kwargs.items():
            self._local_accessed.add(key)
            self._cfg[key] = value
            if key in self._children:
                del self._children[key]
    
    def pop(self, key, *args):
        """Remove and return a value"""
        if isinstance(key, int):
            self._local_accessed.add(f"[{key}]")
            cache_key = f"[{key}]"
        else:
            self._local_accessed.add(key)
            cache_key = key
        
        if cache_key in self._children:
            del self._children[cache_key]
        if args:
            return self._cfg.pop(key, args[0])
        return self._cfg.pop(key)
    
    def append(self, value: Any):
        """Append value to list (only for ListConfig)"""
        if not self._is_list_config():
            raise TypeError("append() only supported for ListConfig")
        self._cfg.append(value)
        idx = len(self._cfg) - 1
        self._local_accessed.add(f"[{idx}]")
    
    def extend(self, values):
        """Extend list with values (only for ListConfig)"""
        if not self._is_list_config():
            raise TypeError("extend() only supported for ListConfig")
        start_idx = len(self._cfg)
        self._cfg.extend(values)
        for i in range(start_idx, len(self._cfg)):
            self._local_accessed.add(f"[{i}]")
    
    def setdefault(self, key: str, default: Any = None) -> Any:
        """Set default value if key doesn't exist"""
        if self._is_list_config():
            raise TypeError("ListConfig does not support setdefault()")
        self._local_accessed.add(key)
        if key not in self._cfg:
            self._cfg[key] = default
        return self.get(key)
    
    def copy(self) -> 'AccessTrackedConfig':
        """Return a shallow copy (does not copy access tracking state)"""
        new_cfg = OmegaConf.create(OmegaConf.to_container(self._cfg, resolve=True))
        return AccessTrackedConfig(new_cfg)
    
    def deepcopy(self) -> 'AccessTrackedConfig':
        """Return a deep copy (does not copy access tracking state)"""
        new_cfg = OmegaConf.create(OmegaConf.to_container(self._cfg, resolve=True))
        return AccessTrackedConfig(new_cfg)
    
    def merge_with(self, *others) -> 'AccessTrackedConfig':
        """Merge with other configs and return new tracked config"""
        configs = [self._cfg]
        for other in others:
            if isinstance(other, AccessTrackedConfig):
                configs.append(other._cfg)
            elif OmegaConf.is_config(other):
                configs.append(other)
            else:
                configs.append(OmegaConf.create(other))
        
        merged = OmegaConf.merge(*configs)
        return AccessTrackedConfig(merged)
    
    def to_dict(self, resolve: bool = True) -> dict:
        """Convert to plain dictionary or list"""
        return OmegaConf.to_container(self._cfg, resolve=resolve)
    
    def to_yaml(self, resolve: bool = False) -> str:
        """Convert to YAML string"""
        return OmegaConf.to_yaml(self._cfg, resolve=resolve)
    
    def unwrap(self) -> Union[DictConfig, ListConfig]:
        """Get the underlying OmegaConf object"""
        return self._cfg
    
    def get_root(self) -> 'AccessTrackedConfig':
        """Get root config object"""
        current = self
        while current._parent is not None:
            current = current._parent
        return current
    
    def _collect_all_paths(self, node: 'AccessTrackedConfig' = None, prefix: str = "") -> Set[str]:
        """Recursively collect all accessed paths"""
        if node is None:
            node = self.get_root()
        
        paths = set()
        for key in node._local_accessed:
            current_path = f"{prefix}.{key}" if prefix and not key.startswith('[') else f"{prefix}{key}" if prefix else key
            paths.add(current_path)
            if key in node._children:
                paths.update(self._collect_all_paths(node._children[key], current_path))
        return paths
    
    def _filter_leaf_paths(self, paths: Set[str]) -> Set[str]:
        """Filter to only leaf paths (no sub-paths)"""
        if not paths:
            return set()
        
        leaf_paths = set()
        for path in paths:
            # Check if any other path starts with this path followed by . or [
            is_leaf = True
            for other in paths:
                if other != path:
                    if other.startswith(f"{path}.") or other.startswith(f"{path}["):
                        is_leaf = False
                        break
            if is_leaf:
                leaf_paths.add(path)
        return leaf_paths
    
    @staticmethod
    def _get_nested_value(cfg, path: str) -> Any:
        """Get nested value through dot-separated path with bracket notation support"""
        import re
        value = cfg
        # Split by . but keep bracket notation together
        parts = re.split(r'\.(?![^\[]*\])', path)
        for part in parts:
            # Handle bracket notation like [0]
            bracket_match = re.match(r'\[(\d+)\]', part)
            if bracket_match:
                idx = int(bracket_match.group(1))
                value = value[idx]
            elif '[' in part:
                # Handle cases like "key[0]"
                key_part, rest = part.split('[', 1)
                if key_part:
                    value = value[key_part]
                indices = re.findall(r'\[(\d+)\]', '[' + rest)
                for idx_str in indices:
                    value = value[int(idx_str)]
            else:
                value = value[part]
        
        return OmegaConf.to_container(value, resolve=True) if OmegaConf.is_config(value) else value
    
    @staticmethod
    def _set_nested_value(d: dict, path: str, value: Any):
        """Set nested value through dot-separated path"""
        import re
        parts = re.split(r'\.(?![^\[]*\])', path)
        
        for i, part in enumerate(parts[:-1]):
            bracket_match = re.match(r'\[(\d+)\]', part)
            if bracket_match:
                idx = int(bracket_match.group(1))
                while len(d) <= idx:
                    d.append({})
                d = d[idx]
            elif '[' in part:
                key_part, rest = part.split('[', 1)
                if key_part:
                    d = d.setdefault(key_part, {})
                indices = re.findall(r'\[(\d+)\]', '[' + rest)
                for idx_str in indices:
                    idx = int(idx_str)
                    if isinstance(d, list):
                        while len(d) <= idx:
                            d.append({})
                        d = d[idx]
                    else:
                        d = d.setdefault(idx, {})
            else:
                d = d.setdefault(part, {})
        
        # Set final value
        last_part = parts[-1]
        bracket_match = re.match(r'\[(\d+)\]', last_part)
        if bracket_match:
            idx = int(bracket_match.group(1))
            while len(d) <= idx:
                d.append(None)
            d[idx] = value
        elif '[' in last_part:
            key_part, rest = last_part.split('[', 1)
            if key_part:
                d = d.setdefault(key_part, [])
            indices = re.findall(r'\[(\d+)\]', '[' + rest)
            for idx_str in indices[:-1]:
                idx = int(idx_str)
                while len(d) <= idx:
                    d.append([])
                d = d[idx]
            final_idx = int(indices[-1])
            while len(d) <= final_idx:
                d.append(None)
            d[final_idx] = value
        else:
            d[last_part] = value
    
    def export_accessed_config(self, use_original_values: bool = True) -> dict:
        """Export accessed configuration as dictionary (only leaf values)"""
        all_paths = self._collect_all_paths()
        leaf_paths = self._filter_leaf_paths(all_paths)
        source_cfg = AccessTrackedConfig._original_cfg_snapshot if use_original_values else self.get_root()._cfg
        
        result = {}
        for path in sorted(leaf_paths):
            try:
                value = self._get_nested_value(source_cfg, path)
                self._set_nested_value(result, path, value)
            except Exception:
                if use_original_values:
                    try:
                        value = self._get_nested_value(self.get_root()._cfg, path)
                        self._set_nested_value(result, path, value)
                    except Exception:
                        pass
        return result
    
    def save_accessed_config(self, filepath: Path, use_original_values: bool = True):
        """Save accessed configuration to file"""
        accessed_config = self.export_accessed_config(use_original_values=use_original_values)
        filepath = Path(filepath)
        
        filepath.parent.mkdir(parents=True, exist_ok=True)
        
        with open(filepath, 'w') as f:
            if filepath.suffix == '.json':
                json.dump(accessed_config, f, indent=2)
            elif filepath.suffix in ('.yaml', '.yml'):
                OmegaConf.save(OmegaConf.create(accessed_config), f)
            else:
                raise ValueError(f"Unsupported file format: {filepath.suffix}")
    
    def get_access_summary(self) -> dict:
        """Get summary of accessed configuration"""
        all_paths = self._collect_all_paths()
        leaf_paths = self._filter_leaf_paths(all_paths)
        
        return {
            "total_accessed_keys": len(all_paths),
            "leaf_accessed_keys": len(leaf_paths),
            "leaf_accessed_paths": sorted(leaf_paths),
            "top_level_keys": sorted(self.get_root()._local_accessed)
        }
    
    def print_access_summary(self):
        """Print a formatted summary of accessed configuration"""
        summary = self.get_access_summary()
        print(f"\n{'='*60}")
        print("Configuration Access Summary")
        print(f"{'='*60}")
        print(f"Total accessed keys: {summary['total_accessed_keys']}")
        print(f"Leaf accessed keys: {summary['leaf_accessed_keys']}")
        print(f"\nTop-level keys accessed: {summary['top_level_keys']}")
        print(f"\nLeaf paths accessed:")
        for path in summary['leaf_accessed_paths']:
            print(f"  - {path}")
        print(f"{'='*60}\n")


def wrap_config(cfg: OmegaConf) -> AccessTrackedConfig:
    """Wrap OmegaConf configuration to enable access tracking"""
    return AccessTrackedConfig(cfg)


def unwrap_config(cfg) -> OmegaConf:
    """Unwrap AccessTrackedConfig to get underlying OmegaConf object"""
    return cfg.unwrap() if isinstance(cfg, AccessTrackedConfig) else cfg


# ========== Monkey Patch OmegaConf for Compatibility ==========

_original_to_container = OmegaConf.to_container
_original_save = OmegaConf.save
_original_to_yaml = OmegaConf.to_yaml
_original_is_config = OmegaConf.is_config
_original_merge = OmegaConf.merge


def _patched_to_container(cfg, resolve=True, enum_to_str=False, structured_config_mode=None):
    """Patched OmegaConf.to_container that handles AccessTrackedConfig"""
    if isinstance(cfg, AccessTrackedConfig):
        cfg = cfg.unwrap()
    
    try:
        if structured_config_mode is not None:
            return _original_to_container(cfg, resolve=resolve, enum_to_str=enum_to_str, 
                                         structured_config_mode=structured_config_mode)
        else:
            return _original_to_container(cfg, resolve=resolve, enum_to_str=enum_to_str)
    except TypeError:
        return _original_to_container(cfg, resolve=resolve)


def _patched_save(config, f, resolve=False):
    """Patched OmegaConf.save that handles AccessTrackedConfig"""
    if isinstance(config, AccessTrackedConfig):
        config = config.unwrap()
    return _original_save(config, f, resolve=resolve)


def _patched_to_yaml(cfg, resolve=False, sort_keys=False):
    """Patched OmegaConf.to_yaml that handles AccessTrackedConfig"""
    if isinstance(cfg, AccessTrackedConfig):
        cfg = cfg.unwrap()
    
    try:
        return _original_to_yaml(cfg, resolve=resolve, sort_keys=sort_keys)
    except TypeError:
        return _original_to_yaml(cfg, resolve=resolve)


def _patched_is_config(obj):
    """Patched OmegaConf.is_config that handles AccessTrackedConfig"""
    return True if isinstance(obj, AccessTrackedConfig) else _original_is_config(obj)


def _patched_merge(*configs):
    """Patched OmegaConf.merge that handles AccessTrackedConfig"""
    unwrapped_configs = []
    for cfg in configs:
        if isinstance(cfg, AccessTrackedConfig):
            unwrapped_configs.append(cfg.unwrap())
        else:
            unwrapped_configs.append(cfg)
    return _original_merge(*unwrapped_configs)


# Apply patches
OmegaConf.to_container = _patched_to_container
OmegaConf.save = _patched_save
OmegaConf.to_yaml = _patched_to_yaml
OmegaConf.is_config = _patched_is_config
OmegaConf.merge = _patched_merge