""" ALLI Interactive Execution Session & Session Continuity - Phase 6 Step 6.4 & Final Audit Provides persistent, thread-safe, stateful execution sessions for validated ALLI workflows. Retains full 256-bit SHA-256 graph fingerprint, deterministic execution order, step indices, completed node results, and outputs across sequential step invocations. Includes: - Full 64-character SHA-256 graph revision fingerprinting - Safe canonical nested JSON serialization handling sets, dates, and arbitrary objects - Thread-safe per-session mutex locking - Bounded memory management with configurable capacity and TTL expiration cleanup - Resilient storage backing for multi-worker topology and process restart recovery """ import hashlib import json import os import tempfile import threading import time from typing import Dict, Any, List, Optional, Set def _canonical_json_serializer(obj: Any) -> Any: """Safe, deterministic JSON serializer for arbitrary nested configuration values.""" if isinstance(obj, (set, frozenset)): return sorted(list(obj), key=str) if hasattr(obj, "to_dict") and callable(obj.to_dict): return obj.to_dict() if hasattr(obj, "isoformat") and callable(obj.isoformat): return obj.isoformat() return str(obj) def compute_graph_hash(graph: Dict[str, Any]) -> str: """ Computes a deterministic full 256-bit SHA-256 fingerprint (64-character hex digest) of graph nodes, edges, and configurations. Used to detect graph revision mismatches when resuming interactive sessions. Handles nested configurations, non-JSON types, and complex values safely without crashing. """ raw_nodes = graph.get("nodes") or [] raw_edges = graph.get("edges") or [] clean_nodes = [] for n in raw_nodes: nid = str(n.get("id")) ntype = str(n.get("type") or n.get("node_type") or "") d = n.get("data") or {} p = n.get("parameters") or {} clean_nodes.append({ "id": nid, "type": ntype, "data": d, "parameters": p, }) clean_nodes.sort(key=lambda x: x["id"]) clean_edges = [] for e in raw_edges: s = str(e.get("source") or e.get("from_node")) t = str(e.get("target") or e.get("to_node")) sh = str(e.get("sourceHandle") or e.get("source_handle") or e.get("from_port") or "") th = str(e.get("targetHandle") or e.get("target_handle") or e.get("to_port") or "") clean_edges.append({"source": s, "target": t, "sourceHandle": sh, "targetHandle": th}) clean_edges.sort(key=lambda x: (x["source"], x["target"], x["sourceHandle"], x["targetHandle"])) canonical_repr = json.dumps( {"nodes": clean_nodes, "edges": clean_edges}, sort_keys=True, default=_canonical_json_serializer, separators=(",", ":"), ) return hashlib.sha256(canonical_repr.encode("utf-8")).hexdigest() class GraphRevisionMismatchError(Exception): """Raised when an attempt is made to resume a session against a modified graph.""" def __init__(self, run_id: str, expected_hash: str, incoming_hash: str): message = ( f"[ERR-GRAPH-MISMATCH] Graph revision mismatch for session '{run_id}'. " f"Expected fingerprint '{expected_hash}', received '{incoming_hash}'. " f"Cannot resume session against a modified graph." ) super().__init__(message) self.run_id = run_id self.expected_hash = expected_hash self.incoming_hash = incoming_hash class ExecutionSession: """ Persistent, thread-safe execution session retaining execution position, node results, outputs, and lifecycle state. """ def __init__( self, run_id: str, graph: Dict[str, Any], execution_order: List[str], execution_plan: Optional[Dict[str, Any]] = None, timeout_seconds: float = 5.0, workflow_id: Optional[str] = None, ): self.run_id = run_id self.workflow_id = workflow_id or str(graph.get("workflow_id") or graph.get("id") or "default_workflow") self.graph = graph self.graph_fingerprint = compute_graph_hash(graph) self.execution_order = list(execution_order) self.execution_plan = execution_plan self.timeout_seconds = timeout_seconds self.current_step_index = 0 self.completed_node_ids: List[str] = [] self.node_outputs_store: Dict[str, Dict[str, Any]] = {} self.node_results: Dict[str, Dict[str, Any]] = {} self.logs: List[str] = [f"[SESSION] Initialized session '{run_id}' with {len(execution_order)} nodes in plan."] self.stdout_lines: List[str] = [] self._secret_values: tuple = () self.lifecycle_status: str = "paused" # "created" | "paused" | "running" | "completed" | "failed" | "cancelled" self.created_at = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()) self.updated_at = self.created_at self.last_accessed_at = time.time() # Thread-safe session lock ensuring concurrent requests cannot advance the same session twice self.lock = threading.RLock() self.cancel_requested = threading.Event() self._initialize_node_states() def _initialize_node_states(self): raw_nodes = self.graph.get("nodes") or [] node_map = {str(n.get("id")): n for n in raw_nodes} for nid in self.execution_order: n = node_map.get(nid, {}) d = n.get("data") or {} ntype = str(n.get("node_type") or n.get("type") or d.get("node_type") or d.get("nodeType") or "").lower() lbl = d.get("label") or n.get("label") or ntype or nid if nid not in self.node_results: self.node_results[nid] = { "run_id": self.run_id, "node_id": nid, "node_type": ntype, "label": lbl, "status": "queued", "config_state": "valid", "exec_state": "queued", "freshness": "no_result", "inputs": {}, "outputs": {}, "output_ports": {}, "duration_ms": 0.0, "retries_attempted": 0, "error": None, "error_semantics": None, "skip_reason": None, "stale_reason": None, "timestamp": self.created_at, } def touch(self): """Updates last accessed timestamp for TTL expiration tracking.""" self.last_accessed_at = time.time() def get_next_eligible_node_id(self) -> Optional[str]: for nid in self.execution_order: st = self.node_results.get(nid, {}).get("exec_state") if st in ("queued", "idle", "pending", None): return nid return None def is_complete(self) -> bool: return self.get_next_eligible_node_id() is None def to_dict(self) -> Dict[str, Any]: from language_core.runtime_events import redact_sensitive_data, redact_sensitive_text from runtime_environment.env_injection import RunEnvContext, redact_secrets_from_obj, redact_secrets_from_text self.touch() next_nid = self.get_next_eligible_node_id() curr_nid = ( self.execution_order[self.current_step_index - 1] if 0 < self.current_step_index <= len(self.execution_order) else None ) sec_vals = getattr(self, "_secret_values", ()) env_ctx = RunEnvContext(secret_values=sec_vals) if sec_vals else None scrub_obj = lambda val: redact_sensitive_data(redact_secrets_from_obj(val, env_ctx)) scrub_txt = lambda val: redact_sensitive_text(redact_secrets_from_text(val, env_ctx)) return { "run_id": self.run_id, "session_id": self.run_id, "workflow_id": self.workflow_id, "graph": self.graph, "graph_fingerprint": self.graph_fingerprint, "lifecycle_status": self.lifecycle_status, "execution_order": self.execution_order, "execution_plan": self.execution_plan, "current_step_index": self.current_step_index, "total_steps": len(self.execution_order), "current_node_id": curr_nid, "next_node_id": next_nid, "is_workflow_complete": self.is_complete(), "completed_node_ids": list(self.completed_node_ids), "node_results": scrub_obj(self.node_results), "outputs": scrub_obj(self.node_outputs_store), "logs": [scrub_txt(item) for item in self.logs], "stdout": scrub_txt("\n".join(self.stdout_lines)), "created_at": self.created_at, "updated_at": self.updated_at, "last_accessed_at": self.last_accessed_at, } @classmethod def from_dict(cls, data: Dict[str, Any]) -> "ExecutionSession": """Rehydrates an ExecutionSession from serialized state dictionary.""" session = cls( run_id=data["run_id"], graph=data.get("graph") or {}, execution_order=data.get("execution_order") or [], execution_plan=data.get("execution_plan"), timeout_seconds=float(data.get("timeout_seconds") or 5.0), workflow_id=data.get("workflow_id"), ) session.graph_fingerprint = data.get("graph_fingerprint") or session.graph_fingerprint session.current_step_index = int(data.get("current_step_index") or 0) session.completed_node_ids = list(data.get("completed_node_ids") or []) session.node_outputs_store = dict(data.get("outputs") or data.get("node_outputs_store") or {}) session.node_results = dict(data.get("node_results") or {}) session.logs = list(data.get("logs") or []) if isinstance(data.get("stdout"), str): session.stdout_lines = data["stdout"].splitlines() elif isinstance(data.get("stdout_lines"), list): session.stdout_lines = list(data["stdout_lines"]) session.lifecycle_status = data.get("lifecycle_status") or "paused" session.created_at = data.get("created_at") or session.created_at session.updated_at = data.get("updated_at") or session.updated_at session.last_accessed_at = float(data.get("last_accessed_at") or time.time()) return session class ExecutionSessionRegistry: """ Thread-safe registry of active interactive workflow execution sessions with: - Bounded capacity (max_sessions cap with LRU eviction) - TTL expiration cleanup - Storage backing directory for cross-worker sharing and restart persistence """ def __init__( self, storage_dir: Optional[str] = None, max_sessions: int = 1000, ttl_seconds: float = 3600.0, ): self._sessions: Dict[str, ExecutionSession] = {} self._lock = threading.RLock() self.max_sessions = max_sessions self.ttl_seconds = ttl_seconds self.storage_dir = storage_dir or os.environ.get( "ALLI_SESSION_STORE_DIR", os.path.join(tempfile.gettempdir(), "alli_sessions"), ) if self.storage_dir: try: os.makedirs(self.storage_dir, exist_ok=True) except Exception: pass def _get_storage_path(self, run_id: str) -> Optional[str]: if not self.storage_dir: return None safe_id = "".join([c for c in run_id if c.isalnum() or c in ("-", "_")]) if not safe_id: safe_id = "default" return os.path.join(self.storage_dir, f"sess_{safe_id}.json") def _persist_session(self, session: ExecutionSession): """Atomically persists session snapshot to storage backing.""" path = self._get_storage_path(session.run_id) if not path: return try: tmp_path = path + ".tmp" data = session.to_dict() with open(tmp_path, "w", encoding="utf-8") as f: json.dump(data, f, default=_canonical_json_serializer) os.replace(tmp_path, path) except Exception: pass def _load_persisted_session(self, run_id: str) -> Optional[ExecutionSession]: """Loads and rehydrates a session from backing storage.""" path = self._get_storage_path(run_id) if not path or not os.path.exists(path): return None try: with open(path, "r", encoding="utf-8") as f: data = json.load(f) return ExecutionSession.from_dict(data) except Exception: return None def _enforce_capacity(self): """Evicts oldest inactive/completed sessions if max capacity is exceeded.""" if len(self._sessions) <= self.max_sessions: return # Prioritize evicting completed or cancelled sessions first removable = [ (nid, sess.last_accessed_at) for nid, sess in self._sessions.items() if sess.lifecycle_status in ("completed", "cancelled", "failed") ] if not removable: removable = [(nid, sess.last_accessed_at) for nid, sess in self._sessions.items()] removable.sort(key=lambda x: x[1]) excess = len(self._sessions) - self.max_sessions + 10 # evict batch of 10 for nid, _ in removable[:excess]: self._sessions.pop(nid, None) def cleanup_expired_sessions(self, max_age_seconds: Optional[float] = None) -> int: """ Removes expired sessions whose inactivity exceeds TTL. Returns count of removed sessions. """ limit = max_age_seconds if max_age_seconds is not None else self.ttl_seconds now = time.time() expired_ids = [] with self._lock: for rid, sess in list(self._sessions.items()): if (now - sess.last_accessed_at) > limit: expired_ids.append(rid) self._sessions.pop(rid, None) for rid in expired_ids: path = self._get_storage_path(rid) if path and os.path.exists(path): try: os.remove(path) except Exception: pass return len(expired_ids) def get_or_create( self, run_id: str, graph: Dict[str, Any], execution_order: List[str], execution_plan: Optional[Dict[str, Any]] = None, timeout_seconds: float = 5.0, workflow_id: Optional[str] = None, ) -> ExecutionSession: incoming_hash = compute_graph_hash(graph) with self._lock: if run_id in self._sessions: session = self._sessions[run_id] session.touch() if session.graph_fingerprint != incoming_hash: raise GraphRevisionMismatchError( run_id=run_id, expected_hash=session.graph_fingerprint, incoming_hash=incoming_hash, ) return session # Attempt rehydration from backing storage (e.g. cross-worker or process restart) rehydrated = self._load_persisted_session(run_id) if rehydrated: rehydrated.touch() if rehydrated.graph_fingerprint != incoming_hash: raise GraphRevisionMismatchError( run_id=run_id, expected_hash=rehydrated.graph_fingerprint, incoming_hash=incoming_hash, ) self._sessions[run_id] = rehydrated return rehydrated self._enforce_capacity() session = ExecutionSession( run_id=run_id, graph=graph, execution_order=execution_order, execution_plan=execution_plan, timeout_seconds=timeout_seconds, workflow_id=workflow_id, ) self._sessions[run_id] = session self._persist_session(session) return session def get(self, run_id: str) -> Optional[ExecutionSession]: with self._lock: if run_id in self._sessions: sess = self._sessions[run_id] sess.touch() return sess # Storage lookup fallback rehydrated = self._load_persisted_session(run_id) if rehydrated: rehydrated.touch() self._sessions[run_id] = rehydrated return rehydrated return None def save(self, session: ExecutionSession): """Persists the updated state of an execution session.""" session.touch() self._persist_session(session) def cancel(self, run_id: str) -> Optional[ExecutionSession]: session = self.get(run_id) if session: session.cancel_requested.set() with session.lock: session.lifecycle_status = "cancelled" for nid in session.execution_order: r = session.node_results.get(nid, {}) st = r.get("exec_state") or r.get("status") if st in ("running", "queued", "pending", "idle"): lbl = r.get("label", nid) session.node_results[nid] = { **r, "status": "cancelled", "exec_state": "cancelled", "freshness": "invalidated", "error": "[CANCELLED] Node execution was cancelled by user request.", } session.logs.append(f"[CANCELLED] Session '{run_id}' marked cancelled.") session.updated_at = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()) self._persist_session(session) return session def remove(self, run_id: str): with self._lock: self._sessions.pop(run_id, None) path = self._get_storage_path(run_id) if path and os.path.exists(path): try: os.remove(path) except Exception: pass GLOBAL_SESSION_REGISTRY = ExecutionSessionRegistry()