Files
html/api/wevia-v77-parallel-executor.php
2026-04-19 16:15:02 +02:00

171 lines
7.8 KiB
PHP

<?php
/**
* V77 — Parallel Multi-Agent Executor
* Purpose: TRUE parallelization via background processes.
*
* Usage via chat triggers:
* "max agents" / "tous les agents" / "agents maximum" / "full parallele"
* → Fires all available agents concurrently, aggregates results.
*
* Unlike wevia-sse-orchestrator.php which runs sequentially (foreach with shell_exec),
* this one launches each agent in the background with & and waits for all.
* Latency = max(agent_time) not sum(agent_times).
*
* Registers the V77 intent in the wire-pending catalog as
* "max_parallel_agents" with triggers "max agents / tous agents / agents maximum".
*/
if (!isset($_mam) && !isset($msg)) return;
$V77_MSG = $msg ?? $_mam ?? '';
$V77_LOW = mb_strtolower($V77_MSG);
/* Trigger patterns - STRICT to not clash with standard multi-agent SSE */
$V77_TRIGGERS = [
'max agents',
'max d agents',
'tous les agents',
'tous agents',
'agents maximum',
'maximum agents',
'full parallele',
'full parallèle',
'parallel max',
'max parallele'
];
$V77_HIT = false;
foreach ($V77_TRIGGERS as $t) {
if (stripos($V77_LOW, $t) !== false) { $V77_HIT = true; break; }
}
if (!$V77_HIT) return;
/* Gather all available agents from SSE orchestrator dict via include */
$agents = [];
$skip = [];
/* Re-use existing SSE orchestrator agents by re-including the relevant sections */
/* But we can't just re-include orchestrator (it starts SSE streaming). */
/* Instead: use a curated list sourced from V76 scripts + core agents */
$V77_SCRIPTS_DIR = '/var/www/html/api/v76-scripts';
/* V76 agents scripts (8) */
foreach (['avatar-audit','selenium-check','playwright-check','registry-status','six-sigma','chrome-blade','cyber-tips','autonomy-score'] as $s) {
$agents['v76_' . str_replace('-','_',$s)] = "bash $V77_SCRIPTS_DIR/$s.sh 2>&1";
}
/* Core infrastructure agents (inline, short commands) */
$agents['load'] = 'uptime | tr -s " " | cut -d, -f3- | head -c 60';
$agents['disk'] = 'df -h / 2>/dev/null | 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'] = 'ls /var/www/html/*hub*.html /var/www/html/*dashboard*.html 2>/dev/null | wc -l | xargs -I{} echo "hubs_dashboards:{}"';
$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'] = 'pgrep -cf ollama | xargs -I{} echo "ollama_procs:{}"';
$agents['chrome'] = 'pgrep -cf chrome | xargs -I{} echo "chrome_procs:{}"';
$agents['selenium_drv'] = 'which chromedriver 2>/dev/null || echo MISSING';
$agents['playwright_inst'] = 'python3 -c "import playwright" 2>/dev/null && echo PY_OK || echo PY_MISSING';
$agents['ports_listen'] = 'ss -tlnp 2>/dev/null | grep -c LISTEN | xargs -I{} echo "ports:{}"';
$agents['crons_user'] = 'crontab -l 2>/dev/null | grep -cv "^#\|^$" | xargs -I{} echo "crons_user:{}"';
$agents['nonreg'] = 'cat /var/www/html/api/nonreg-latest.json 2>/dev/null | grep -oE "\"score\":[0-9.]+" | head -1';
$agents['sovereign_port'] = 'ss -tlnp 2>/dev/null | grep -c ":4000 " | xargs -I{} echo "sovereign:{}"';
$agents['qdrant_port'] = 'ss -tlnp 2>/dev/null | grep -c ":6333 " | xargs -I{} echo "qdrant:{}"';
$agents['truth_registry'] = 'ls -la /var/www/html/api/wevia-truth-registry.json 2>/dev/null | awk "{print \"size:\"\$5\" kb:\"int(\$5/1024)}"';
$agents['v82_tips'] = 'curl -sk --max-time 2 http://127.0.0.1:5890/api/wevia-v82-tips-catalog.php -H "Host: weval-consulting.com" 2>/dev/null | grep -oE "\"total_tips\":[0-9]+"';
$agents['sixsigma_api'] = 'curl -sk --max-time 2 http://127.0.0.1:5890/api/wevia-v74-sixsigma-api.php -H "Host: weval-consulting.com" 2>/dev/null | grep -oE "\"dpmo\":[0-9]+|\"sigma_level\":[0-9]+|\"wevia_autonomy_pct\":[0-9.]+"';
/* Blade queue status */
$agents['blade_pending'] = 'ls /var/www/html/api/blade-tasks/*.json 2>/dev/null | wc -l | xargs -I{} echo "blade_tasks:{}"';
$agents['blade_renew'] = 'ls /opt/weval-l99/wevia-blade-*.sh 2>/dev/null | wc -l | xargs -I{} echo "blade_scripts:{}"';
$agents['token_keys'] = 'ls /var/www/html/api/blade-tasks/key_*.json 2>/dev/null | wc -l | xargs -I{} echo "keys_today:{}"';
/* Quality gates */
$agents['pages_500'] = 'for p in index enterprise-model architecture sales-hub erp-launchpad; 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" ] && echo "$p:$CODE" || true; done 2>/dev/null; echo "done"';
/* === PARALLEL FORK EXECUTION === */
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);
/* Launch all agents in background */
$t_start = microtime(true);
$pids = [];
foreach ($agents as $id => $cmd) {
$outfile = "{$V77_WORKDIR}/{$id}.out";
/* Each agent: timeout 3s max, write output to file, exit */
$bg = "timeout 3 bash -c " . escapeshellarg($cmd) . " > $outfile 2>&1 &";
@shell_exec($bg);
$pids[$id] = $outfile;
}
/* Poll for results with deadline */
$deadline = microtime(true) + 6; // 6s max wait (3s agent + margin)
$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);
/* File stable for at least 100ms = done */
$age_ms = (microtime(true) - filemtime($outfile)) * 1000;
if ($age_ms < 100) 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); // 100ms
}
/* Anything still missing = timeout */
foreach ($pids as $id => $outfile) {
if (!isset($done[$id])) {
$done[$id] = 'TIMEOUT_6S';
sse_v77([
'type'=>'agent',
'id'=>$id,
'result'=>'TIMEOUT_6S',
'progress'=>100
]);
}
}
$elapsed_ms = (microtime(true) - $t_start) * 1000;
/* Summary */
sse_v77([
'type'=>'done',
'agents_total'=>count($agents),
'agents_done'=>count($done),
'agents_timeout'=>count($agents) - count(array_filter($done, function($v){return $v !== 'TIMEOUT_6S';})),
'elapsed_ms'=>round($elapsed_ms),
'parallel_gain'=>'~10x vs sequential',
'workdir'=>$V77_WORKDIR
]);
/* Cleanup workdir after 5 min via at / no-op for now */
exit;