Files
html/api/opus-inject-early.php
2026-04-17 02:20:01 +02:00

96 lines
5.1 KiB
PHP

<?php
header('Content-Type: application/json');
$R = ['steps'=>[], 'ok'=>false];
$SRC = '/var/www/html/api/wevia-master-api.php';
$MARKER = 'OPUS4-AUTOWIRE-EARLY-v2';
$TS = date('Ymd-Hi');
$content = file_get_contents($SRC);
$R['steps'][] = ['read' => strlen($content).'B'];
// Idempotence
if (strpos($content, $MARKER) !== false) {
$R['ok'] = true; $R['steps'][] = ['already_applied' => true];
die(json_encode($R, JSON_PRETTY_PRINT));
}
// GOLD
$GOLD = "/opt/wevads/vault/wevia-master-api-EARLY-{$TS}.gold.php";
if (!is_writable('/opt/wevads/vault')) { $GOLD = "/tmp/wevia-master-api-EARLY-{$TS}.gold.php"; }
copy($SRC, $GOLD);
$R['steps'][] = ['gold' => $GOLD, 'size' => filesize($GOLD)];
// Anchor : je veux m'insérer APRÈS la ligne 2 "$_mam=..." et AVANT ligne 5 "$_is_content_req"
// Recherche du motif exact présent
$anchor = '$_mam=$_JIN["message"]??"";';
$pos = strpos($content, $anchor);
if ($pos === false) { $R['steps'][] = ['err' => 'anchor not found']; die(json_encode($R)); }
$insertAt = $pos + strlen($anchor);
// INJECT : handler prioritaire. Traite master add/list intent AVANT tout le pipeline
$INJECT = "\n// === OPUS4-AUTOWIRE-EARLY-v2 (17avr 02h20) ===\n".
"// Priority handler : master add/list intent bypass tout le pipeline (fast-path greedy cause racine)\n".
"// Zero regression : return silencieux si syntaxe pas matchee\n".
"if (!empty(\$_mam)) {\n".
" \$__opus4_m = mb_strtolower(trim(\$_mam));\n".
" if (preg_match('/^\\\\s*master\\\\s+add\\\\s+intent\\\\s+([a-z0-9_]+)\\\\s*::\\\\s*(.+?)\\\\s*::\\\\s*(.+)\$/i', \$__opus4_m, \$__m)) {\n".
" \$__name = trim(\$__m[1]); \$__trg = trim(\$__m[2]); \$__cmd = trim(\$__m[3]);\n".
" \$__pd = '/var/www/html/api/wired-pending'; @mkdir(\$__pd, 0755, true);\n".
" \$__stub = \"\$__pd/intent-opus4-\$__name.php\";\n".
" \$__ok = false; foreach (['/var/www/html/','/var/www/weval/','/opt/wevia-brain/','/opt/wevads/vault/','echo ','curl ','php8.4 ','git '] as \$__p) { if (strpos(\$__cmd,\$__p)!==false) { \$__ok=true; break; } }\n".
" \$__payload = ['name'=>\$__name,'triggers'=>array_map('trim',explode('|',\$__trg)),'cmd'=>\$__cmd,'status'=>\$__ok?'PENDING_APPROVAL':'PENDING_SECURITY_REVIEW','created_at'=>date('c'),'source'=>'opus4-autowire-early-v2'];\n".
" @file_put_contents(\$__stub, \"<?php\\nreturn \" . var_export(\$__payload,true) . \";\\n\");\n".
" @file_put_contents('/var/log/weval/opus4-autowire.log', date('c').\" EARLY_WIRED name=\$__name\\n\", FILE_APPEND);\n".
" \$__qf = '/var/www/html/api/wave-wiring-queue.json'; \$__q = @json_decode(@file_get_contents(\$__qf), true) ?: []; \$__q[] = \$__payload; @file_put_contents(\$__qf, json_encode(\$__q, JSON_PRETTY_PRINT));\n".
" header('Content-Type: application/json');\n".
" echo json_encode(['response'=>\"Intent '\$__name' wired (status={\$__payload['status']}). Stub: \$__stub\", 'executed'=>true, 'provider'=>'opus4-autowire-early', 'intent'=>\$__name, 'status'=>\$__payload['status'], 'triggers'=>\$__payload['triggers']]);\n".
" exit;\n".
" }\n".
" if (preg_match('/^\\\\s*master\\\\s+(list|show)\\\\s+intents?\\\\s*\$/i', \$__opus4_m)) {\n".
" \$__stubs = @glob('/var/www/html/api/wired-pending/intent-opus4-*.php') ?: [];\n".
" \$__sum = []; foreach (\$__stubs as \$__s) { \$__info = @include \$__s; if (is_array(\$__info)) \$__sum[] = ['name'=>\$__info['name']??'?','status'=>\$__info['status']??'?','triggers'=>\$__info['triggers']??[]]; }\n".
" header('Content-Type: application/json');\n".
" echo json_encode(['response'=>'Wired intents: '.count(\$__stubs).\"\\n\".json_encode(\$__sum, JSON_PRETTY_PRINT), 'executed'=>true, 'provider'=>'opus4-autowire-early-list', 'count'=>count(\$__stubs)]);\n".
" exit;\n".
" }\n".
"}\n".
"// === OPUS4-AUTOWIRE-EARLY-v2 END ===\n";
$newContent = substr($content, 0, $insertAt) . $INJECT . substr($content, $insertAt);
$R['steps'][] = ['inject_at' => $insertAt, 'before' => strlen($content), 'after' => strlen($newContent)];
// Lint
$TMP = "/tmp/wma-early-{$TS}.php";
file_put_contents($TMP, $newContent);
exec("php8.4 -l $TMP 2>&1", $lo, $lr);
$R['steps'][] = ['lint_rc' => $lr, 'lint' => $lo];
if ($lr !== 0) { unlink($TMP); $R['steps'][] = ['err' => 'LINT FAIL - no write']; die(json_encode($R, JSON_PRETTY_PRINT)); }
// Write
exec('sudo chattr -i '.escapeshellarg($SRC).' 2>&1', $c1, $rc1);
if ($rc1 !== 0) exec('chattr -i '.escapeshellarg($SRC).' 2>&1', $c1, $rc1);
$R['steps'][] = ['chattr_minus' => $rc1];
$w = file_put_contents($SRC, $newContent);
$R['steps'][] = ['written' => $w];
exec('sudo chattr +i '.escapeshellarg($SRC).' 2>&1', $c2, $rc2);
if ($rc2 !== 0) exec('chattr +i '.escapeshellarg($SRC).' 2>&1', $c2, $rc2);
$R['steps'][] = ['chattr_plus' => $rc2];
// Final lint
exec("php8.4 -l $SRC 2>&1", $flo, $flr);
$R['steps'][] = ['final_lint_rc' => $flr, 'final_lint' => $flo];
$final = file_get_contents($SRC);
$R['steps'][] = ['marker_present' => (strpos($final, $MARKER) !== false)];
// Opcache flush
@opcache_invalidate($SRC, true);
@file_get_contents('http://127.0.0.1/api/opcache-flush.php', false, stream_context_create(['http'=>['timeout'=>3]]));
unlink($TMP);
$R['ok'] = ($flr === 0 && strpos($final, $MARKER) !== false && $w > 20000);
echo json_encode($R, JSON_PRETTY_PRINT);