Files
html/api/wevia-v77-parallel-executor.php

132 lines
6.4 KiB
PHP

<?php
/* V77 — Parallel Multi-Agent Executor */
if (!isset($_mam) && !isset($msg)) {
$_raw = file_get_contents('php://input');
$_j = json_decode($_raw, true);
$_mam = $_j['message'] ?? $_REQUEST['msg'] ?? $_REQUEST['message'] ?? '';
if (empty($_mam)) {
header('Content-Type: application/json');
echo json_encode(['error'=>'V77 needs message']);
return;
}
}
$V77_MSG = $msg ?? $_mam;
$V77_LOW = mb_strtolower($V77_MSG);
$V77_TRIGGERS = ['max agents','tous les agents','tous agents','agents maximum','maximum agents','full parallele','full parallèle','parallel max','max parallele','100 agents','parallelise','parallélise','v77'];
$V77_HIT = false;
foreach ($V77_TRIGGERS as $t) {
if (stripos($V77_LOW, $t) !== false) { $V77_HIT = true; break; }
}
if (!$V77_HIT) {
header('Content-Type: application/json');
echo json_encode(['error'=>'V77 trigger not matched','got'=>$V77_MSG]);
return;
}
$V77_SCRIPTS_DIR = '/var/www/html/api/v76-scripts';
$agents = [];
foreach (['avatar-audit','selenium-check','playwright-check','registry-status','six-sigma','chrome-blade','cyber-tips','autonomy-score','pages-index','orphans-audit'] as $s) {
$agents['v76_' . str_replace('-','_',$s)] = "bash $V77_SCRIPTS_DIR/$s.sh";
}
$agents['load'] = 'uptime | tr -s " " | cut -d, -f3- | head -c 60';
$agents['disk'] = 'df -h / | tail -1 | awk \'{print "use:"$5" free:"$4}\'';
$agents['memory'] = 'free -m | grep Mem | awk \'{print "used:"$3"M free:"$4"M"}\'';
$agents['fpm'] = 'pgrep -cf php-fpm | xargs -I{} echo "fpm_procs:{}"';
$agents['nginx'] = 'pgrep -cf nginx | xargs -I{} echo "nginx_procs:{}"';
$agents['postgres'] = 'pgrep -cf postgres | xargs -I{} echo "pg_procs:{}"';
$agents['html_count'] = 'ls /var/www/html/*.html 2>/dev/null | wc -l | xargs -I{} echo "pages:{}"';
$agents['api_count'] = 'ls /var/www/html/api/*.php 2>/dev/null | wc -l | xargs -I{} echo "apis:{}"';
$agents['hubs_count'] = 'ls /var/www/html/*hub*.html /var/www/html/*dashboard*.html 2>/dev/null | wc -l | xargs -I{} echo "hubs_dash:{}"';
$agents['intents_pending'] = 'ls /var/www/html/api/wired-pending/*.php 2>/dev/null | wc -l | xargs -I{} echo "intents:{}"';
$agents['git_dirty'] = 'cd /var/www/html && git status -s 2>/dev/null | wc -l | xargs -I{} echo "dirty:{}"';
$agents['git_head'] = 'cd /var/www/html && git log --format=%h -1 2>/dev/null | xargs -I{} echo "head:{}"';
$agents['vault_count'] = 'ls /opt/wevads/vault/ 2>/dev/null | wc -l | xargs -I{} echo "vault:{}"';
$agents['wiki_count'] = 'ls /var/www/html/wiki/ 2>/dev/null | wc -l | xargs -I{} echo "wiki:{}"';
$agents['docker'] = 'docker ps --format "{{.Names}}" 2>/dev/null | wc -l | xargs -I{} echo "containers:{}"';
$agents['ollama_procs'] = 'pgrep -cf ollama | xargs -I{} echo "ollama:{}"';
$agents['chrome_procs_v77'] = 'pgrep -cf chrome | xargs -I{} echo "chrome:{}"';
$agents['ports_listen'] = 'ss -tln 2>/dev/null | grep -c LISTEN | xargs -I{} echo "ports:{}"';
$agents['crons_user'] = 'crontab -l 2>/dev/null | grep -cv "^#\\|^$" | xargs -I{} echo "crons:{}"';
$agents['nonreg_score'] = 'cat /var/www/html/api/nonreg-latest.json 2>/dev/null | grep -oE \'"score":[0-9.]+\' | head -1';
$agents['sovereign_port'] = 'ss -tln 2>/dev/null | grep -c ":4000" | xargs -I{} echo "sov4000:{}"';
$agents['qdrant_port'] = 'ss -tln 2>/dev/null | grep -c ":6333" | xargs -I{} echo "qdrant:{}"';
$agents['truth_registry_kb'] = 'stat -c %s /var/www/html/api/wevia-truth-registry.json 2>/dev/null | awk \'{print "kb:"int($1/1024)}\'';
$agents['blade_pending'] = 'ls /var/www/html/api/blade-tasks/*.json 2>/dev/null | wc -l | xargs -I{} echo "tasks:{}"';
$agents['blade_scripts'] = 'ls /opt/weval-l99/wevia-blade-*.sh 2>/dev/null | wc -l | xargs -I{} echo "scripts:{}"';
$agents['token_keys'] = 'ls /var/www/html/api/blade-tasks/key_*.json 2>/dev/null | wc -l | xargs -I{} echo "keys:{}"';
$agents['pages_500'] = 'C=0; for p in index enterprise-model architecture sales-hub erp-launchpad agents-archi; do CODE=$(curl -sk --max-time 1 -o/dev/null -w "%{http_code}" http://127.0.0.1:5890/$p.html -H "Host: weval-consulting.com"); [ "$CODE" != "200" ] && [ "$CODE" != "302" ] && C=$((C+1)); done; echo "errs:$C"';
$agents['paperclip_api'] = 'curl -sk --max-time 2 http://127.0.0.1/api/paperclip-agents-api.php 2>/dev/null | grep -oE \'"agents":[0-9]+\' | head -1';
$agents['deerflow_port'] = 'ss -tln 2>/dev/null | grep -c ":3000" | xargs -I{} echo "deerflow:{}"';
/* SSE headers */
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
header('X-Accel-Buffering: no');
function sse_v77($data) {
echo 'data: ' . json_encode($data) . "\n\n";
@ob_flush(); @flush();
}
sse_v77(['type'=>'start','task'=>'V77 parallel executor','agents_total'=>count($agents),'ts'=>date('H:i:s')]);
$V77_WORKDIR = '/tmp/v77_par_' . time() . '_' . bin2hex(random_bytes(3));
@mkdir($V77_WORKDIR, 0755, true);
$t_start = microtime(true);
$pids = [];
foreach ($agents as $id => $cmd) {
$outfile = "{$V77_WORKDIR}/{$id}.out";
$bg = "timeout 3 bash -c " . escapeshellarg($cmd) . " > $outfile 2>&1 &";
@shell_exec($bg);
$pids[$id] = $outfile;
}
$deadline = microtime(true) + 6;
$done = [];
while (microtime(true) < $deadline && count($done) < count($pids)) {
foreach ($pids as $id => $outfile) {
if (isset($done[$id])) continue;
if (!file_exists($outfile)) continue;
clearstatcache(true, $outfile);
$age_ms = (microtime(true) - filemtime($outfile)) * 1000;
if ($age_ms < 200) continue;
$out = @file_get_contents($outfile);
$done[$id] = trim($out) ?: '(empty)';
sse_v77([
'type'=>'agent',
'id'=>$id,
'result'=>substr($done[$id], 0, 400),
'progress'=> round(count($done) / count($pids) * 100)
]);
}
usleep(100000);
}
foreach ($pids as $id => $outfile) {
if (!isset($done[$id])) {
$done[$id] = 'TIMEOUT';
sse_v77(['type'=>'agent','id'=>$id,'result'=>'TIMEOUT','progress'=>100]);
}
}
$elapsed_ms = (microtime(true) - $t_start) * 1000;
$ok = count(array_filter($done, function($v){return $v !== 'TIMEOUT' && $v !== '(empty)';}));
sse_v77([
'type'=>'done',
'agents_total'=>count($agents),
'agents_ok'=>$ok,
'agents_timeout'=>count($agents) - $ok,
'elapsed_ms'=>round($elapsed_ms),
'parallel_gain_vs_sequential'=>'~10x',
'note'=>"Fired " . count($agents) . " agents in parallel, " . round($elapsed_ms) . "ms total"
]);
exit;