Resolution Tracking Options
The Problem
We can't predict all possible ways RuntimeValues can be nested: - Nested dicts/lists at any depth - Custom objects - Tuples, sets, frozensets - Any future collection types - User-defined containers
Option 1: Always Resolve Everything (Safe but Slow)
def _synthesize_hcl(self, writer):
# Always resolve - simple and correct
config = self._to_config()
resolved_config = resolve_value(config, context)
Option 2: Deep Inspection with Recursion Limit
def _needs_resolution_check(self, value, depth=0):
if depth > 3: # Limit recursion
return True # Assume it might need resolution
# Check current value
if hasattr(value, "_is_runtime_value"):
return True
# Recurse into collections
if isinstance(value, (dict, list, tuple, set)):
# ... recursive check ...
pass
Option 3: Lazy Marking During Resolution
def _synthesize_hcl(self, writer):
config = self._to_config()
# Try to use config directly
try:
self._write_resource_attributes(writer, config)
except NeedsResolutionError:
# Only resolve if we hit a RuntimeValue during writing
resolved = resolve_value(config, context)
self._write_resource_attributes(writer, resolved)
Option 4: Registration Pattern (Most Robust)
class Resource:
def __init__(self, **config):
self._runtime_values = set() # Track all RuntimeValues
super().__init__(**config)
def register_runtime_value(self, rv):
"""Called by RuntimeValue when assigned anywhere."""
self._runtime_values.add(rv)
@property
def needs_resolution(self):
return bool(self._runtime_values)
Then modify RuntimeValue creation:
class ResourceOutputWithContext:
def __init__(self, resource, attribute, expected_type):
# ... existing ...
# Register with target resource if in assignment context
if target_resource := get_assignment_target():
target_resource.register_runtime_value(self)
Option 5: Conservative Heuristic
def _needs_resolution(self):
# If ANY config value is not a primitive, assume it might need resolution
for value in self._config.values():
if type(value) not in _PRIMITIVE_TYPES:
return True
return False
Recommendation: Option 1 or Option 5
Given the complexity and infinite nesting possibilities, I recommend either:
- Option 1: Always resolve (simple, correct, 26k r/s is still 870% better than baseline)
- Option 5: Conservative heuristic (simple, fast, some over-resolution)
The current approach of trying to check specific patterns is fragile and will always have edge cases.