Files
html/api/opus-wire-guard-patcher.php
opus af6f9be5c8
Some checks failed
WEVAL NonReg / nonreg (push) Has been cancelled
auto-sync-all
2026-04-17 02:09:37 +02:00

105 lines
4.6 KiB
PHP

<?php
// PATCHER wevia-infra-intercept.php — ajoute guard anti-greedy
// Cause racine : fast-path intercepte "master add intent", "::" syntax, et phrases multi-action
// Doctrines: #2 zero-reg, #3 GOLD, #5 séquence, #12 Opus intervient quand WEVIA incapacitée, #13 root cause, #21 PHP only
header('Content-Type: application/json');
$R = ['steps'=>[], 'ok'=>false];
$SRC = '/var/www/weval/wevia-ia/wevia-infra-intercept.php';
$VAULT = '/opt/wevads/vault';
$TS = date('Ymd-Hi');
$GOLD = "$VAULT/wevia-infra-intercept-{$TS}.gold.php";
// Marker unique (évite double application)
$MARKER = 'OPUS4-AUTONOMY-GUARD-v1';
// Step 0 : sanity
if (!file_exists($SRC)) { $R['steps'][]=['err'=>'src missing']; die(json_encode($R)); }
$content = file_get_contents($SRC);
$R['steps'][]=['read'=>strlen($content).'B'];
// Step 1 : idempotence
if (strpos($content, $MARKER) !== false) {
$R['ok']=true; $R['steps'][]=['already_applied'=>true];
die(json_encode($R, JSON_PRETTY_PRINT));
}
// Step 2 : GOLD
if (!is_dir($VAULT) || !is_writable($VAULT)) {
$GOLD = "/tmp/wevia-infra-intercept-{$TS}.gold.php";
}
if (!copy($SRC, $GOLD)) { $R['steps'][]=['err'=>"gold failed to $GOLD"]; die(json_encode($R)); }
$R['steps'][]=['gold'=>$GOLD, 'size'=>filesize($GOLD)];
// Step 3 : construire le guard
// Règle 1 : syntaxe Wave 128 "master add/list intent" → return (laisse master-router.php gérer)
// Règle 2 : "::" double séparateur (Wave 128 pattern) → return
// Règle 3 : messages avec chemins absolus + verbes d'action multiples → log et return
// Règle 4 : message commence par "opus " → forcer passage master-router (bypass greedy)
$GUARD = <<<'GUARD'
// === OPUS4-AUTONOMY-GUARD-v1 (17avr) ===
// Fix cause racine manque autonomie WEVIA : fast-path greedy interceptait
// syntaxe auto-wire (Wave 128) et phrases multi-action -> renvoyait LLM fallback qui hallucinait.
// Ce guard ROUTE vers master-router.php (en aval) pour 3 patterns structurants.
// ZERO regression : uniquement des returns additionnels, aucune logique existante modifiee.
if (preg_match('/^\s*master\s+(add|list|show)\s+intent/i', $_wm)) {
@file_put_contents('/var/log/weval/opus4-autowire.log', date('c')." WAVE128 [".substr($_wm,0,80)."]\n", FILE_APPEND);
return; // laisse master-router.php gerer
}
if (substr_count($_wm, '::') >= 2) {
@file_put_contents('/var/log/weval/opus4-autowire.log', date('c')." DBLSEP [".substr($_wm,0,80)."]\n", FILE_APPEND);
return;
}
if (preg_match('/^\s*(opus|wire)\s+/i', $_wm) && preg_match('/^\s*(opus|wire)\s+(intent|add|wire|route|bypass)/i', $_wm)) {
@file_put_contents('/var/log/weval/opus4-autowire.log', date('c')." OPUSPFX [".substr($_wm,0,80)."]\n", FILE_APPEND);
return;
}
// === OPUS4-AUTONOMY-GUARD-v1 END ===
GUARD;
// Step 4 : injection — après "if(!$_wm) return;" ligne 5
$anchor = 'if(!$_wm) return;';
$pos = strpos($content, $anchor);
if ($pos === false) { $R['steps'][]=['err'=>'anchor not found']; die(json_encode($R)); }
$insertAt = $pos + strlen($anchor);
$newContent = substr($content, 0, $insertAt) . "\n" . $GUARD . substr($content, $insertAt);
$R['steps'][]=['anchor_pos'=>$pos, 'insert_at'=>$insertAt, 'before_len'=>strlen($content), 'after_len'=>strlen($newContent)];
// Step 5 : syntax lint via php -l (on écrit temp d'abord)
$TMP = '/tmp/wii-test-'.$TS.'.php';
file_put_contents($TMP, $newContent);
$lintOut = []; exec("php8.4 -l $TMP 2>&1", $lintOut, $lintRC);
$R['steps'][]=['lint_rc'=>$lintRC, 'lint'=>$lintOut];
if ($lintRC !== 0) {
unlink($TMP);
$R['steps'][]=['err'=>'LINT FAIL - abort, no write'];
die(json_encode($R, JSON_PRETTY_PRINT));
}
// Step 6 : chattr -i + write + chattr +i
exec('sudo chattr -i '.escapeshellarg($SRC).' 2>&1', $co1, $rc1);
if ($rc1 !== 0) { exec('chattr -i '.escapeshellarg($SRC).' 2>&1', $co1, $rc1); }
$R['steps'][]=['chattr_minus'=>$rc1];
$written = file_put_contents($SRC, $newContent);
$R['steps'][]=['written'=>$written];
exec('sudo chattr +i '.escapeshellarg($SRC).' 2>&1', $co2, $rc2);
if ($rc2 !== 0) { exec('chattr +i '.escapeshellarg($SRC).' 2>&1', $co2, $rc2); }
$R['steps'][]=['chattr_plus'=>$rc2];
// Step 7 : re-lint le fichier final
$finalLintOut = []; exec("php8.4 -l $SRC 2>&1", $finalLintOut, $finalLintRC);
$R['steps'][]=['final_lint_rc'=>$finalLintRC, 'final_lint'=>$finalLintOut];
// Step 8 : verify marker present
$finalContent = file_get_contents($SRC);
$R['steps'][]=['marker_present'=>(strpos($finalContent, $MARKER) !== false)];
unlink($TMP);
$R['ok'] = ($finalLintRC === 0 && strpos($finalContent, $MARKER) !== false && $written > 50000);
echo json_encode($R, JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE);