|
|
| """
|
| ╔══════════════════════════════════════════════════════════════════════════════╗
|
| ║ QSAP INTEGRATION MODULE ║
|
| ║ quasar_main4.py ←→ quasar_selective_attention.py ║
|
| ║ ║
|
| ║ Responsibilities ║
|
| ║ ─────────────── ║
|
| ║ 1. TRAINING BLOCK — monkey-patches every training entry-point in ║
|
| ║ quasar_main4.py to no-op while QSAP is active. Any of the four ║
|
| ║ training paths (AVN continuous thread, AVN._train, AsyncTraining ║
|
| ║ Executor, QuantumSystemTrainer.train_step) will log a suppression ║
|
| ║ message and return immediately without gradient steps. ║
|
| ║ ║
|
| ║ 2. QSAP INSTALL — primary path calls install_qsap_v4() which wires ║
|
| ║ the full enforcement stack (FrozenTransaction, LatestStateLatch, ║
|
| ║ ShardedInferenceEngine, hard-block on _process_latest_features). ║
|
| ║ If install_qsap_v4 is unavailable or raises AT RUNTIME (e.g. a ║
|
| ║ transitive import like torchvision is missing), we fall through to ║
|
| ║ install_qsap (v3) with a loud warning banner so operators know ║
|
| ║ enforcement is NOT active. Specifically: ║
|
| ║ a. Registers MessageContext in the QSAP module namespace so ║
|
| ║ _qsap_on_feature_message can find it via sys.modules scan. ║
|
| ║ b. Confirms quantum_bridge is fully initialised before handing it ║
|
| ║ to the QSAP engine. ║
|
| ║ c. Tries install_qsap_v4(system) first — on ANY exception ║
|
| ║ (ImportError, RuntimeError, missing transitive dep), logs ║
|
| ║ the failure and falls through to install_qsap(system). This ║
|
| ║ robustness was ADDED in v2.1.0 after a production incident ║
|
| ║ where v4 raised `No module named 'torchvision'` at runtime and ║
|
| ║ the previous catch-ImportError-only handler returned False ║
|
| ║ without trying the v3 fallback, leaving the engine with no ║
|
| ║ QSAP install at all. ║
|
| ║ d. Stores the engine handle on system._qsap_engine for diagnostics. ║
|
| ║ e. Sets system._qsap_enforcement_active = True iff v4 succeeded ║
|
| ║ (so the qsap_status() helper can report whether inference is ║
|
| ║ truly routed through ShardedInferenceEngine). ║
|
| ║ ║
|
| ║ 3. HEALTH PRINTER — background thread logs print_qsap_health() every ║
|
| ║ QSAP_HEALTH_INTERVAL_S seconds so you can see drop rates and ║
|
| ║ freshness deltas in the console while running. ║
|
| ║ ║
|
| ║ Usage (in quasar_main4.py) ║
|
| ║ ────────────────────────── ║
|
| ║ Step A — import (place with other try/except module imports): ║
|
| ║ ║
|
| ║ try: ║
|
| ║ from qsap_integration import ( ║
|
| ║ QSAP_TRAINING_BLOCKED, ║
|
| ║ apply_qsap_training_block, ║
|
| ║ apply_qsap_integration, ║
|
| ║ ) ║
|
| ║ QSAP_INTEGRATION_AVAILABLE = True ║
|
| ║ except ImportError as _e: ║
|
| ║ QSAP_INTEGRATION_AVAILABLE = False ║
|
| ║ print(f"⚠️ [QSAP] qsap_integration not found: {_e}") ║
|
| ║ ║
|
| ║ Step B — apply training block BEFORE AVNTrainer is first instantiated ║
|
| ║ (place right before _init_avn_system() / IntegratedSignal ║
|
| ║ System construction so the continuous-training thread is never ║
|
| ║ started in the first place): ║
|
| ║ ║
|
| ║ if QSAP_INTEGRATION_AVAILABLE: ║
|
| ║ apply_qsap_training_block() ║
|
| ║ ║
|
| ║ Step C — install QSAP onto the live system object (place AFTER ║
|
| ║ install_state_fix(system) and AFTER quantum_bridge is ready): ║
|
| ║ ║
|
| ║ if QSAP_INTEGRATION_AVAILABLE: ║
|
| ║ apply_qsap_integration(system) ║
|
| ║ ║
|
| ║ To re-BLOCK training later (e.g. for debugging or incident response): ║
|
| ║ import qsap_integration ║
|
| ║ qsap_integration.QSAP_TRAINING_BLOCKED = True ║
|
| ║ ║
|
| ║ Note: As of 2026-04-18, training is UNBLOCKED by default. The rewards- ║
|
| ║ channel metadata transport fix made the RL reward loop live ║
|
| ║ end-to-end, so the paranoid-safe default no longer applies. ║
|
| ║ ║
|
| ╚══════════════════════════════════════════════════════════════════════════════╝
|
|
|
| Author : ENG Karl + QSAP integration layer (2026-04)
|
| Version : 2.1.0 — v4 enforcement primary, v3 graceful fallback on ANY
|
| runtime exception (not just ImportError). Prevents the
|
| failure mode where v4 raised mid-init for a missing
|
| transitive dep and left the engine with no QSAP at all.
|
| """
|
|
|
| from __future__ import annotations
|
|
|
| import sys
|
| import time
|
| import threading
|
| import logging
|
| from typing import Optional
|
|
|
|
|
|
|
|
|
|
|
| try:
|
| import torch
|
| import torch.nn as nn
|
| except ImportError:
|
| torch = None
|
| nn = None
|
|
|
| logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| QSAP_TRAINING_BLOCKED: bool = False
|
|
|
|
|
| QSAP_HEALTH_INTERVAL_S: float = 120.0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| _training_block_applied: bool = False
|
|
|
|
|
| def apply_qsap_training_block() -> None:
|
| """
|
| Monkey-patch all training entry-points to no-op while
|
| QSAP_TRAINING_BLOCKED is True.
|
|
|
| Safe to call multiple times — subsequent calls are idempotent.
|
| Must be called BEFORE AVNTrainer is first instantiated so that the
|
| continuous-training thread is never launched.
|
| """
|
| global _training_block_applied
|
| if _training_block_applied:
|
| return
|
|
|
|
|
| _patch_avn_start_continuous_training()
|
|
|
|
|
| _patch_avn_train()
|
|
|
|
|
| _patch_async_training_executor()
|
|
|
|
|
| _patch_quantum_system_trainer()
|
|
|
| _training_block_applied = True
|
|
|
| print()
|
| if QSAP_TRAINING_BLOCKED:
|
| print("╔══════════════════════════════════════════════════════════════════════╗")
|
| print("║ QSAP TRAINING BLOCK APPLIED (v1.0.0) ║")
|
| print("╠══════════════════════════════════════════════════════════════════════╣")
|
| print("║ All training entry-points monkey-patched: ║")
|
| print("║ A. AVNTrainer.start_continuous_training → no-op ║")
|
| print("║ B. AVNTrainer._train → early-return ║")
|
| print("║ C. AsyncTrainingExecutor.submit_training → skip ║")
|
| print("║ D. QuantumSystemTrainer.train_step → return None ║")
|
| print("║ ║")
|
| print("║ To re-enable: import qsap_integration ║")
|
| print("║ qsap_integration.QSAP_TRAINING_BLOCKED = False ║")
|
| print("╚══════════════════════════════════════════════════════════════════════╝")
|
| else:
|
| print("╔══════════════════════════════════════════════════════════════════════╗")
|
| print("║ QSAP TRAINING PATCHES INSTALLED — TRAINING UNBLOCKED (v1.0.0) ║")
|
| print("╠══════════════════════════════════════════════════════════════════════╣")
|
| print("║ All training entry-points wrapped (pass-through while flag=False): ║")
|
| print("║ A. AVNTrainer.start_continuous_training → passes through ║")
|
| print("║ B. AVNTrainer._train → passes through ║")
|
| print("║ C. AsyncTrainingExecutor.submit_training → passes through ║")
|
| print("║ D. QuantumSystemTrainer.train_step → passes through ║")
|
| print("║ ║")
|
| print("║ To re-block: import qsap_integration ║")
|
| print("║ qsap_integration.QSAP_TRAINING_BLOCKED = True ║")
|
| print("╚══════════════════════════════════════════════════════════════════════╝")
|
| print()
|
|
|
|
|
|
|
|
|
| def _patch_avn_start_continuous_training() -> None:
|
| """Patch A: AVNTrainer.start_continuous_training → no-op."""
|
| _main = sys.modules.get("__main__") or sys.modules.get("quasar_main4")
|
| AVNTrainer = getattr(_main, "AVNTrainer", None)
|
| if AVNTrainer is None:
|
| logger.warning("[QSAP-Block] AVNTrainer not found in __main__ — patch A skipped")
|
| return
|
|
|
| _orig = AVNTrainer.start_continuous_training
|
|
|
| def _blocked_start_continuous_training(self_avn):
|
| import qsap_integration as _qi
|
| if _qi.QSAP_TRAINING_BLOCKED:
|
| print(
|
| f"[{time.strftime('%H:%M:%S')}] 🔒 [QSAP-Block] "
|
| f"AVNTrainer.start_continuous_training suppressed "
|
| f"(QSAP_TRAINING_BLOCKED=True)"
|
| )
|
| return
|
| return _orig(self_avn)
|
|
|
| AVNTrainer.start_continuous_training = _blocked_start_continuous_training
|
| AVNTrainer._original_start_continuous_training = _orig
|
| logger.debug("[QSAP-Block] Patch A applied: AVNTrainer.start_continuous_training")
|
|
|
|
|
| def _patch_avn_train() -> None:
|
| """Patch B: AVNTrainer._train → early-return when blocked."""
|
| _main = sys.modules.get("__main__") or sys.modules.get("quasar_main4")
|
| AVNTrainer = getattr(_main, "AVNTrainer", None)
|
| if AVNTrainer is None:
|
| logger.warning("[QSAP-Block] AVNTrainer not found — patch B skipped")
|
| return
|
|
|
| _orig = AVNTrainer._train
|
|
|
| def _blocked_train(self_avn):
|
| import qsap_integration as _qi
|
| if _qi.QSAP_TRAINING_BLOCKED:
|
|
|
| _now = time.time()
|
| if _now - getattr(self_avn, "_qsap_block_last_log", 0) > 60:
|
| print(
|
| f"[{time.strftime('%H:%M:%S')}] 🔒 [QSAP-Block] "
|
| f"AVNTrainer._train suppressed (QSAP_TRAINING_BLOCKED=True)"
|
| )
|
| self_avn._qsap_block_last_log = _now
|
| return None
|
| return _orig(self_avn)
|
|
|
| AVNTrainer._train = _blocked_train
|
| AVNTrainer._original_train = _orig
|
| logger.debug("[QSAP-Block] Patch B applied: AVNTrainer._train")
|
|
|
|
|
| def _patch_async_training_executor() -> None:
|
| """Patch C: AsyncTrainingExecutor.submit_training → skip when blocked."""
|
| _main = sys.modules.get("__main__") or sys.modules.get("quasar_main4")
|
| AsyncTrainingExecutor = getattr(_main, "AsyncTrainingExecutor", None)
|
| if AsyncTrainingExecutor is None:
|
| logger.warning("[QSAP-Block] AsyncTrainingExecutor not found — patch C skipped")
|
| return
|
|
|
| _orig = AsyncTrainingExecutor.submit_training
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| def _blocked_submit_training(self_exec, trainer, buffer, batch_size=32,
|
| training_number=0, **kwargs):
|
| import qsap_integration as _qi
|
| if _qi.QSAP_TRAINING_BLOCKED:
|
| _now = time.time()
|
| if _now - getattr(self_exec, "_qsap_block_last_log", 0) > 60:
|
| print(
|
| f"[{time.strftime('%H:%M:%S')}] 🔒 [QSAP-Block] "
|
| f"AsyncTrainingExecutor.submit_training skipped "
|
| f"(QSAP_TRAINING_BLOCKED=True)"
|
| )
|
| self_exec._qsap_block_last_log = _now
|
| return None
|
| return _orig(self_exec, trainer, buffer, batch_size=batch_size,
|
| training_number=training_number, **kwargs)
|
|
|
| AsyncTrainingExecutor.submit_training = _blocked_submit_training
|
| AsyncTrainingExecutor._original_submit_training = _orig
|
| logger.debug("[QSAP-Block] Patch C applied: AsyncTrainingExecutor.submit_training")
|
|
|
|
|
| def _patch_quantum_system_trainer() -> None:
|
| """Patch D: QuantumSystemTrainer.train_step → return None when blocked."""
|
| _main = sys.modules.get("__main__") or sys.modules.get("quasar_main4")
|
| QuantumSystemTrainer = getattr(_main, "QuantumSystemTrainer", None)
|
| if QuantumSystemTrainer is None:
|
| logger.warning("[QSAP-Block] QuantumSystemTrainer not found — patch D skipped")
|
| return
|
|
|
| _orig = QuantumSystemTrainer.train_step
|
|
|
| def _blocked_train_step(self_qt, batch_size=None):
|
| import qsap_integration as _qi
|
| if _qi.QSAP_TRAINING_BLOCKED:
|
| _now = time.time()
|
| if _now - getattr(self_qt, "_qsap_block_last_log", 0) > 60:
|
| print(
|
| f"[{time.strftime('%H:%M:%S')}] 🔒 [QSAP-Block] "
|
| f"QuantumSystemTrainer.train_step suppressed "
|
| f"(QSAP_TRAINING_BLOCKED=True)"
|
| )
|
| self_qt._qsap_block_last_log = _now
|
| return None
|
| return _orig(self_qt, batch_size)
|
|
|
| QuantumSystemTrainer.train_step = _blocked_train_step
|
| QuantumSystemTrainer._original_train_step = _orig
|
| logger.debug("[QSAP-Block] Patch D applied: QuantumSystemTrainer.train_step")
|
|
|
|
|
| def stop_existing_avn_training_thread(system) -> None:
|
| """
|
| If the AVNTrainer continuous-training thread is already running (because
|
| the block was applied after AVNTrainer.__init__), signal it to stop.
|
|
|
| Signals the thread; does NOT join — the training loop checks
|
| _continuous_training_active every train_interval_seconds.
|
| """
|
| avn_trainer = getattr(system, "avn_trainer", None)
|
| if avn_trainer is None:
|
| return
|
|
|
| was_active = getattr(avn_trainer, "_continuous_training_active", False)
|
| if was_active:
|
| avn_trainer._continuous_training_active = False
|
| print(
|
| f"[{time.strftime('%H:%M:%S')}] 🔒 [QSAP-Block] "
|
| f"AVNTrainer continuous-training thread signalled to stop"
|
| )
|
|
|
|
|
|
|
|
|
|
|
|
|
| def apply_qsap_integration(
|
| system,
|
| alpha_threshold: float = 0.02,
|
| tau_min_ms: float = 50.0,
|
| budget_slots: Optional[int] = None,
|
| commit_wait_max_ms: float = 150.0,
|
| abstain_threshold: float = 0.1,
|
| ) -> bool:
|
| """
|
| Install the QSAP perception-action loop onto a live IntegratedSignalSystem.
|
|
|
| Pre-conditions (callers must guarantee):
|
| 1. install_state_fix(system) has already run — _aoi_tracker attached.
|
| 2. system.quantum_bridge is fully initialised (not None).
|
| 3. system.agents is populated (non-empty dict).
|
| 4. ably_manager is attached and subscriptions are active (so the
|
| patched on_feature_message starts receiving real ticks immediately).
|
|
|
| Post-conditions:
|
| • system.on_feature_message → _qsap_on_feature_message
|
| • system._process_latest_features → legacy (callable as fallback)
|
| • system._qsap_engine → SelectiveBindingEngine handle
|
| • system.training_enabled → False (belt-and-suspenders block)
|
| • Background health-printer thread started (logs every
|
| QSAP_HEALTH_INTERVAL_S seconds)
|
|
|
| Returns True on success, False if QSAP module unavailable or
|
| quantum_bridge not ready.
|
| """
|
|
|
| if system is None:
|
| print("[QSAP-Install] ❌ system is None — aborting")
|
| return False
|
|
|
| if not getattr(system, "agents", None):
|
| print("[QSAP-Install] ❌ system.agents is empty — aborting")
|
| return False
|
|
|
| if not getattr(system, "quantum_bridge", None):
|
| print("[QSAP-Install] ❌ system.quantum_bridge is None — aborting")
|
| print(" QSAP needs quantum_bridge for per-agent inference.")
|
| return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| try:
|
| import quasar_selective_attention as _qsap_mod
|
| from quasar_selective_attention import install_qsap, print_qsap_health
|
| except ImportError as e:
|
| print(f"[QSAP-Install] ❌ Cannot import quasar_selective_attention: {e}")
|
| print(" Place quasar_selective_attention.py in the same")
|
| print(" directory as quasar_main4.py and retry.")
|
| return False
|
|
|
|
|
|
|
| install_qsap_v4 = getattr(_qsap_mod, "install_qsap_v4", None)
|
| apply_enforcement_patch = getattr(_qsap_mod, "apply_enforcement_patch", None)
|
| print_enforcement_health = getattr(_qsap_mod, "print_enforcement_health", None)
|
| enforcement_diagnostics = getattr(_qsap_mod, "enforcement_diagnostics", None)
|
| _V4_AVAILABLE = all((install_qsap_v4, apply_enforcement_patch,
|
| print_enforcement_health, enforcement_diagnostics))
|
|
|
| if not _V4_AVAILABLE:
|
| print("[QSAP-Install] ⚠️ v4 entry points not exported by SA module — "
|
| "will use v3 install_qsap. Enforcement patch will NOT be active.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| _sa_has_mc = hasattr(_qsap_mod, 'MessageContext') and isinstance(
|
| getattr(_qsap_mod, 'MessageContext', None), type
|
| )
|
| if _sa_has_mc:
|
| print(
|
| "[QSAP-Install] ✅ MessageContext already defined in SA module (§0b) — "
|
| "skipping injection from __main__ (F6 guard active)"
|
| )
|
| else:
|
|
|
| _main = sys.modules.get("__main__") or sys.modules.get("quasar_main4")
|
| _MainMC = getattr(_main, "MessageContext", None)
|
| if _MainMC is not None:
|
| _qsap_mod.MessageContext = _MainMC
|
| print("[QSAP-Install] ✅ MessageContext injected from __main__ (legacy SA module)")
|
| else:
|
| print(
|
| "[QSAP-Install] ⚠️ MessageContext not found in __main__ and not in SA module — "
|
| "QSAP will use SimpleNamespace fallback (harmless but upgrade SA module)"
|
| )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| import qsap_integration as _qi
|
| if _qi.QSAP_TRAINING_BLOCKED:
|
| system.training_enabled = False
|
| stop_existing_avn_training_thread(system)
|
| print(f"[QSAP-Install] 🔒 system.training_enabled = False")
|
| else:
|
| system.training_enabled = True
|
| _avn_trainer = getattr(system, "avn_trainer", None)
|
| if _avn_trainer is not None:
|
| _ct_alive = getattr(_avn_trainer, "_continuous_training_active", False)
|
| if not _ct_alive:
|
|
|
|
|
|
|
| try:
|
| _avn_trainer.start_continuous_training()
|
| print(
|
| f"[{time.strftime('%H:%M:%S')}] ✅ [QSAP-Block] "
|
| f"AVN continuous-training re-kicked "
|
| f"(QSAP_TRAINING_BLOCKED=False)"
|
| )
|
| except Exception as _ct_err:
|
| print(
|
| f"[{time.strftime('%H:%M:%S')}] ⚠️ [QSAP-Block] "
|
| f"AVN continuous-training re-kick failed: {_ct_err}"
|
| )
|
| print(f"[QSAP-Install] ✅ system.training_enabled = True (training UNBLOCKED)")
|
|
|
|
|
|
|
|
|
| _budget = budget_slots or len(system.agents)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| engine = None
|
| _v4_succeeded = False
|
|
|
| if _V4_AVAILABLE:
|
| try:
|
| _v4_ok = install_qsap_v4(
|
| system = system,
|
| alpha_threshold = alpha_threshold,
|
| tau_min_ms = tau_min_ms,
|
| commit_wait_max_ms = commit_wait_max_ms,
|
| abstain_threshold = abstain_threshold,
|
| health_interval_s = int(QSAP_HEALTH_INTERVAL_S),
|
| )
|
| if _v4_ok:
|
| engine = getattr(system, "_qsap_engine", None)
|
| if engine is not None:
|
| _v4_succeeded = True
|
| else:
|
| print("[QSAP-Install] ⚠️ install_qsap_v4 returned True but "
|
| "system._qsap_engine is None — falling back to v3")
|
| else:
|
| print("[QSAP-Install] ⚠️ install_qsap_v4 returned False — "
|
| "falling back to v3")
|
| except Exception as _v4_exc:
|
|
|
|
|
|
|
| print(f"[QSAP-Install] ⚠️ install_qsap_v4() raised: {_v4_exc}")
|
| print(f"[QSAP-Install] ⚠️ Falling back to v3 install_qsap. "
|
| f"Enforcement patch will NOT be active.")
|
| import traceback
|
| traceback.print_exc()
|
|
|
|
|
| for _stale_attr in ("_qsap_engine", "_qsap_enforcement_active"):
|
| if hasattr(system, _stale_attr):
|
| try:
|
| delattr(system, _stale_attr)
|
| except Exception:
|
| pass
|
|
|
|
|
| if not _v4_succeeded:
|
| try:
|
| engine = install_qsap(
|
| system_instance = system,
|
| alpha_threshold = alpha_threshold,
|
| tau_min_ms = tau_min_ms,
|
| budget_slots = _budget,
|
| commit_wait_max_ms = commit_wait_max_ms,
|
| abstain_threshold = abstain_threshold,
|
| )
|
| system._qsap_engine = engine
|
|
|
| if apply_enforcement_patch is not None:
|
| try:
|
| apply_enforcement_patch(system)
|
| print('[QSAP-Install] ✅ Enforcement patch applied on v3 fallback path')
|
| except Exception as _ep_err:
|
| print(f'[QSAP-Install] ⚠️ Enforcement patch failed on v3 path: {_ep_err}')
|
| except Exception as e:
|
|
|
| print(f"[QSAP-Install] ❌ install_qsap() (v3 fallback) also raised: {e}")
|
| import traceback; traceback.print_exc()
|
| return False
|
|
|
|
|
|
|
| system._qsap_enforcement_active = bool(_v4_succeeded)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| try:
|
| import numpy as _np
|
| qb = getattr(system, 'quantum_bridge', None)
|
| if qb is not None and hasattr(qb, '_orig_predict_single_agent'):
|
| _real_orig = qb._orig_predict_single_agent
|
|
|
| def _deduped_predict_single_agent(agent_name, states_dict=None):
|
| try:
|
| if getattr(qb, 'quantum_system', None) is None:
|
| return _np.array([0.5, 0.5], dtype=_np.float32)
|
| if getattr(qb, 'state_cache', None) is None:
|
| return _np.array([0.5, 0.5], dtype=_np.float32)
|
|
|
| return _real_orig(agent_name, states_dict)
|
| except Exception as _pred_err:
|
| print(f"[QSAP] predict_single_agent failed for {agent_name}: {_pred_err}")
|
| return _np.array([0.5, 0.5], dtype=_np.float32)
|
|
|
| qb.predict_single_agent = _deduped_predict_single_agent
|
| print("[QSAP-Install] ✅ De-duplicated predict_single_agent Q-value print")
|
| else:
|
| print("[QSAP-Install] ⚠️ No _orig_predict_single_agent found — "
|
| "double Q-value print may persist")
|
| except Exception as _dedup_err:
|
| print(f"[QSAP-Install] ⚠️ Q-value dedup patch failed: {_dedup_err}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| if not _v4_succeeded:
|
| _start_qsap_health_printer(system, print_qsap_health)
|
| print("[QSAP-Install] ℹ️ Started v3 QSAP health printer "
|
| "(v4 not active — upgrade SA module or fix missing deps "
|
| "to enable enforcement diagnostics)")
|
| else:
|
| print(f"[QSAP-Install] ✅ v4 enforcement health printer active "
|
| f"(reports every {int(QSAP_HEALTH_INTERVAL_S)}s via print_enforcement_health)")
|
|
|
| print()
|
| if _v4_succeeded:
|
| print("╔══════════════════════════════════════════════════════════════════════╗")
|
| print("║ QSAP INTEGRATION COMPLETE — v4 ENFORCEMENT ACTIVE ║")
|
| print("╠══════════════════════════════════════════════════════════════════════╣")
|
| print(f"║ on_feature_message → _qsap_enforced_on_feature_message ║")
|
| print(f"║ _process_latest_features → _legacy_blocked_stub (HARD-BLOCKED) ║")
|
| print(f"║ ShardedInferenceEngine → instrumented with per-agent timings ║")
|
| print(f"║ system._qsap_engine → {type(engine).__name__:<41}║")
|
| _t_state_str = "True (UNBLOCKED)" if not _qi.QSAP_TRAINING_BLOCKED else "False (BLOCKED)"
|
| print(f"║ training_enabled → {_t_state_str:<41}║")
|
| print(f"║ Health report every → {int(QSAP_HEALTH_INTERVAL_S)}s (enforcement + engine) ║")
|
| print("╠══════════════════════════════════════════════════════════════════════╣")
|
| print("║ Diagnostic tags now active in log/stdout: ║")
|
| print("║ [QSAP-ENFORCE] ENTER — per-tick dispatch proof ║")
|
| print("║ [QSAP-SHARD] agent=… — per-agent inference proof ║")
|
| print("║ [QSAP-VIOLATION] — legacy bypass attempt (should be 0) ║")
|
| print("╚══════════════════════════════════════════════════════════════════════╝")
|
| else:
|
| print("╔══════════════════════════════════════════════════════════════════════╗")
|
| print("║ QSAP INTEGRATION COMPLETE — ⚠️ v3 FALLBACK (no enforcement) ║")
|
| print("╠══════════════════════════════════════════════════════════════════════╣")
|
| print(f"║ on_feature_message → _qsap_on_feature_message (v3) ║")
|
| print(f"║ _process_latest_features → legacy (fallback only — NOT blocked) ║")
|
| print(f"║ system._qsap_engine → {type(engine).__name__:<41}║")
|
| _t_state_str = "True (UNBLOCKED)" if not _qi.QSAP_TRAINING_BLOCKED else "False (BLOCKED)"
|
| print(f"║ training_enabled → {_t_state_str:<41}║")
|
| print(f"║ Health report every → {int(QSAP_HEALTH_INTERVAL_S)}s (engine only, no enforcement) ║")
|
| print("╠══════════════════════════════════════════════════════════════════════╣")
|
| print("║ ⚠️ v4 enforcement did NOT install (missing dep or runtime error). ║")
|
| print("║ _process_latest_features remains a LIVE BYPASS PATH. ║")
|
| print("║ Fix: pip install torchvision (or whichever transitive dep ║")
|
| print("║ was named in the exception above), then restart. ║")
|
| print("╚══════════════════════════════════════════════════════════════════════╝")
|
| print()
|
|
|
| return True
|
|
|
|
|
|
|
|
|
|
|
|
|
| def _start_qsap_health_printer(system, print_qsap_health_fn) -> None:
|
| """
|
| Daemon thread that calls print_qsap_health() every QSAP_HEALTH_INTERVAL_S
|
| seconds, so freshness stats and superseded-drop counts are visible in the
|
| console without any manual call.
|
| """
|
| def _health_loop():
|
| import qsap_integration as _qi
|
| while True:
|
| time.sleep(_qi.QSAP_HEALTH_INTERVAL_S)
|
| try:
|
| engine = getattr(system, "_qsap_engine", None)
|
| if engine is not None:
|
| print_qsap_health_fn(engine)
|
| except Exception as _e:
|
| print(f"[QSAP-Health] ⚠️ Health report failed: {_e}")
|
|
|
| t = threading.Thread(
|
| target=_health_loop,
|
| daemon=True,
|
| name="QSAP-HealthPrinter",
|
| )
|
| t.start()
|
| print(f"[QSAP-Install] ✅ Health-printer thread started "
|
| f"(reports every {QSAP_HEALTH_INTERVAL_S:.0f}s)")
|
|
|
|
|
|
|
|
|
|
|
|
|
| def qsap_status(system) -> dict:
|
| """
|
| Return a dict summarising the current QSAP+training-block state.
|
| Useful for dashboard widgets or one-off checks.
|
|
|
| Under v4 enforcement, the returned dict includes enforcement counters
|
| pulled from enforcement_diagnostics(). A healthy system reports:
|
| enforcement_active = True
|
| legacy_violation_count = 0
|
| qsap_control_rate_pct ≈ 100.0
|
|
|
| Example:
|
| from qsap_integration import qsap_status
|
| print(qsap_status(system))
|
| """
|
| import qsap_integration as _qi
|
|
|
| engine = getattr(system, "_qsap_engine", None)
|
| engine_report = engine.health_report() if engine is not None else {}
|
|
|
| status = {
|
| "qsap_installed": engine is not None,
|
| "enforcement_active": bool(getattr(system, "_qsap_enforcement_active", False)),
|
| "training_blocked": _qi.QSAP_TRAINING_BLOCKED,
|
| "system_training_enabled": getattr(system, "training_enabled", "?"),
|
| "avn_ct_active": getattr(
|
| getattr(system, "avn_trainer", None),
|
| "_continuous_training_active", False
|
| ),
|
| "engine_cycles": engine_report.get("cycles_total", 0),
|
| "engine_publishes": engine_report.get("publishes", 0),
|
| "engine_superseded_drops": engine_report.get("superseded_drops", {}),
|
| "freshness_stats": engine_report.get("freshness_stats", {}),
|
| }
|
|
|
|
|
|
|
| try:
|
| from quasar_selective_attention import enforcement_diagnostics
|
| diag = enforcement_diagnostics()
|
| status.update({
|
| "qsap_dispatch_count": diag.get("qsap_dispatch_count", 0),
|
| "legacy_bypass_count": diag.get("legacy_bypass_count", 0),
|
| "legacy_violation_count": diag.get("legacy_violation_count", 0),
|
| "qsap_control_rate_pct": diag.get("qsap_control_rate_pct"),
|
| "sealed_txn_publish_count": diag.get("sealed_txn_publish_count", 0),
|
| "invalid_msg_drops": diag.get("invalid_msg_drops", 0),
|
| "dispatch_latency_ms": diag.get("dispatch_latency_ms", {}),
|
| "per_agent_inference_ms": diag.get("per_agent_inference_ms", {}),
|
| })
|
| except (ImportError, AttributeError):
|
| status.update({
|
| "qsap_dispatch_count": None,
|
| "legacy_bypass_count": None,
|
| "legacy_violation_count": None,
|
| "qsap_control_rate_pct": None,
|
| "sealed_txn_publish_count": None,
|
| })
|
|
|
| return status |