193 lines
6.8 KiB
PHP
193 lines
6.8 KiB
PHP
<?php
|
|
// V77 WEVIA Coherence + Gap Analyzer
|
|
// Detects inconsistencies between declared vs actual counts
|
|
// Allows WEVIA Master to self-report issues and trigger fixes
|
|
|
|
header("Content-Type: application/json");
|
|
$action = $_REQUEST["action"] ?? "check";
|
|
|
|
function safe_json($path) {
|
|
if (!is_readable($path)) return null;
|
|
return json_decode(@file_get_contents($path), true);
|
|
}
|
|
|
|
function http_json($url, $timeout = 5) {
|
|
$ch = curl_init();
|
|
curl_setopt_array($ch, [
|
|
CURLOPT_URL => $url,
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_TIMEOUT => $timeout,
|
|
CURLOPT_SSL_VERIFYPEER => false,
|
|
CURLOPT_HTTPHEADER => ["Host: weval-consulting.com"]
|
|
]);
|
|
$body = curl_exec($ch);
|
|
curl_close($ch);
|
|
return $body ? json_decode($body, true) : null;
|
|
}
|
|
|
|
if ($action === "check") {
|
|
$issues = [];
|
|
|
|
// Check 1: gaps hardcoded vs reality
|
|
$agent_factory = http_json("http://127.0.0.1/api/wevia-agent-factory.php?action=count");
|
|
$real_gaps = $agent_factory["total_missing"] ?? 0;
|
|
$stubs_count = count(glob("/var/www/html/api/agent-stubs/agent_*.php"));
|
|
|
|
// Scan all pages for hardcoded gap numbers
|
|
$pages = glob("/var/www/html/*.html");
|
|
$gap_patterns = ["47 gaps", "54 gaps", "150 gaps"];
|
|
$hardcoded_gaps = [];
|
|
foreach ($pages as $p) {
|
|
$c = @file_get_contents($p);
|
|
if (!$c) continue;
|
|
foreach ($gap_patterns as $pat) {
|
|
if (strpos($c, $pat) !== false) {
|
|
$hardcoded_gaps[] = ["file" => basename($p), "pattern" => $pat];
|
|
}
|
|
}
|
|
}
|
|
|
|
if (count($hardcoded_gaps) > 0) {
|
|
$issues[] = [
|
|
"type" => "hardcoded_gaps_vs_reality",
|
|
"reality" => $real_gaps,
|
|
"stubs_created" => $stubs_count,
|
|
"hardcoded_instances" => $hardcoded_gaps
|
|
];
|
|
}
|
|
|
|
// Check 2: mega aggregator says X, training UI says Y
|
|
$mega = http_json("http://127.0.0.1/api/wevia-mega-agents.php?action=counts");
|
|
$orch = http_json("http://127.0.0.1/api/wevia-orchestration-v75.php?action=summary");
|
|
|
|
// Check 3: Registry tools count drift
|
|
$reg = safe_json("/var/www/html/api/wevia-tool-registry.json");
|
|
$reg_count = count($reg["tools"] ?? $reg ?? []);
|
|
|
|
// Check 4: Test layer freshness
|
|
$layers = ["nonreg", "nonreg-reg67", "nonreg-reg68", "nonreg-reg69", "nonreg-reg70", "nonreg-reg71", "v74-e2e", "v75-deep-e2e", "v76-chrome-e2e"];
|
|
$test_coherence = [];
|
|
foreach ($layers as $l) {
|
|
$f = "/var/www/html/api/" . $l . "-latest.json";
|
|
if (file_exists($f)) {
|
|
$d = json_decode(file_get_contents($f), true);
|
|
$test_coherence[$l] = [
|
|
"pass" => $d["pass"] ?? 0,
|
|
"total" => $d["total"] ?? 0,
|
|
"score" => $d["score"] ?? 0,
|
|
"age_hours" => round((time() - filemtime($f)) / 3600, 1)
|
|
];
|
|
if (($d["score"] ?? 0) < 100) {
|
|
$issues[] = [
|
|
"type" => "test_layer_regression",
|
|
"layer" => $l,
|
|
"score" => $d["score"],
|
|
"fails" => $d["fail"] ?? 0
|
|
];
|
|
}
|
|
if (((time() - filemtime($f)) / 3600) > 72) {
|
|
$issues[] = [
|
|
"type" => "test_layer_stale",
|
|
"layer" => $l,
|
|
"age_hours" => round((time() - filemtime($f)) / 3600, 1)
|
|
];
|
|
}
|
|
}
|
|
}
|
|
|
|
// Check 5: KPI cards showing 0 on tech platform
|
|
$v64 = http_json("http://127.0.0.1/api/wevia-v64-departments-kpi.php");
|
|
$depts_total = 0;
|
|
$depts_zero_kpis = 0;
|
|
if ($v64 && isset($v64["departments"])) {
|
|
foreach ($v64["departments"] as $dept) {
|
|
$depts_total++;
|
|
$all_zero = true;
|
|
if (isset($dept["kpis"])) {
|
|
foreach ($dept["kpis"] as $k) {
|
|
if (($k["current"] ?? 0) != 0) { $all_zero = false; break; }
|
|
}
|
|
}
|
|
if ($all_zero) $depts_zero_kpis++;
|
|
}
|
|
}
|
|
if ($depts_zero_kpis > $depts_total * 0.3) {
|
|
$issues[] = [
|
|
"type" => "kpi_cards_mostly_zero",
|
|
"depts_zero" => $depts_zero_kpis,
|
|
"depts_total" => $depts_total,
|
|
"pct" => round(100 * $depts_zero_kpis / max(1, $depts_total), 1)
|
|
];
|
|
}
|
|
|
|
echo json_encode([
|
|
"ok" => true,
|
|
"version" => "V77-coherence",
|
|
"ts" => date("c"),
|
|
"total_issues" => count($issues),
|
|
"issues" => $issues,
|
|
"facts" => [
|
|
"gaps_reality" => $real_gaps,
|
|
"stubs_created" => $stubs_count,
|
|
"registry_tools" => $reg_count,
|
|
"agents_structured" => $mega["total_aggregated"] ?? null,
|
|
"agents_declared" => $mega["manifest_total_live"] ?? null,
|
|
"depts_total" => $depts_total,
|
|
"depts_with_zero_kpis" => $depts_zero_kpis
|
|
],
|
|
"test_layers" => $test_coherence
|
|
], JSON_PRETTY_PRINT);
|
|
exit;
|
|
}
|
|
|
|
if ($action === "fix_gaps_ui") {
|
|
// Fix all hardcoded "47 gaps" and "54 gaps" in HTML files to match reality
|
|
$agent_factory = http_json("http://127.0.0.1/api/wevia-agent-factory.php?action=count");
|
|
$real = $agent_factory["already_created"] ?? 0;
|
|
|
|
$patched = [];
|
|
foreach (glob("/var/www/html/*.html") as $p) {
|
|
$c = @file_get_contents($p);
|
|
if (!$c) continue;
|
|
$orig = $c;
|
|
$c = str_replace("47 gaps", $real . " gaps", $c);
|
|
$c = str_replace("54 gaps", $real . " gaps", $c);
|
|
if ($c !== $orig) {
|
|
if (@file_put_contents($p, $c) !== false) {
|
|
$patched[] = basename($p);
|
|
}
|
|
}
|
|
}
|
|
echo json_encode(["ok" => true, "patched" => $patched, "target" => $real], JSON_PRETTY_PRINT);
|
|
exit;
|
|
}
|
|
|
|
if ($action === "scan_kpi_zeros") {
|
|
// Scan all KPI cards showing 0 and categorize by need
|
|
$v64 = http_json("http://127.0.0.1/api/wevia-v64-departments-kpi.php");
|
|
$zeros = [];
|
|
if ($v64 && isset($v64["departments"])) {
|
|
foreach ($v64["departments"] as $dept) {
|
|
foreach (($dept["kpis"] ?? []) as $kpi) {
|
|
if (($kpi["current"] ?? 0) == 0 && ($kpi["target"] ?? 0) > 0) {
|
|
$zeros[] = [
|
|
"dept" => $dept["label"] ?? $dept["id"] ?? "?",
|
|
"kpi" => $kpi["label"] ?? "?",
|
|
"target" => $kpi["target"] ?? "?",
|
|
"unit" => $kpi["unit"] ?? "?",
|
|
"gap_agent" => $kpi["gap_agent"] ?? null
|
|
];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
echo json_encode([
|
|
"ok" => true,
|
|
"total_zero_kpis" => count($zeros),
|
|
"zeros" => $zeros
|
|
], JSON_PRETTY_PRINT);
|
|
exit;
|
|
}
|
|
|
|
echo json_encode(["ok" => false, "error" => "unknown action", "valid" => ["check", "fix_gaps_ui", "scan_kpi_zeros"]]);
|