Skip to content

Push-Based Resolution Signaling Design

Current Problem with Forward Signaling

While forward signaling is better than recursive checking everything, it still requires: - Recursively checking values when assigned to detect RuntimeValues - ~5,000+ function calls for checking collections

Better Solution: Push-Based Signaling

RuntimeValues should signal UP to their containing resource when created or assigned, rather than resources checking DOWN into their values.

Design Options

Option 1: Context-Aware RuntimeValue Creation

# Track current resource being constructed
_current_resource_context = contextvars.ContextVar("current_resource", default=None)


class Resource:
    def __init__(self, **kwargs):
        # Set context during construction
        token = _current_resource_context.set(self)
        try:
            super().__init__(**kwargs)
            # Process config values
        finally:
            _current_resource_context.reset(token)


class ResourceOutputWithContext:
    def __init__(self, resource, attribute, expected_type):
        # ... existing init ...

        # Check if we're inside a resource construction
        if current_resource := _current_resource_context.get():
            current_resource._needs_resolution = True

Option 2: Direct Assignment Detection (Simpler)

class Resource:
    def __setattr__(self, name, value):
        super().__setattr__(name, value)

        # Direct check - no recursion!
        if hasattr(value, "_is_runtime_value"):
            self.__dict__["_needs_resolution"] = True
            return

        # For collections, we still need to check, but differently
        value_type = type(value)
        if value_type is dict:
            # Check values directly (1 level only)
            for v in value.values():
                if hasattr(v, "_is_runtime_value"):
                    self.__dict__["_needs_resolution"] = True
                    return
        elif value_type in (list, tuple):
            # Check items directly (1 level only)
            for item in value:
                if hasattr(item, "_is_runtime_value"):
                    self.__dict__["_needs_resolution"] = True
                    return

Option 3: Lazy Resolution Marking

# Don't check at assignment time at all
# Instead, when creating config for synthesis:


def _to_config(self):
    config = {}
    self._needs_resolution = False

    for key, value in self.__dict__.items():
        if not key.startswith("_"):
            config[key] = value
            # Only check top-level values
            if hasattr(value, "_is_runtime_value"):
                self._needs_resolution = True

    return config

Option 2 is the best balance: - Simple to implement - No context variables needed - Direct checks only (no deep recursion) - Minimal overhead for primitives

Performance Impact

Current recursive approach for 10k resources with 50% having RuntimeValues: - ~150,000 hasattr() calls - Deep recursion into all collections - 23,493 r/s

Expected with Option 2: - ~15,000 hasattr() calls (only direct values + 1-level collections) - No deep recursion - Expected: ~35,000+ r/s

Implementation Benefits

  1. Elimination of Recursive Checking: No _contains_runtime_values method needed
  2. Constant Time for Primitives: Primitive assignments have zero overhead
  3. Linear Time for Collections: Only check direct children, not nested
  4. Clear Signal Path: RuntimeValue → Resource (direct path)