chore: sauvegarde complète avant factorisation executor
Some checks failed
security-audit / Bandit (scan statique) (push) Successful in 12s
security-audit / pip-audit (CVE dépendances) (push) Successful in 10s
security-audit / Scan secrets (grep) (push) Successful in 8s
tests / Lint (ruff + black) (push) Successful in 13s
tests / Tests unitaires (sans GPU) (push) Failing after 14s
tests / Tests sécurité (critique) (push) Has been skipped

Point de sauvegarde incluant les fichiers non committés des sessions
précédentes (systemd, docs, agents, GPU manager).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Dom
2026-04-20 17:03:44 +02:00
parent 623be15bfe
commit 447fbb2c6e
1869 changed files with 791438 additions and 324 deletions

View File

@@ -2,12 +2,20 @@
"""
Gestionnaire de vision avancé pour Agent V1.
Optimisé pour le streaming fibre avec détection de changement.
Captures disponibles :
- Plein écran (full) : contexte global 1920x1080+
- Crop ciblé (crop) : 80x80 autour du clic (apprentissage VLM)
- Fenêtre active (window) : image isolée de la fenêtre + métadonnées
(titre, rect, coordonnées clic relatives) — cross-platform
"""
import os
import time
import logging
import hashlib
import platform
from typing import Any, Dict, Optional
from PIL import Image, ImageFilter, ImageStat
import mss
from ..config import TARGETED_CROP_SIZE, SCREENSHOT_QUALITY, BLUR_SENSITIVE
@@ -15,6 +23,9 @@ from .blur_sensitive import blur_sensitive_regions
logger = logging.getLogger(__name__)
# OS courant (détecté une seule fois)
_SYSTEM = platform.system()
class VisionCapturer:
def __init__(self, session_dir: str):
self.session_dir = session_dir
@@ -27,13 +38,16 @@ class VisionCapturer:
"""
Capture l'écran complet.
Si force=False, vérifie d'abord si l'écran a changé.
Enrichit les métadonnées avec le titre de la fenêtre active
(utile pour le contextualisation des heartbeats côté serveur).
"""
try:
with mss.mss() as sct:
monitor = sct.monitors[1]
sct_img = sct.grab(monitor)
img = Image.frombytes("RGB", sct_img.size, sct_img.bgra, "raw", "BGRX")
# Détection de changement (pour Heartbeat)
if not force:
current_hash = self._compute_quick_hash(img)
@@ -52,8 +66,24 @@ class VisionCapturer:
logger.error(f"Erreur Context Capture: {e}")
return ""
def get_active_window_title(self) -> str:
"""Retourne le titre de la fenêtre active (pour enrichir les heartbeats).
Fallback gracieux : retourne une chaîne vide si indisponible.
"""
try:
from ..window_info_crossplatform import get_active_window_info
info = get_active_window_info()
return info.get("title", "")
except Exception:
return ""
def capture_dual(self, x: int, y: int, screenshot_id: str, anonymize=False) -> dict:
"""Capture duale (Full + Crop) systématique (forcée car liée à une action)."""
"""Capture triple (Full + Crop + Fenêtre active) systématique.
La fenêtre active est un AJOUT — en cas d'échec, le full + crop
sont toujours retournés (fallback gracieux).
"""
try:
with mss.mss() as sct:
full_path = os.path.join(self.shots_dir, f"{screenshot_id}_full.png")
@@ -67,7 +97,7 @@ class VisionCapturer:
left = max(0, x - w // 2)
top = max(0, y - h // 2)
crop_img = img.crop((left, top, left + w, top + h))
if anonymize:
crop_img = crop_img.filter(ImageFilter.GaussianBlur(radius=4))
@@ -82,11 +112,130 @@ class VisionCapturer:
# Mise à jour du hash pour le prochain heartbeat
self.last_img_hash = self._compute_quick_hash(img)
return {"full": full_path, "crop": crop_path}
result = {"full": full_path, "crop": crop_path}
# --- Capture de la fenêtre active ---
# Ajout non-bloquant : enrichit le résultat avec l'image
# de la fenêtre seule + métadonnées (titre, rect, clic relatif)
window_info = self.capture_active_window(x, y, screenshot_id, full_img=img)
if window_info:
result["window_capture"] = window_info
return result
except Exception as e:
logger.error(f"Erreur Dual Capture: {e}")
return {}
def capture_active_window(
self,
x: int,
y: int,
screenshot_id: str,
full_img: Optional[Image.Image] = None,
) -> Optional[Dict[str, Any]]:
"""Capture l'image de la fenêtre active seule + métadonnées.
Stratégie :
1. Obtenir le rectangle de la fenêtre via l'API OS (pywin32 / xdotool / Quartz)
2. Cropper depuis le screenshot plein écran (plus fiable que PrintWindow)
3. Calculer les coordonnées du clic relatives à la fenêtre
Args:
x, y: coordonnées du clic en pixels écran
screenshot_id: identifiant pour le nom de fichier
full_img: screenshot plein écran déjà capturé (optionnel, évite une
double capture si appelé depuis capture_dual)
Returns:
Dict avec window_image, window_title, window_rect, click_in_window,
window_size — ou None si la fenêtre est introuvable.
"""
try:
from ..window_info_crossplatform import get_active_window_rect
rect_info = get_active_window_rect()
if not rect_info:
logger.debug("Fenêtre active introuvable — skip capture fenêtre")
return None
win_rect = rect_info["rect"] # [left, top, right, bottom]
win_left, win_top, win_right, win_bottom = win_rect
win_w, win_h = rect_info["size"] # [width, height]
title = rect_info.get("title", "unknown_window")
app_name = rect_info.get("app_name", "unknown_app")
# Ignorer les fenêtres trop petites (barres de tâches, popups système)
if win_w < 50 or win_h < 50:
logger.debug(f"Fenêtre trop petite ({win_w}x{win_h}) — skip")
return None
# Coordonnées du clic relatives à la fenêtre
click_rel_x = x - win_left
click_rel_y = y - win_top
# Si le clic est en dehors de la fenêtre, on le signale mais on continue
click_inside = (0 <= click_rel_x <= win_w and 0 <= click_rel_y <= win_h)
# --- Crop de la fenêtre depuis le plein écran ---
if full_img is None:
# Pas de screenshot fourni — en capturer un (cas standalone)
try:
with mss.mss() as sct:
monitor = sct.monitors[1]
sct_img = sct.grab(monitor)
full_img = Image.frombytes(
"RGB", sct_img.size, sct_img.bgra, "raw", "BGRX"
)
except Exception as e:
logger.error(f"Erreur capture plein écran pour fenêtre : {e}")
return None
# Borner le crop aux limites de l'image plein écran
img_w, img_h = full_img.size
crop_left = max(0, win_left)
crop_top = max(0, win_top)
crop_right = min(img_w, win_right)
crop_bottom = min(img_h, win_bottom)
if crop_right <= crop_left or crop_bottom <= crop_top:
logger.debug("Fenêtre hors écran — skip capture fenêtre")
return None
window_img = full_img.crop((crop_left, crop_top, crop_right, crop_bottom))
# Floutage conformité AI Act
if BLUR_SENSITIVE:
blur_sensitive_regions(window_img)
# Sauvegarde
window_path = os.path.join(
self.shots_dir, f"{screenshot_id}_window.png"
)
window_img.save(window_path, "PNG", quality=SCREENSHOT_QUALITY)
result = {
"window_image": window_path,
"window_title": title,
"app_name": app_name,
"window_rect": win_rect,
"window_size": [win_w, win_h],
"click_in_window": [click_rel_x, click_rel_y],
"click_inside_window": click_inside,
}
logger.debug(
f"Fenêtre capturée : {title} ({win_w}x{win_h}) — "
f"clic relatif ({click_rel_x}, {click_rel_y})"
)
return result
except ImportError as e:
logger.debug(f"Module fenêtre indisponible : {e}")
return None
except Exception as e:
logger.error(f"Erreur capture fenêtre active : {e}")
return None
def _compute_quick_hash(self, img: Image) -> str:
"""Calcule un hash rapide basé sur une vignette réduite pour détecter les changements."""
# On réduit l'image à 64x64 pour comparer les masses de couleurs (très rapide)

View File

@@ -17,7 +17,7 @@ from __future__ import annotations
import platform
import subprocess
from typing import Dict, Optional
from typing import Any, Dict, Optional
def _run_cmd(cmd: list[str]) -> Optional[str]:
@@ -36,11 +36,11 @@ def get_active_window_info() -> Dict[str, str]:
"title": "...",
"app_name": "..."
}
Détecte automatiquement l'OS et utilise la méthode appropriée.
"""
system = platform.system()
if system == "Linux":
return _get_window_info_linux()
elif system == "Windows":
@@ -51,6 +51,32 @@ def get_active_window_info() -> Dict[str, str]:
return {"title": "unknown_window", "app_name": "unknown_app"}
def get_active_window_rect() -> Optional[Dict[str, Any]]:
"""
Renvoie le rectangle de la fenêtre active :
{
"title": "...",
"app_name": "...",
"rect": [left, top, right, bottom],
"position": [left, top],
"size": [width, height],
"hwnd": int # Windows uniquement
}
Retourne None si la fenêtre est introuvable ou minimisée.
Détecte automatiquement l'OS et utilise la méthode appropriée.
"""
system = platform.system()
if system == "Windows":
return _get_window_rect_windows()
elif system == "Linux":
return _get_window_rect_linux()
elif system == "Darwin":
return _get_window_rect_macos()
return None
def _get_window_info_linux() -> Dict[str, str]:
"""
Linux: utilise xdotool (X11)
@@ -178,6 +204,163 @@ def _get_window_info_macos() -> Dict[str, str]:
}
def _get_window_rect_windows() -> Optional[Dict[str, Any]]:
"""
Windows : utilise pywin32 pour obtenir le rectangle de la fenêtre active.
Retourne None si la fenêtre est minimisée (icônifiée) ou si pywin32 manque.
"""
try:
import win32gui
import win32process
import psutil
hwnd = win32gui.GetForegroundWindow()
if not hwnd:
return None
# Ignorer les fenêtres minimisées (pas de contenu visible)
if win32gui.IsIconic(hwnd):
return None
title = win32gui.GetWindowText(hwnd) or "unknown_window"
# Rectangle de la fenêtre (coordonnées écran absolues)
left, top, right, bottom = win32gui.GetWindowRect(hwnd)
width = right - left
height = bottom - top
# Ignorer les fenêtres de taille nulle ou absurde
if width <= 0 or height <= 0:
return None
# Nom du processus
_, pid = win32process.GetWindowThreadProcessId(hwnd)
try:
app_name = psutil.Process(pid).name()
except Exception:
app_name = "unknown_app"
return {
"title": title,
"app_name": app_name,
"rect": [left, top, right, bottom],
"position": [left, top],
"size": [width, height],
"hwnd": hwnd,
}
except ImportError:
return None
except Exception:
return None
def _get_window_rect_linux() -> Optional[Dict[str, Any]]:
"""
Linux (X11) : utilise xdotool + xwininfo pour obtenir le rectangle.
Nécessite : sudo apt-get install xdotool x11-utils
"""
try:
# Identifiant de la fenêtre active
wid = _run_cmd(["xdotool", "getactivewindow"])
if not wid:
return None
title = _run_cmd(["xdotool", "getactivewindow", "getwindowname"]) or "unknown_window"
pid_str = _run_cmd(["xdotool", "getactivewindow", "getwindowpid"])
app_name = "unknown_app"
if pid_str:
app_name = _run_cmd(["ps", "-p", pid_str.strip(), "-o", "comm="]) or "unknown_app"
# Géométrie via xdotool --shell (position + taille)
geom_raw = _run_cmd(["xdotool", "getwindowgeometry", "--shell", wid])
if not geom_raw:
return None
vals: Dict[str, int] = {}
for line in geom_raw.strip().splitlines():
if "=" in line:
k, v = line.split("=", 1)
try:
vals[k.strip()] = int(v.strip())
except ValueError:
pass
if not {"X", "Y", "WIDTH", "HEIGHT"} <= vals.keys():
return None
x, y = vals["X"], vals["Y"]
w, h = vals["WIDTH"], vals["HEIGHT"]
return {
"title": title,
"app_name": app_name,
"rect": [x, y, x + w, y + h],
"position": [x, y],
"size": [w, h],
}
except Exception:
return None
def _get_window_rect_macos() -> Optional[Dict[str, Any]]:
"""
macOS : utilise Quartz (CGWindowListCopyWindowInfo) pour obtenir le rectangle.
Nécessite : pip install pyobjc-framework-Quartz
"""
try:
from AppKit import NSWorkspace
from Quartz import (
CGWindowListCopyWindowInfo,
kCGWindowListOptionOnScreenOnly,
kCGNullWindowID,
)
active_app = NSWorkspace.sharedWorkspace().activeApplication()
app_name = active_app.get("NSApplicationName", "unknown_app")
window_list = CGWindowListCopyWindowInfo(
kCGWindowListOptionOnScreenOnly, kCGNullWindowID
)
for window in window_list:
owner_name = window.get("kCGWindowOwnerName", "")
if owner_name != app_name:
continue
bounds = window.get("kCGWindowBounds")
if not bounds:
continue
x = int(bounds.get("X", 0))
y = int(bounds.get("Y", 0))
w = int(bounds.get("Width", 0))
h = int(bounds.get("Height", 0))
if w <= 0 or h <= 0:
continue
title = window.get("kCGWindowName", "unknown_window") or "unknown_window"
return {
"title": title,
"app_name": app_name,
"rect": [x, y, x + w, y + h],
"position": [x, y],
"size": [w, h],
}
except ImportError:
return None
except Exception:
return None
return None
# Test rapide
if __name__ == "__main__":
import time
@@ -185,8 +368,13 @@ if __name__ == "__main__":
print(f"OS détecté: {platform.system()}")
print("\nTest de capture fenêtre active (5 secondes)...")
print("Changez de fenêtre pour tester!\n")
for i in range(5):
info = get_active_window_info()
rect = get_active_window_rect()
print(f"[{i+1}] App: {info['app_name']:20s} | Title: {info['title']}")
if rect:
print(f" Rect: {rect['rect']} | Size: {rect['size']}")
else:
print(" Rect: non disponible")
time.sleep(1)