feat: Complete pipeline hot-rebuild implementation with acceptance tests
- Implements pipeline hot-rebuild with state preservation (issue #43) - Adds auto-injection of MVP stages for missing capabilities - Adds radial camera mode for polar coordinate scanning - Adds afterimage and motionblur effects using framebuffer history - Adds comprehensive acceptance tests for camera modes and pipeline rebuild - Updates presets.toml with new effect configurations Related to: #35 (Pipeline Mutation API epic) Closes: #43, #44, #45
This commit is contained in:
@@ -68,6 +68,15 @@ class Pipeline:
|
||||
self._metrics_enabled = self.config.enable_metrics
|
||||
self._frame_metrics: list[FrameMetrics] = []
|
||||
self._max_metrics_frames = 60
|
||||
|
||||
# Minimum capabilities required for pipeline to function
|
||||
# NOTE: Research later - allow presets to override these defaults
|
||||
self._minimum_capabilities: set[str] = {
|
||||
"source",
|
||||
"render.output",
|
||||
"display.output",
|
||||
"camera.state", # Always required for viewport filtering
|
||||
}
|
||||
self._current_frame_number = 0
|
||||
|
||||
def add_stage(self, name: str, stage: Stage, initialize: bool = True) -> "Pipeline":
|
||||
@@ -214,15 +223,22 @@ class Pipeline:
|
||||
pass
|
||||
|
||||
def _rebuild(self) -> None:
|
||||
"""Rebuild execution order after mutation without full reinitialization."""
|
||||
"""Rebuild execution order after mutation or auto-injection."""
|
||||
was_initialized = self._initialized
|
||||
self._initialized = False
|
||||
|
||||
self._capability_map = self._build_capability_map()
|
||||
self._execution_order = self._resolve_dependencies()
|
||||
|
||||
try:
|
||||
self._validate_dependencies()
|
||||
self._validate_types()
|
||||
except StageError:
|
||||
pass
|
||||
|
||||
# Restore initialized state
|
||||
self._initialized = was_initialized
|
||||
|
||||
def get_stage(self, name: str) -> Stage | None:
|
||||
"""Get a stage by name."""
|
||||
return self._stages.get(name)
|
||||
@@ -297,10 +313,123 @@ class Pipeline:
|
||||
"stage_count": len(self._stages),
|
||||
}
|
||||
|
||||
def build(self) -> "Pipeline":
|
||||
"""Build execution order based on dependencies."""
|
||||
@property
|
||||
def minimum_capabilities(self) -> set[str]:
|
||||
"""Get minimum capabilities required for pipeline to function."""
|
||||
return self._minimum_capabilities
|
||||
|
||||
@minimum_capabilities.setter
|
||||
def minimum_capabilities(self, value: set[str]):
|
||||
"""Set minimum required capabilities.
|
||||
|
||||
NOTE: Research later - allow presets to override these defaults
|
||||
"""
|
||||
self._minimum_capabilities = value
|
||||
|
||||
def validate_minimum_capabilities(self) -> tuple[bool, list[str]]:
|
||||
"""Validate that all minimum capabilities are provided.
|
||||
|
||||
Returns:
|
||||
Tuple of (is_valid, missing_capabilities)
|
||||
"""
|
||||
missing = []
|
||||
for cap in self._minimum_capabilities:
|
||||
if not self._find_stage_with_capability(cap):
|
||||
missing.append(cap)
|
||||
return len(missing) == 0, missing
|
||||
|
||||
def ensure_minimum_capabilities(self) -> list[str]:
|
||||
"""Automatically inject MVP stages if minimum capabilities are missing.
|
||||
|
||||
Auto-injection is always on, but defaults are trivial to override.
|
||||
Returns:
|
||||
List of stages that were injected
|
||||
"""
|
||||
from engine.camera import Camera
|
||||
from engine.data_sources.sources import EmptyDataSource
|
||||
from engine.display import DisplayRegistry
|
||||
from engine.pipeline.adapters import (
|
||||
CameraClockStage,
|
||||
CameraStage,
|
||||
DataSourceStage,
|
||||
DisplayStage,
|
||||
SourceItemsToBufferStage,
|
||||
)
|
||||
|
||||
injected = []
|
||||
|
||||
# Check for source capability
|
||||
if (
|
||||
not self._find_stage_with_capability("source")
|
||||
and "source" not in self._stages
|
||||
):
|
||||
empty_source = EmptyDataSource(width=80, height=24)
|
||||
self.add_stage("source", DataSourceStage(empty_source, name="empty"))
|
||||
injected.append("source")
|
||||
|
||||
# Check for camera.state capability (must be BEFORE render to accept SOURCE_ITEMS)
|
||||
camera = None
|
||||
if not self._find_stage_with_capability("camera.state"):
|
||||
# Inject static camera (trivial, no movement)
|
||||
camera = Camera.scroll(speed=0.0)
|
||||
camera.set_canvas_size(200, 200)
|
||||
if "camera_update" not in self._stages:
|
||||
self.add_stage(
|
||||
"camera_update", CameraClockStage(camera, name="camera-clock")
|
||||
)
|
||||
injected.append("camera_update")
|
||||
|
||||
# Check for render capability
|
||||
if (
|
||||
not self._find_stage_with_capability("render.output")
|
||||
and "render" not in self._stages
|
||||
):
|
||||
self.add_stage("render", SourceItemsToBufferStage(name="items-to-buffer"))
|
||||
injected.append("render")
|
||||
|
||||
# Check for camera stage (must be AFTER render to accept TEXT_BUFFER)
|
||||
if camera and "camera" not in self._stages:
|
||||
self.add_stage("camera", CameraStage(camera, name="static"))
|
||||
injected.append("camera")
|
||||
|
||||
# Check for display capability
|
||||
if (
|
||||
not self._find_stage_with_capability("display.output")
|
||||
and "display" not in self._stages
|
||||
):
|
||||
display = DisplayRegistry.create("terminal")
|
||||
if display:
|
||||
self.add_stage("display", DisplayStage(display, name="terminal"))
|
||||
injected.append("display")
|
||||
|
||||
# Rebuild pipeline if stages were injected
|
||||
if injected:
|
||||
self._rebuild()
|
||||
|
||||
return injected
|
||||
|
||||
def build(self, auto_inject: bool = True) -> "Pipeline":
|
||||
"""Build execution order based on dependencies.
|
||||
|
||||
Args:
|
||||
auto_inject: If True, automatically inject MVP stages for missing capabilities
|
||||
"""
|
||||
self._capability_map = self._build_capability_map()
|
||||
self._execution_order = self._resolve_dependencies()
|
||||
|
||||
# Validate minimum capabilities and auto-inject if needed
|
||||
if auto_inject:
|
||||
is_valid, missing = self.validate_minimum_capabilities()
|
||||
if not is_valid:
|
||||
injected = self.ensure_minimum_capabilities()
|
||||
if injected:
|
||||
print(
|
||||
f" \033[38;5;226mAuto-injected stages for missing capabilities: {injected}\033[0m"
|
||||
)
|
||||
# Rebuild after auto-injection
|
||||
self._capability_map = self._build_capability_map()
|
||||
self._execution_order = self._resolve_dependencies()
|
||||
|
||||
self._validate_dependencies()
|
||||
self._validate_types()
|
||||
self._initialized = True
|
||||
@@ -367,12 +496,24 @@ class Pipeline:
|
||||
temp_mark.add(name)
|
||||
stage = self._stages.get(name)
|
||||
if stage:
|
||||
# Handle capability-based dependencies
|
||||
for dep in stage.dependencies:
|
||||
# Find a stage that provides this capability
|
||||
dep_stage_name = self._find_stage_with_capability(dep)
|
||||
if dep_stage_name:
|
||||
visit(dep_stage_name)
|
||||
|
||||
# Handle direct stage dependencies
|
||||
for stage_dep in stage.stage_dependencies:
|
||||
if stage_dep in self._stages:
|
||||
visit(stage_dep)
|
||||
else:
|
||||
# Stage dependency not found - this is an error
|
||||
raise StageError(
|
||||
name,
|
||||
f"Missing stage dependency: '{stage_dep}' not found in pipeline",
|
||||
)
|
||||
|
||||
temp_mark.remove(name)
|
||||
visited.add(name)
|
||||
ordered.append(name)
|
||||
|
||||
Reference in New Issue
Block a user