Files
html/api/paperclip-unfreeze.php

66 lines
3.5 KiB
PHP

<?php
header("Content-Type: application/json; charset=utf-8");
set_time_limit(120);
$agent_token = $_SERVER["HTTP_X_AGENT_TOKEN"] ?? "";
$referer = $_SERVER["HTTP_REFERER"] ?? "";
$is_internal = ($agent_token === "weval_agent_2026_secure_k7m3p9x")
|| (strpos($referer, "/wevia-master.html") !== false)
|| (strpos($referer, "/paperclip") !== false);
if (!$is_internal) {
echo json_encode(["error"=>"internal-only","intent"=>"paperclip_unfreeze"]);
exit;
}
$action = $_GET["action"] ?? $_POST["action"] ?? "status";
$t0 = microtime(true);
function pg_q($sql) {
$esc = escapeshellarg($sql);
$cmd = "PGPASSWORD=admin123 psql -U admin -h localhost -d paperclip -tAc " . $esc . " 2>&1";
return trim(shell_exec($cmd) ?? "");
}
$out = ["intent" => "paperclip_unfreeze", "action" => $action];
if ($action === "status") {
$out["total_issues"] = intval(pg_q("SELECT COUNT(*) FROM issues"));
$out["in_progress"] = intval(pg_q("SELECT COUNT(*) FROM issues WHERE status = 'in_progress'"));
$out["backlog"] = intval(pg_q("SELECT COUNT(*) FROM issues WHERE status = 'backlog'"));
$out["todo"] = intval(pg_q("SELECT COUNT(*) FROM issues WHERE status = 'todo'"));
$out["done"] = intval(pg_q("SELECT COUNT(*) FROM issues WHERE status = 'done'"));
$out["blocked"] = intval(pg_q("SELECT COUNT(*) FROM issues WHERE status = 'blocked'"));
$out["routines_active"] = intval(pg_q("SELECT COUNT(*) FROM routines WHERE status = 'active'"));
$out["routines_triggered_24h"] = intval(pg_q("SELECT COUNT(*) FROM routines WHERE last_triggered_at > NOW() - INTERVAL '24 hours'"));
$out["heartbeat_runs_1h"] = intval(pg_q("SELECT COUNT(*) FROM heartbeat_runs WHERE started_at > NOW() - INTERVAL '1 hour'"));
$out["agents_total"] = intval(pg_q("SELECT COUNT(*) FROM agents"));
$out["sovereign_running"] = !empty(shell_exec("pgrep -f paperclipai 2>/dev/null"));
$out["systemd_active"] = trim(shell_exec("systemctl is-active paperclip 2>&1")) === "active";
$out["port_3102_listen"] = !empty(shell_exec("ss -tln | grep :3102 2>&1"));
$out["health"] = ($out["sovereign_running"] && $out["systemd_active"] && $out["port_3102_listen"]) ? "healthy" : "degraded";
}
elseif ($action === "restart") {
shell_exec("sudo pkill -f paperclipai 2>&1 >/dev/null");
shell_exec("sudo systemctl restart paperclip 2>&1 >/dev/null");
sleep(3);
shell_exec("/opt/paperclip-weval/keepalive-sovereign.sh 2>&1 >/dev/null &");
sleep(10);
$out["restart"] = "completed";
$out["sovereign_running"] = !empty(shell_exec("pgrep -f paperclipai 2>/dev/null"));
}
elseif ($action === "unfreeze_backlog") {
$promoted = pg_q("UPDATE issues SET status='todo', updated_at=NOW() WHERE id IN (SELECT id FROM issues WHERE status='backlog' ORDER BY updated_at ASC LIMIT 10) RETURNING id");
$out["promoted_backlog_to_todo"] = empty($promoted) ? 0 : substr_count($promoted, chr(10)) + 1;
}
elseif ($action === "report") {
$out["systemctl"] = trim(shell_exec("systemctl is-active paperclip 2>&1"));
$out["keepalive_restarts_total"] = intval(trim(shell_exec("wc -l < /var/log/paperclip-keepalive.log 2>&1")));
$out["port_3102_listen"] = !empty(shell_exec("ss -tln | grep :3102 2>&1"));
$out["last_heartbeat"] = pg_q("SELECT MAX(started_at) FROM heartbeat_runs");
$out["oldest_backlog"] = pg_q("SELECT MIN(updated_at) FROM issues WHERE status='backlog'");
}
$out["elapsed_ms"] = round((microtime(true) - $t0) * 1000);
echo json_encode($out, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);