1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796
/*
 * Sai server - ./src/server/m-ws-browser.c
 *
 * Copyright (C) 2019 - 2025 Andy Green <andy@warmcat.com>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation:
 *  version 2.1 of the License.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *  MA  02110-1301  USA
 *
 *   b1 --\   sai-        sai-   /-- browser
 *   b2 ----- server ---- web ------ browser
 *   b3 --/                  *   \-- browser
 *
 * These are ws rx and tx handlers related to browser ws connections, on
 * /broswe URLs.
 */

#include <libwebsockets.h>
#include <string.h>
#include <signal.h>
#include <time.h>

#include "w-private.h"

/*
 * For decoding specific event data request from browser
 */

/*
 * (Structs and maps removed - now in common/include/private.h and common/struct-metadata.c)
 */

static lws_struct_map_t lsm_browser_evinfo[] = {
	LSM_CARRAY	(sai_browse_rx_evinfo_t, event_hash,	"event_hash"),
};

static lws_struct_map_t lsm_browser_branchlist[] = {
	LSM_CARRAY	(sai_browse_rx_branchlist_t, project,	"project"),
};

static lws_struct_map_t lsm_browser_taskreset[] = {
	LSM_CARRAY	(sai_browse_rx_evinfo_t, event_hash,	"uuid"),
};

static lws_struct_map_t lsm_browser_platreset[] = {
	LSM_CARRAY	(sai_browse_rx_platreset_t, event_uuid, "event_uuid"),
	LSM_CARRAY	(sai_browse_rx_platreset_t, platform,   "platform"),
};

static lws_struct_map_t lsm_browser_builderdelete[] = {
	LSM_CARRAY	(sai_browse_rx_builderdelete_t, builder_name, "builder_name"),
};

typedef struct sai_browse_rx_builder_visibility {
	uint8_t visible;
} sai_browse_rx_builder_visibility_t;

static lws_struct_map_t lsm_browser_builder_visibility[] = {
	LSM_UNSIGNED	(sai_browse_rx_builder_visibility_t, visible, "visible"),
};

static void
saiw_browser_logs_reset(struct pss *pss);
static int
saiw_browser_logs_cursor_stale(struct vhd *vhd, struct pss *pss);

static lws_struct_map_t lsm_browser_taskinfo[] = {
	LSM_CARRAY	(sai_browse_rx_taskinfo_t, task_hash,		"task_hash"),
	LSM_UNSIGNED	(sai_browse_rx_taskinfo_t, logs,		"logs"),
	LSM_UNSIGNED    (sai_browse_rx_taskinfo_t, js_api_version,	"js_api_version"),
	LSM_UNSIGNED    (sai_browse_rx_taskinfo_t, offset,		"offset"),
	LSM_UNSIGNED    (sai_browse_rx_taskinfo_t, last_log_ts,		"last_log_ts"),
	LSM_UNSIGNED    (sai_browse_rx_taskinfo_t, last_log_uid,	"last_log_uid"),
	LSM_SIGNED      (sai_browse_rx_taskinfo_t, run,			"run"),
	/* Sidebar selection scoping the overview to project + branch */
	LSM_CARRAY	(sai_browse_rx_taskinfo_t, project,		"project"),
	LSM_CARRAY	(sai_browse_rx_taskinfo_t, ref,		"ref"),
};

/*
 * Schema list so lws_struct can pick the right object to create based on the
 * incoming schema name
 */

static const lws_struct_map_t lsm_schema_json_map_bwsrx[] = {
	LSM_SCHEMA	(sai_browse_rx_taskinfo_t, NULL, lsm_browser_taskinfo,
					      "com.warmcat.sai.taskinfo"),
	LSM_SCHEMA	(sai_browse_rx_evinfo_t, NULL, lsm_browser_evinfo,
					      "com.warmcat.sai.eventinfo"),
	LSM_SCHEMA	(sai_browse_rx_evinfo_t, NULL, lsm_browser_taskreset,
			/* shares struct */   "com.warmcat.sai.taskreset"),
	LSM_SCHEMA	(sai_browse_rx_evinfo_t, NULL, lsm_browser_taskreset,
			/* shares struct */   "com.warmcat.sai.taskremovealltries"),
	LSM_SCHEMA	(sai_browse_rx_evinfo_t, NULL, lsm_browser_taskreset,
			/* shares struct */   "com.warmcat.sai.taskrebuildlaststep"),
	LSM_SCHEMA	(sai_browse_rx_evinfo_t, NULL, lsm_browser_taskreset,
			/* shares struct */   "com.warmcat.sai.eventreset"),
	LSM_SCHEMA	(sai_browse_rx_evinfo_t, NULL, lsm_browser_taskreset,
			/* shares struct */   "com.warmcat.sai.eventdelete"),
	LSM_SCHEMA	(sai_cancel_t,		 NULL, lsm_task_cancel,
					      "com.warmcat.sai.taskcan"),
	LSM_SCHEMA	(sai_load_report_t,	 NULL, lsm_load_report_members,
					      "com.warmcat.sai.loadreport"),
	LSM_SCHEMA	(sai_rebuild_t,		 NULL, lsm_rebuild,
					      "com.warmcat.sai.rebuild"),
	LSM_SCHEMA	(sai_browse_rx_platreset_t, NULL, lsm_browser_platreset,
					      "com.warmcat.sai.platreset"),
	LSM_SCHEMA	(sai_stay_t,		 NULL, lsm_stay,
					      "com.warmcat.sai.stay"),
	LSM_SCHEMA	(sai_pcon_control_t,	 NULL, lsm_pcon_control,
			/* shares struct */   "com.warmcat.sai.pcon_control"),
	LSM_SCHEMA_DLL2	(sai_watcher_service_t, list, NULL, lsm_watcher_service,
					      "com.warmcat.sai.watcher_services"),
	LSM_SCHEMA	(sai_browse_rx_builderdelete_t, NULL, lsm_browser_builderdelete,
					      "com.warmcat.sai.builderdelete"),
	LSM_SCHEMA	(sai_openshell_t, NULL, lsm_openshell,
					      "com.warmcat.sai.openshell"),
	LSM_SCHEMA	(sai_closeshell_t, NULL, lsm_closeshell,
					      "com.warmcat.sai.closeshell"),
	LSM_SCHEMA	(sai_ptydata_t, NULL, lsm_ptydata,
					      "com.warmcat.sai.ptydata"),
	LSM_SCHEMA	(sai_browse_rx_builder_visibility_t, NULL, lsm_browser_builder_visibility,
					      "com.warmcat.sai.builder_visibility"),
	/* sidebar project / branch list requests (read-only, no auth) */
	LSM_SCHEMA	(sai_browse_rx_branchlist_t, NULL, lsm_browser_branchlist,
					      "com.warmcat.sai.projlist"),
	LSM_SCHEMA	(sai_browse_rx_branchlist_t, NULL, lsm_browser_branchlist,
					      "com.warmcat.sai.branchlist"),
	/*
	 * Ad-hoc builds (admin only): cloneinfo is answered locally with what
	 * the dialog needs to prefill, taskclone is forwarded to sai-server
	 */
	LSM_SCHEMA	(sai_browse_rx_evinfo_t, NULL, lsm_browser_taskreset,
			/* shares struct */   "com.warmcat.sai.cloneinfo"),
	LSM_SCHEMA	(sai_browse_rx_taskclone_t, NULL, lsm_taskclone,
					      "com.warmcat.sai.taskclone"),
	/*
	 * Findings (admin only): the list and one finding's details are
	 * answered locally, changes are forwarded to sai-server
	 */
	LSM_SCHEMA	(sai_findingset_t, NULL, lsm_findingset,
			/* shares struct */   "com.warmcat.sai.findings"),
	LSM_SCHEMA	(sai_findingset_t, NULL, lsm_findingset,
			/* shares struct */   "com.warmcat.sai.findingget"),
	LSM_SCHEMA	(sai_findingset_t, NULL, lsm_findingset,
					      "com.warmcat.sai.findingset"),
};

enum {
	SAIM_WS_BROWSER_RX_TASKINFO,
	SAIM_WS_BROWSER_RX_EVENTINFO,
	SAIM_WS_BROWSER_RX_TASKRESET,
	SAIM_WS_BROWSER_RX_TASKREMOVEALLTRIES,
	SAIM_WS_BROWSER_RX_TASKREBUILDLASTSTEP,
	SAIM_WS_BROWSER_RX_EVENTRESET,
	SAIM_WS_BROWSER_RX_EVENTDELETE,
	SAIM_WS_BROWSER_RX_TASKCANCEL,
	SAIM_WS_BROWSER_RX_LOADREPORT,
	SAIM_WS_BROWSER_RX_REBUILD,
	SAIM_WS_BROWSER_RX_PLATRESET,
	SAIM_WS_BROWSER_RX_STAY,
	SAIM_WS_BROWSER_RX_PCON_CONTROL,
	SAIM_WS_BROWSER_RX_WATCHER_SERVICES,
	SAIM_WS_BROWSER_RX_BUILDERDELETE,
	SAIM_WS_BROWSER_RX_OPENSHELL,
	SAIM_WS_BROWSER_RX_CLOSESHELL,
	SAIM_WS_BROWSER_RX_PTYDATA,
	SAIM_WS_BROWSER_RX_BUILDER_VISIBILITY,
	SAIM_WS_BROWSER_RX_PROJLIST,
	SAIM_WS_BROWSER_RX_BRANCHLIST,
	SAIM_WS_BROWSER_RX_CLONEINFO,
	SAIM_WS_BROWSER_RX_TASKCLONE,
	SAIM_WS_BROWSER_RX_FINDINGS,
	SAIM_WS_BROWSER_RX_FINDINGGET,
	SAIM_WS_BROWSER_RX_FINDINGSET,
};

/* nonzero if s is exactly len hex chars, as task / event uuids are */
static int
saiw_id_ok(const char *s, size_t len)
{
	return strlen(s) == len && sai_is_git_hash(s);
}

/*
 * Answer com.warmcat.sai.cloneinfo: everything the ad-hoc build dialog needs
 * to prefill from the seed task, plus the project's scratch ("_"-prefixed)
 * branches as most recently pushed, newest first, so the dialog can default
 * to the latest one.
 *
 * Everything here comes from the databases sai-server maintains, which we
 * read directly, so no round trip to the server is needed.
 */
static int
saiw_browser_send_cloneinfo(struct vhd *vhd, struct pss *pss,
			    const char *seed_uuid)
{
	struct lwsac *ac_t = NULL, *ac_e = NULL;
	char event_uuid[33], esc[96], filt[160], *ebuf = NULL, *p, *end;
	lws_dll2_owner_t o_t, o_e;
	sqlite3_stmt *stmt = NULL;
	uint8_t *rbuf = NULL;
	sqlite3 *pdb = NULL;
	int ret = 1, first = 1, n;
	sai_event_t *e;
	sai_task_t *t;

	sai_task_uuid_to_event_uuid(event_uuid, seed_uuid);

	/* the seed task's event, for the repo name and its current ref */

	lws_sql_purify(esc, event_uuid, sizeof(esc));
	lws_snprintf(filt, sizeof(filt), " and uuid='%s'", esc);
	n = lws_struct_sq3_deserialize(vhd->pdb, filt, NULL,
				       lsm_schema_sq3_map_event, &o_e, &ac_e,
				       0, 1);
	if (n < 0 || !o_e.head) {
		lwsl_notice("%s: no event %s\n", __func__, event_uuid);
		goto bail;
	}
	e = lws_container_of(o_e.head, sai_event_t, list);

	/* the seed task itself, latest run */

	if (sai_event_db_ensure_open(vhd->context, &vhd->sqlite3_cache,
				     vhd->sqlite3_path_lhs, event_uuid, 0,
				     &pdb)) {
		lwsl_notice("%s: no event db %s\n", __func__, event_uuid);
		goto bail;
	}

	lws_sql_purify(esc, seed_uuid, sizeof(esc));
	lws_snprintf(filt, sizeof(filt), " and uuid='%s'", esc);
	n = lws_struct_sq3_deserialize(pdb, filt, "run desc",
				       lsm_schema_sq3_map_task, &o_t, &ac_t,
				       0, 1);
	sai_event_db_close(&vhd->sqlite3_cache, &pdb);
	if (n < 0 || !o_t.head) {
		lwsl_notice("%s: no task %s\n", __func__, seed_uuid);
		goto bail;
	}
	t = lws_container_of(o_t.head, sai_task_t, list);

	/*
	 * The build script is up to 4KiB and JSON escaping can grow each
	 * byte to 6, so both the escape buffer and the reply are heap
	 */
	ebuf = malloc((sizeof(t->build) * 6) + 8);
	rbuf = malloc(LWS_PRE + 32768);
	if (!ebuf || !rbuf)
		goto bail;
	p = (char *)rbuf + LWS_PRE;
	end = p + 32768;

	p += lws_snprintf(p, lws_ptr_diff_size_t(end, p),
			  "{\"schema\":\"com.warmcat.sai.cloneinfo\","
			  "\"seed_uuid\":\"%s\",",
			  lws_json_purify(esc, seed_uuid, sizeof(esc) - 1, NULL));
	p += lws_snprintf(p, lws_ptr_diff_size_t(end, p), "\"repo_name\":\"%s\",",
			  lws_json_purify(esc, e->repo_name, sizeof(esc) - 1, NULL));
	p += lws_snprintf(p, lws_ptr_diff_size_t(end, p), "\"ref\":\"%s\",",
			  lws_json_purify(esc, e->ref, sizeof(esc) - 1, NULL));
	p += lws_snprintf(p, lws_ptr_diff_size_t(end, p), "\"taskname\":\"%s\",",
			  lws_json_purify(esc, t->taskname, sizeof(esc) - 1, NULL));
	p += lws_snprintf(p, lws_ptr_diff_size_t(end, p), "\"platform\":\"%s\",",
			  lws_json_purify(esc, t->platform, sizeof(esc) - 1, NULL));
	p += lws_snprintf(p, lws_ptr_diff_size_t(end, p), "\"build\":\"%s\",",
			  lws_json_purify(ebuf, t->build,
					  (int)(sizeof(t->build) * 6) + 7, NULL));

	/*
	 * Scratch branches pushed for this project, newest first.  The
	 * pushes table is created by sai-server; if it isn't there yet the
	 * prepare fails and the list is simply empty.
	 */
	p += lws_snprintf(p, lws_ptr_diff_size_t(end, p), "\"refs\":[");

	if (sqlite3_prepare_v2(vhd->pdb,
			"SELECT ref, hash FROM pushes WHERE repo_name = ? "
			"AND ref LIKE 'refs/heads/\\_%' ESCAPE '\\' "
			"ORDER BY created DESC LIMIT 20",
			-1, &stmt, NULL) == SQLITE_OK) {
		sqlite3_bind_text(stmt, 1, e->repo_name, -1, SQLITE_STATIC);

		while (sqlite3_step(stmt) == SQLITE_ROW &&
		       lws_ptr_diff_size_t(end, p) > 256) {
			const char *rn = (const char *)sqlite3_column_text(stmt, 0),
				   *h = (const char *)sqlite3_column_text(stmt, 1);

			if (!rn || !h)
				continue;

			p += lws_snprintf(p, lws_ptr_diff_size_t(end, p),
					  "%s{\"ref\":\"%s\",", first ? "" : ",",
					  lws_json_purify(esc, rn, sizeof(esc) - 1, NULL));
			p += lws_snprintf(p, lws_ptr_diff_size_t(end, p),
					  "\"hash\":\"%s\"}",
					  lws_json_purify(esc, h, sizeof(esc) - 1, NULL));
			first = 0;
		}
		sqlite3_finalize(stmt);
	} else
		lwsl_info("%s: no pushes table\n", __func__);

	p += lws_snprintf(p, lws_ptr_diff_size_t(end, p), "]}");

	saiw_ws_browser_queue_REQUIRES_LWS_PRE(pss, rbuf + LWS_PRE,
			lws_ptr_diff_size_t(p, (char *)rbuf + LWS_PRE),
			LWS_WRITE_TEXT);
	ret = 0;

bail:
	free(rbuf);
	free(ebuf);
	lwsac_free(&ac_t);
	lwsac_free(&ac_e);

	return ret;
}


/*
 * For issuing combined task and event data back to browser
 */

typedef struct sai_browse_taskreply {
	const sai_event_t	*event;
	const sai_task_t	*task;
	lws_dll2_owner_t	runs;
} sai_browse_taskreply_t;

static lws_struct_map_t lsm_taskreply[] = {
	LSM_CHILD_PTR	(sai_browse_taskreply_t, event,	sai_event_t, NULL,
			 lsm_event, "e"),
	LSM_CHILD_PTR	(sai_browse_taskreply_t, task,	sai_task_t, NULL,
			 lsm_task, "t"),
	LSM_LIST	(sai_browse_taskreply_t, runs,	sai_task_t, list, NULL,
			 lsm_task, "runs"),
};

const lws_struct_map_t lsm_schema_json_map_taskreply[] = {
	LSM_SCHEMA	(sai_browse_taskreply_t, NULL, lsm_taskreply,
			 "com.warmcat.sai.taskinfo"),
};

enum sai_overview_state {
	SOS_EVENT,
	SOS_TASKS,
};

/*
 * Tx backpressure thresholds for browser connections.
 *
 * DEFER: producers that can retry (overview / log batches) hold off while
 *        the connection is this backed-up, and retry from a sul once it has
 *        drained below; matches the threshold the log path already used.
 *
 * HWM:   if the backlog is still above this, the peer is simply not
 *        consuming (zero TCP window).  It is set above the largest legit
 *        single composed message (an unscoped overview on a populated db
 *        can be a few MB), so reaching it means repeated messages have
 *        piled up undrained; we shed the connection instead of letting it
 *        pin memory.  The raw_tx sanity limit (32MiB) remains the hard
 *        ceiling behind this.
 */
#define SAIW_BROWSER_TX_DEFER	(100 * 1024)
#define SAIW_BROWSER_TX_HWM	(8 * 1024 * 1024)

int
saiw_ws_browser_queue_REQUIRES_LWS_PRE(struct pss *pss, const void *buf,
				       size_t len, enum lws_write_protocol flags)
{
	int *pi = (int *)((const char *)buf - sizeof(int)), r = 0;

	if (lws_buflist2_total_len(&pss->raw_tx) > SAIW_BROWSER_TX_HWM) {
		/*
		 * The browser stopped reading and everything queued for it is
		 * still sitting here.  Stop appending and have the connection
		 * closed rather than keep allocating for it.
		 */
		if (!pss->tx_shed) {
			pss->tx_shed = 1;
			lwsl_wsi_notice(pss->wsi,
					"tx backlog over HWM, shedding conn");
			lws_wsi_close(pss->wsi, LWS_TO_KILL_ASYNC);
		}

		return 1;
	}

	*pi = (int)flags;

	if (lws_buflist2_append_segment(&pss->raw_tx, buf - sizeof(int), len + sizeof(int)) < 0) {
		lwsl_wsi_err(pss->wsi, "unable to buflist_append"); /* still ask to drain */
		r = 1;
	}

	lws_callback_on_writable(pss->wsi);

	return r;
}

/*
 * This allows other parts of sai-web to queue a raw buffer to be sent to
 * all connected browsers, eg, for load reports.
 *
 * The flags are lws_write() flags.
 */
void
saiw_ws_broadcast_browsers_REQUIRES_LWS_PRE(struct vhd *vhd, const void *buf,
					    size_t len, enum lws_write_protocol flags)
{
	lws_start_foreach_dll(struct lws_dll2 *, p, vhd->browsers.head) {
		struct pss *pss = lws_container_of(p, struct pss, same);

		saiw_ws_browser_queue_REQUIRES_LWS_PRE(pss, buf, len, flags);

	} lws_end_foreach_dll(p);
}



int
sai_sql3_get_uint64_cb(void *user, int cols, char **values, char **name)
{
	uint64_t *pui = (uint64_t *)user;

	*pui = (uint64_t)atoll(values[0]);

	return 0;
}



/*
 * Ask for writeable cb on all browser connections subscribed to a particular
 * task (so we can send them some more logs)
 */

int
saiw_subs_request_writeable(struct vhd *vhd, const char *task_uuid)
{
	lws_start_foreach_dll(struct lws_dll2 *, p,
			      vhd->subs_owner.head) {
		struct pss *pss = lws_container_of(p, struct pss, subs_list);

		if (!strcmp(pss->sub_task_uuid, task_uuid))
			lws_callback_on_writable(pss->wsi);

	} lws_end_foreach_dll(p);

	return 0;
}

static int
saiw_pss_schedule_eventinfo(struct pss *pss, const char *event_uuid)
{
	/*
	 * This pss may be locked to a specific event
	 */

	if (pss->specific_task[0] && memcmp(pss->specific_task, event_uuid, 32))
		goto bail;

	/*
	 * The browser selected a specific event (com.warmcat.sai.eventinfo) and
	 * wants that one event's full task list in the tasks pane.  Stash the
	 * event uuid as a one-shot hint so saiw_browser_queue_overview() scopes
	 * to just that event AND emits full task data for it (bypassing the
	 * summary-only mode used for the multi-event sidebar list).  The hint
	 * is cleared by saiw_browser_queue_overview() after it is consumed.
	 */
	lws_strncpy(pss->event_tasks_uuid, event_uuid,
		    sizeof(pss->event_tasks_uuid));

	saiw_browser_queue_overview(pss->vhd, pss);
	saiw_browser_broadcast_queue_builders(pss->vhd, pss);

	return 0;

bail:
	saiw_browser_queue_overview(pss->vhd, pss);
	saiw_browser_broadcast_queue_builders(pss->vhd, pss);

	return 1;
}

/* we leave an allocation in sch->query_ac ... */

static int
saiw_pss_schedule_taskinfo(struct pss *pss, const char *task_uuid, int logsub, int run_idx)
{
	char qu[192], event_uuid[33], esc2[96], buf[4096 + LWS_PRE],
	     *start = buf + LWS_PRE, *p = start, *end = buf + sizeof(buf);
	const sai_event_t *one_event = NULL;
	sai_browse_taskreply_t task_reply;
	struct lwsac *query_ac = NULL, *runs_ac = NULL, *art_ac = NULL;
	sai_task_t *one_task = NULL;
	lws_struct_serialize_t *js;
	char esc[256], filt[192];
	lws_dll2_owner_t owner;
	sqlite3 *pdb = NULL;
	lws_dll2_owner_t o;
	sai_task_t *pt;
	char fi = 1;
	int m, n;
	size_t w;

	sai_task_uuid_to_event_uuid(event_uuid, task_uuid);

	/*
	 * This pss may be locked to a specific event and not want to hear
	 * anything unrelated to that event... lock to task is same deal but
	 * we will also send it non-log info about other tasks, so it can
	 * keep its event summary alive
	 */

	if (pss->specific_task[0] &&
	    memcmp(pss->specific_task, event_uuid, 32)) {
		lwsl_info("%s: specific_task '%s' vs event_uuid '%s\n",
			    __func__, pss->specific_task, event_uuid);
		goto bail;
	}

	/*
	 * Open the event-specific database object... a task in a project this
	 * vhost doesn't show is treated the same as one whose event is gone
	 */

	if (!saiw_event_visible(pss->vhd, event_uuid) ||
	    sai_event_db_ensure_open(pss->vhd->context, &pss->vhd->sqlite3_cache,
			      pss->vhd->sqlite3_path_lhs, event_uuid, 0, &pdb)) {
		uint8_t buf[LWS_PRE + 128];
		int n1 = lws_snprintf((char *)buf + LWS_PRE, sizeof(buf) - LWS_PRE,
				     "{\"schema\":\"com.warmcat.sai.event_deleted\"}");
		saiw_ws_browser_queue_REQUIRES_LWS_PRE(pss, buf + LWS_PRE, (size_t)n1,
						       LWS_WRITE_TEXT);
		return 0;
	}

	/*
	 * get the related task object into its own ac... there might
	 * be a lot of related data, so we hold the ac in the sch for
	 * as long as needed to send it out
	 */

	lws_sql_purify(esc, task_uuid, sizeof(esc));
	if (run_idx >= 0)
		lws_snprintf(qu, sizeof(qu), " and uuid='%s' and run=%d", esc, run_idx);
	else
		lws_snprintf(qu, sizeof(qu), " and uuid='%s'", esc);
	n = lws_struct_sq3_deserialize(pdb, qu, run_idx >= 0 ? NULL : "run desc", lsm_schema_sq3_map_task,
				       &o, &query_ac, 0, 1);
				       
	memset(&task_reply, 0, sizeof(task_reply));
	lws_dll2_owner_clear(&task_reply.runs);
	lws_snprintf(qu, sizeof(qu), " and uuid='%s'", esc);
	if (lws_struct_sq3_deserialize(pdb, qu, "run desc", lsm_schema_sq3_map_task,
				       &task_reply.runs, &runs_ac, 0, 100) < 0)
		lwsl_err("%s: runs deserialize failed\n", __func__);
				       
	sai_event_db_close(&pss->vhd->sqlite3_cache, &pdb);
	if (n < 0 || !o.head)
		goto bail;

	pt = lws_container_of(o.head, sai_task_t, list);
	one_task = pt;

	/* let the pss take over the task info ac and schedule sending */

	lws_dll2_remove((struct lws_dll2 *)&one_task->list);

	/*
	 * let's also get the event object the task relates to into
	 * its own event struct, additionally qualify this task against any
	 * pss reponame-specific constraint and bail if doesn't match
	 */

	lws_sql_purify(esc, event_uuid, sizeof(esc));
	m = lws_snprintf(qu, sizeof(qu), " and uuid='%s'", esc);
	if (pss->specific_project[0]) {
		lws_sql_purify(esc2, pss->specific_project, sizeof(esc2));
		m += lws_snprintf(qu + m, sizeof(qu) - (unsigned int)m, " and repo_name='%s'", esc2);
	}

	if (pss->specific_ref[0] && pss->specificity != SAIM_SPECIFIC_TASK) {
		lws_sql_purify(esc2, pss->specific_ref, sizeof(esc2));
		if (pss->specific_ref[0] == 'r') {
			/* check event ref against, eg, ref/heads/xxx */
			if (!strcmp(pss->specific_ref, "refs/heads/master"))
				m += lws_snprintf(qu + m, sizeof(qu) - (unsigned int)m,
					" and (ref='refs/heads/master' or ref='refs/heads/main')");
			else
				m += lws_snprintf(qu + m, sizeof(qu) - (unsigned int)m, " and ref='%s'", esc2);
		} else
			/* check event hash against, eg, 12341234abcd... */
			m += lws_snprintf(qu + m, sizeof(qu) - (unsigned int)m, " and hash='%s'", esc2);
	}

	n = lws_struct_sq3_deserialize(pss->vhd->pdb, qu, NULL,
				       lsm_schema_sq3_map_event, &o,
				       &query_ac, 0, 1);
	if (n < 0 || !o.head)
		/*
		 * It's OK if the parent event is not visible in the current
		 * filtered view, we can still update the task state where it
		 * appears inside other visible events
		 */
		one_event = NULL;
	else
		one_event = lws_container_of(o.head, sai_event_t, list);

	/*
	 * We're sending a browser the specific task info that he
	 * asked for.
	 *
	 * We already got the task struct out of the db in .one_task
	 * (all in .query_ac)... we're responsible for destroying it
	 * when we go out of scope...
	 */

	task_reply.event		= one_event;
	task_reply.task			= one_task;
	one_task->rebuildable		= (one_task->state == SAIES_FAIL ||
					   one_task->state == SAIES_CANCELLED) &&
					  (lws_now_secs() - (one_task->started +
					   (one_task->duration / 1000000)) < 24 * 3600);

	js = lws_struct_json_serialize_create(lsm_schema_json_map_taskreply,
					      LWS_ARRAY_SIZE(lsm_schema_json_map_taskreply),
					      0, &task_reply);
	if (!js) {
		lwsl_warn("%s: couldn't create\n", __func__);
		goto bail;
	}

	do {
		n = (int)lws_struct_json_serialize(js, (uint8_t *)p, lws_ptr_diff_size_t(end, p), &w);
		if (n == LSJS_RESULT_ERROR) {
			lws_struct_json_serialize_destroy(&js);
			lwsl_notice("%s: taskinfo: error generating json\n", __func__);
			goto bail;
		}
		p += w;

		if (lws_ptr_diff_size_t(end, (uint8_t *)p) < 512) {
			saiw_ws_browser_queue_REQUIRES_LWS_PRE(pss, start,
								    lws_ptr_diff_size_t(p, start),
								    lws_write_ws_flags(LWS_WRITE_TEXT, fi, 0));
			p = start;
			fi = 0;
		}

	} while (n == LSJS_RESULT_CONTINUE);

	lws_struct_json_serialize_destroy(&js);

	/*
	 * Let's also try to fetch any artifacts into pss->aft_owner...
	 * no db or no artifacts can also be a normal situation...
	 */

	sai_task_uuid_to_event_uuid(event_uuid, one_task->uuid);

	lws_dll2_owner_clear(&owner);
	if (!sai_event_db_ensure_open(pss->vhd->context, &pss->vhd->sqlite3_cache,
				      pss->vhd->sqlite3_path_lhs, event_uuid,
				      0, &pdb)) {

		/* uuid is db-derived, purify keeps the literal safe anyway */
		lws_sql_purify(esc, one_task->uuid, sizeof(esc));

		if (run_idx >= 0)
			lws_snprintf(filt, sizeof(filt), " and (task_uuid == '%s') and run=%d",
			     esc, run_idx);
		else
			lws_snprintf(filt, sizeof(filt), " and (task_uuid == '%s') and run=%d",
			     esc, one_task->run);

		if (lws_struct_sq3_deserialize(pdb, filt, NULL,
					       lsm_schema_sq3_map_artifact,
					       &owner,
					       &art_ac, 0, 10))
			lwsl_err("%s: get afcts failed\n", __func__);

		sai_event_db_close(&pss->vhd->sqlite3_cache, &pdb);
	}



	saiw_ws_browser_queue_REQUIRES_LWS_PRE(pss, start,
					       lws_ptr_diff_size_t(p, start),
					       lws_write_ws_flags(LWS_WRITE_TEXT, fi, 1));

	/* does he want to subscribe to logs? */
	if (logsub) {
		int new_run = run_idx >= 0 ? run_idx : one_task->run;
		/* was this pss watching anything before now? */
		int had_sub = !!pss->sub_task_uuid[0];
		int is_new_task = strcmp(pss->sub_task_uuid, one_task->uuid);
		int is_new_run = pss->sub_run != new_run;

		strcpy(pss->sub_task_uuid, one_task->uuid);
		pss->sub_run = new_run;

		if (!pss->subs_list.owner) {
			lws_dll2_add_head(&pss->subs_list, &pss->vhd->subs_owner);
		}

		/*
		 * The browser tells us the highest row it already has, so it can
		 * resume across a reconnect instead of us shipping the whole log
		 * again.  It sends 0 when it wants the log from the start, which
		 * is what it does whenever it selects a task.
		 *
		 * We have to wipe what it is showing if we are about to send
		 * from the start, or if the rows it has belong to a run it is no
		 * longer looking at... otherwise the new lines just pile up
		 * underneath lines that are not part of this build any more.
		 */

		if (!pss->initial_log_uid ||
		    (had_sub && (is_new_task || is_new_run))) {
			saiw_browser_logs_reset(pss);
			saiw_broadcast_logs_batch(pss->vhd, pss);
		} else {
			pss->sub_uid = pss->initial_log_uid;
			pss->sub_timestamp = pss->initial_log_timestamp;

			if (saiw_browser_logs_cursor_stale(pss->vhd, pss)) {
				/* the rows it is resuming from are gone */
				saiw_browser_logs_reset(pss);
				saiw_broadcast_logs_batch(pss->vhd, pss);
			}
		}
	} else if (!strcmp(pss->sub_task_uuid, one_task->uuid)) {
		/* If already subscribed to this task, track new runs automatically */
		int new_run = run_idx >= 0 ? run_idx : one_task->run;

		if (pss->sub_run != new_run) {
			pss->sub_run = new_run;
			saiw_browser_logs_reset(pss);
			saiw_broadcast_logs_batch(pss->vhd, pss);
		} else
			/*
			 * Same task, same run, but the rows may have been
			 * removed under us: "remove all tries" deletes a task's
			 * logs and puts it back to run 0, so the run alone does
			 * not always change
			 */
			if (saiw_browser_logs_cursor_stale(pss->vhd, pss)) {
				saiw_browser_logs_reset(pss);
				saiw_broadcast_logs_batch(pss->vhd, pss);
			}
	}

	saiw_browser_broadcast_queue_builders(pss->vhd, pss);

	if (owner.head) {
		sai_artifact_t *aft = (sai_artifact_t *)owner.head;

		p = start;
		fi = 1;

		lwsl_info("%s: WSS_SEND_ARTIFACT_INFO: consuming artifact\n", __func__);

		lws_dll2_remove(&aft->list);

		/* we don't want to disclose this to browsers */
		aft->artifact_up_nonce[0] = '\0';

		js = lws_struct_json_serialize_create(lsm_schema_json_map_artifact,
				LWS_ARRAY_SIZE(lsm_schema_json_map_artifact),
				0, aft);
		if (!js) {
			lwsl_err("%s ----------------- failed to render artifact json\n", __func__);
			goto bail;
		}

		do {
			n = (int)lws_struct_json_serialize(js, (uint8_t *)p, lws_ptr_diff_size_t(end, p), &w);
			if (n == LSJS_RESULT_ERROR) {
				lws_struct_json_serialize_destroy(&js);
				lwsl_notice("%s: taskinfo: ---------- error generating json\n", __func__);
				goto bail;
			}
			p += w;
			if (lws_ptr_diff_size_t(end, p) < 512) {
				saiw_ws_broadcast_browsers_REQUIRES_LWS_PRE(pss->vhd, start,
									    lws_ptr_diff_size_t(p, start),
									    lws_write_ws_flags(LWS_WRITE_TEXT, fi, 0));
				p = start;
				fi = 0;
			}

		} while (n == LSJS_RESULT_CONTINUE);

		lws_struct_json_serialize_destroy(&js);
	}

	lwsac_free(&query_ac);
	lwsac_free(&runs_ac);
	lwsac_free(&art_ac);

	return 0;

bail:
	lwsac_free(&query_ac);
	lwsac_free(&runs_ac);
	lwsac_free(&art_ac);

	return 1;
}

/*
 * We need to schedule re-sending out task and event state to anyone subscribed
 * to the task that changed or its associated event
 */

int
saiw_subs_task_state_change(struct vhd *vhd, const char *task_uuid)
{
	lws_start_foreach_dll(struct lws_dll2 *, p,
			      vhd->subs_owner.head) {
		struct pss *pss = lws_container_of(p, struct pss, subs_list);

		if (!strcmp(pss->sub_task_uuid, task_uuid))
			saiw_pss_schedule_taskinfo(pss, task_uuid, 0, pss->sub_run);

	} lws_end_foreach_dll(p);

	return 0;
}


int
saiw_browsers_task_state_change(struct vhd *vhd, const char *task_uuid)
{
	char event_uuid[33];

	sai_task_uuid_to_event_uuid(event_uuid, task_uuid);

	lws_start_foreach_dll(struct lws_dll2 *, p, vhd->browsers.head) {
		struct pss *pss = lws_container_of(p, struct pss, same);

		if (!pss->is_gitohashi &&
		    (!pss->selected_event_uuid[0] ||
		     !strcmp(pss->selected_event_uuid, event_uuid)))
			saiw_pss_schedule_taskinfo(pss, task_uuid, 0, -1);
	} lws_end_foreach_dll(p);

	return 0;
}


int
saiw_event_state_change(struct vhd *vhd, const char *event_uuid)
{
	/* long poll feed requests may be waiting on this */
	saiw_rss_event_change(vhd);

	lws_start_foreach_dll(struct lws_dll2 *, p, vhd->browsers.head) {
		struct pss *pss = lws_container_of(p, struct pss, same);

		if (!pss->is_gitohashi)
			saiw_pss_schedule_eventinfo(pss, event_uuid);
	} lws_end_foreach_dll(p);

	return 0;
}

/*
 * Nonzero if the browser message, which has decoded into dest, is about
 * something in a project this vhost shows, or about nothing project-specific
 * at all (eg, builders).  Taskinfo and eventinfo don't need checking here,
 * the replies to them are already limited to the visible projects.
 */

static int
saiw_rx_visible(struct vhd *vhd, int schema_idx, void *dest)
{
	switch (schema_idx) {
	case SAIM_WS_BROWSER_RX_TASKRESET:
	case SAIM_WS_BROWSER_RX_TASKREMOVEALLTRIES:
	case SAIM_WS_BROWSER_RX_TASKREBUILDLASTSTEP:
	case SAIM_WS_BROWSER_RX_EVENTRESET:
	case SAIM_WS_BROWSER_RX_EVENTDELETE:
	case SAIM_WS_BROWSER_RX_CLONEINFO:
		return saiw_event_visible(vhd,
				((sai_browse_rx_evinfo_t *)dest)->event_hash);
	case SAIM_WS_BROWSER_RX_TASKCANCEL:
		return saiw_event_visible(vhd, ((sai_cancel_t *)dest)->task_uuid);
	case SAIM_WS_BROWSER_RX_PLATRESET:
		return saiw_event_visible(vhd,
				((sai_browse_rx_platreset_t *)dest)->event_uuid);
	case SAIM_WS_BROWSER_RX_OPENSHELL:
		return saiw_event_visible(vhd,
				((sai_openshell_t *)dest)->task_uuid);
	case SAIM_WS_BROWSER_RX_CLOSESHELL:
		return saiw_event_visible(vhd,
				((sai_closeshell_t *)dest)->task_uuid);
	case SAIM_WS_BROWSER_RX_PTYDATA:
		return saiw_event_visible(vhd,
				((sai_ptydata_t *)dest)->task_uuid);
	case SAIM_WS_BROWSER_RX_TASKCLONE:
		return saiw_event_visible(vhd,
				((sai_browse_rx_taskclone_t *)dest)->seed_uuid);
	case SAIM_WS_BROWSER_RX_FINDINGGET:
	case SAIM_WS_BROWSER_RX_FINDINGSET:
		return saiw_project_visible(vhd,
				((sai_findingset_t *)dest)->repo);
	}

	return 1;
}

/*
 * sai-web has sent us a request for either overview, or data on a specific
 * task
 */

int
saiw_ws_json_rx_browser(struct vhd *vhd, struct pss *pss, uint8_t *buf,
			size_t bl, unsigned int ss_flags)
{
	sai_browse_rx_taskinfo_t *ti;
	sai_browse_rx_evinfo_t *ei;
	lws_struct_args_t a;
	sai_cancel_t *can;
	int m, ret = -1;

	lwsl_info("%s: len %d, flags: %d\n", __func__, (int)bl, ss_flags);
	/* lwsl_hexdump_info(buf, bl); */

	memset(&a, 0, sizeof(a));
	/*
	 * pss->js_api_version defaults to 1 (from ESTABLISHED callback).
	 * A new client will update it by sending a js-hello message.
	 */
	a.map_st[0] = lsm_schema_json_map_bwsrx;
	a.map_entries_st[0] = LWS_ARRAY_SIZE(lsm_schema_json_map_bwsrx);
	a.map_entries_st[1] = LWS_ARRAY_SIZE(lsm_schema_json_map_bwsrx);
	a.ac_block_size = 128;

	lws_struct_json_init_parse(&pss->ctx, NULL, &a);
	m = lejp_parse(&pss->ctx, (uint8_t *)buf, (int)bl);
	if (m < 0 || !a.dest) {
		lwsl_hexdump_notice(buf, bl);
		lwsl_notice("%s: browser->web JSON decode failed '%s'\n",
				__func__, lejp_error_to_string(m));
		ret = m;
		goto bail;
	}

	/*
	 * Which object we ended up with depends on the schema that came in...
	 * a.top_schema_index is the index in lsm_schema_json_map_bwsrx it
	 * matched on
	 */

	if (pss->auth_state != SAI_AUTH_STATE_LOGGED_IN_GRANT_ADMIN && (
	    a.top_schema_index == SAIM_WS_BROWSER_RX_TASKRESET ||
	    a.top_schema_index == SAIM_WS_BROWSER_RX_TASKREMOVEALLTRIES ||
	    a.top_schema_index == SAIM_WS_BROWSER_RX_TASKREBUILDLASTSTEP ||
	    a.top_schema_index == SAIM_WS_BROWSER_RX_EVENTRESET ||
	    a.top_schema_index == SAIM_WS_BROWSER_RX_EVENTDELETE ||
	    a.top_schema_index == SAIM_WS_BROWSER_RX_TASKCANCEL ||
	    a.top_schema_index == SAIM_WS_BROWSER_RX_REBUILD ||
	    a.top_schema_index == SAIM_WS_BROWSER_RX_PLATRESET ||
	    a.top_schema_index == SAIM_WS_BROWSER_RX_STAY ||
	    a.top_schema_index == SAIM_WS_BROWSER_RX_PCON_CONTROL ||
	    a.top_schema_index == SAIM_WS_BROWSER_RX_BUILDERDELETE ||
	    a.top_schema_index == SAIM_WS_BROWSER_RX_OPENSHELL ||
	    a.top_schema_index == SAIM_WS_BROWSER_RX_CLOSESHELL ||
	    a.top_schema_index == SAIM_WS_BROWSER_RX_PTYDATA ||
	    a.top_schema_index == SAIM_WS_BROWSER_RX_CLONEINFO ||
	    a.top_schema_index == SAIM_WS_BROWSER_RX_TASKCLONE ||
	    a.top_schema_index == SAIM_WS_BROWSER_RX_FINDINGS ||
	    a.top_schema_index == SAIM_WS_BROWSER_RX_FINDINGGET ||
	    a.top_schema_index == SAIM_WS_BROWSER_RX_FINDINGSET)) {
		uint8_t unauth_buf[LWS_PRE + 128];
		int n1 = lws_snprintf((char *)unauth_buf + LWS_PRE, sizeof(unauth_buf) - LWS_PRE,
				     "{\"schema\":\"com.warmcat.sai.unauthorized\"}");
		saiw_ws_browser_queue_REQUIRES_LWS_PRE(pss, unauth_buf + LWS_PRE, (size_t)n1, LWS_WRITE_TEXT);
		lwsl_notice("%s: Unauthorized attempt to execute administrative action (schema %d, auth_state %d)\n", __func__, a.top_schema_index, (int)pss->auth_state);
		goto soft_error;
	}

	/*
	 * Nobody, admin or not, can act on projects this vhost doesn't show:
	 * for it, they don't exist.  Drop it, the UI never sends these.
	 */
	if (!saiw_rx_visible(vhd, a.top_schema_index, a.dest)) {
		lwsl_notice("%s: dropping schema %d for a project not shown "
			    "on this vhost\n", __func__, a.top_schema_index);
		goto ok;
	}

	switch (a.top_schema_index) {

	case SAIM_WS_BROWSER_RX_BUILDER_VISIBILITY:
		{
			sai_browse_rx_builder_visibility_t *v = (sai_browse_rx_builder_visibility_t *)a.dest;
			pss->wants_builder_info = v->visible;
			if (v->visible) {
				saiw_browser_broadcast_queue_builders(pss->vhd, pss);
				saiw_browser_broadcast_queue_pcons(pss->vhd, pss);
				saiw_browser_broadcast_queue_power_history(pss->vhd, pss);
			}
		}
		goto ok;

	case SAIM_WS_BROWSER_RX_TASKINFO:
		ti = (sai_browse_rx_taskinfo_t *)a.dest;

		lwsl_info("%s: schema index %d, task hash %s\n", __func__,
				a.top_schema_index, ti->task_hash);

		if (!ti->task_hash[0]) {
			/*
			 * he's asking for the overview schema
			 */
			// lwsl_warn("%s: SAIM_WS_BROWSER_RX_TASKINFO: doing WSS_PREPARE_BUILDER_SUMMARY\n", __func__);

			if (ti->js_api_version)
				pss->js_api_version = ti->js_api_version;
			pss->overview_offset = ti->offset;

			/*
			 * Update the runtime sidebar selection so subsequent
			 * overview / live pushes are scoped to this project +
			 * branch.  An empty value means "no constraint".
			 */
			lws_strncpy(pss->selected_project, ti->project,
				    sizeof(pss->selected_project));
			lws_strncpy(pss->selected_ref, ti->ref,
				    sizeof(pss->selected_ref));

			saiw_browser_broadcast_queue_builders(pss->vhd, pss);
 
			{
				uint8_t buf[LWS_PRE + 4096], *start = buf + LWS_PRE, *p = start, *end = buf + sizeof(buf);
				
				p += lws_snprintf((char *)p, lws_ptr_diff_size_t(end, p), 
					"{\"schema\":\"com.warmcat.sai.watcher_services\",\"watchers\":[]}");
				saiw_ws_browser_queue_REQUIRES_LWS_PRE(pss, start, lws_ptr_diff_size_t(p, start), LWS_WRITE_TEXT);
			}

			{
				uint8_t buf[LWS_PRE + 256], *start = buf + LWS_PRE, *p = start, *end = buf + sizeof(buf);
				
				p += lws_snprintf((char *)p, lws_ptr_diff_size_t(end, p), 
					"{\"schema\":\"com.warmcat.sai.auth_state\",\"auth_state\":%d}", (int)pss->auth_state);
				saiw_ws_browser_queue_REQUIRES_LWS_PRE(pss, start, lws_ptr_diff_size_t(p, start), LWS_WRITE_TEXT);
			}
 
			saiw_browser_queue_overview(pss->vhd, pss);
			break;
		}

		/*
		 * get the related task object into its own ac... there might
		 * be a lot of related data, so we hold the ac in the pss for
		 * as long as needed to send it out
		 */

		if (ti->logs) {
			pss->initial_log_timestamp = ti->last_log_ts;
			pss->initial_log_uid = ti->last_log_uid;
		} else {
			pss->initial_log_timestamp = 0;
			pss->initial_log_uid = 0;
		}

		if (saiw_pss_schedule_taskinfo(pss, ti->task_hash, !!ti->logs, ti->run))
			goto soft_error;

		goto ok;

	case SAIM_WS_BROWSER_RX_EVENTINFO:

		ei = (sai_browse_rx_evinfo_t *)a.dest;

		lws_strncpy(pss->selected_event_uuid, ei->event_hash, sizeof(pss->selected_event_uuid));

		if (saiw_pss_schedule_eventinfo(pss, ei->event_hash))
			goto soft_error;

		goto ok;

	case SAIM_WS_BROWSER_RX_PROJLIST:
	{
		/*
		 * Return the set of unique project (repo_name) values seen
		 * in the events db, ordered by name.  Read-only.
		 */
		sqlite3_stmt *stmt = NULL;
		uint8_t buf[LWS_PRE + 4096], *start = buf + LWS_PRE,
			*p = start, *end = buf + sizeof(buf);
		char esc[96], q[256];
		/* first_elem = first array entry (omit leading comma);
		 * sent_any   = have we already tx'd a ws fragment of this msg */
		int first_elem = 1, sent_any = 0, rc;

		lws_snprintf(q, sizeof(q),
				"SELECT repo_name, MAX(created) AS mc FROM events "
				"WHERE state != ?%s GROUP BY repo_name "
				"ORDER BY mc DESC", saiw_visible_sql(vhd));

		if (sqlite3_prepare_v2(vhd->pdb, q, -1, &stmt, NULL) !=
								SQLITE_OK) {
			lwsl_notice("%s: projlist prepare failed\n", __func__);
			goto soft_error;
		}
		sqlite3_bind_int(stmt, 1, SAIES_DELETED);

		p += lws_snprintf((char *)p, lws_ptr_diff_size_t(end, p),
				  "{\"schema\":\"com.warmcat.sai.projlist\","
				  "\"projects\":[");
		while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
			const char *pn = (const char *)
					sqlite3_column_text(stmt, 0);
			if (!pn)
				continue;
			if (lws_ptr_diff_size_t(end, p) < 96) {
				saiw_ws_browser_queue_REQUIRES_LWS_PRE(pss,
					start, lws_ptr_diff_size_t(p, start),
					lws_write_ws_flags(LWS_WRITE_TEXT,
							   !sent_any, 0));
				sent_any = 1;
				p = start;
			}
			p += lws_snprintf((char *)p,
				lws_ptr_diff_size_t(end, p), "%s\"%s\"",
				first_elem ? "" : ",",
				lws_json_purify(esc, pn, sizeof(esc) - 1, NULL));
			first_elem = 0;
		}
		sqlite3_finalize(stmt);

		p += lws_snprintf((char *)p, lws_ptr_diff_size_t(end, p),
				  "]}");
		saiw_ws_browser_queue_REQUIRES_LWS_PRE(pss, start,
				lws_ptr_diff_size_t(p, start),
				LWS_WRITE_TEXT);
		goto ok;
	}

	case SAIM_WS_BROWSER_RX_BRANCHLIST:
	{
		/*
		 * Return the set of unique refs for the given project, in
		 * most-recent-first order (the ref whose latest event is the
		 * newest comes first).  A parallel "branch_states" object maps
		 * each ref to the state of its latest non-deleted event, so the
		 * browser can colour the branch list rows by build result.
		 * Read-only.  project and the state filters are bound, not
		 * interpolated.
		 *
		 * We run the grouped query once into an lwsac snapshot, then
		 * emit both the "branches" array and the "branch_states" object
		 * from that single consistent result.  The correlated subquery
		 * resolves the state of the newest non-deleted event for the
		 * group's ref within the same project.  Bind order follows
		 * parameter appearance: project, SAIES_DELETED (subquery),
		 * then SAIES_DELETED, project (outer).
		 */
		sai_browse_rx_branchlist_t *bl =
				(sai_browse_rx_branchlist_t *)a.dest;
		struct bl_row {
			struct lws_dll2	list;
			char		*ref;
			int		state;
		};
		struct lwsac *ac = NULL;
		lws_dll2_owner_t owner;
		sqlite3_stmt *stmt = NULL;
		uint8_t buf[LWS_PRE + 4096], *start = buf + LWS_PRE,
			*p = start, *end = buf + sizeof(buf);
		char esc[96], pesc[96], q[512];
		int first_elem = 1, sent_any = 0, rc;

		lws_dll2_owner_clear(&owner);

		/*
		 * Belt-and-braces: also purify (the bound param already
		 * prevents injection, but this keeps the echoed project
		 * field safe to emit too).
		 */
		lws_sql_purify(pesc, bl->project, sizeof(pesc) - 1);

		/*
		 * A project this vhost doesn't show has no branches; the
		 * subquery only looks at the project the outer one found.
		 */
		lws_snprintf(q, sizeof(q),
				"SELECT ref, "
				"(SELECT e2.state FROM events e2 "
				" WHERE e2.ref = events.ref "
				" AND e2.repo_name = ? AND e2.state != ? "
				" ORDER BY e2.created DESC LIMIT 1) AS ls "
				"FROM events "
				"WHERE state != ? AND repo_name = ?%s "
				"GROUP BY ref ORDER BY MAX(created) DESC",
				saiw_visible_sql(vhd));

		if (sqlite3_prepare_v2(vhd->pdb, q, -1, &stmt, NULL) !=
								SQLITE_OK) {
			lwsl_notice("%s: branchlist prepare failed\n",
					__func__);
			goto soft_error;
		}
		sqlite3_bind_text(stmt, 1, pesc, -1, SQLITE_STATIC);
		sqlite3_bind_int(stmt, 2, SAIES_DELETED);
		sqlite3_bind_int(stmt, 3, SAIES_DELETED);
		sqlite3_bind_text(stmt, 4, pesc, -1, SQLITE_STATIC);

		while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
			struct bl_row *row;
			const char *rn = (const char *)
					sqlite3_column_text(stmt, 0);
			size_t rn_len;
			if (!rn)
				continue;
			rn_len = strlen(rn);
			row = lwsac_use_zero(&ac, sizeof(*row), 0);
			if (!row) {
				sqlite3_finalize(stmt);
				lwsac_free(&ac);
				goto soft_error;
			}
			row->ref = lwsac_use(&ac, rn_len + 1, 0);
			if (!row->ref) {
				sqlite3_finalize(stmt);
				lwsac_free(&ac);
				goto soft_error;
			}
			memcpy(row->ref, rn, rn_len + 1);
			row->state = sqlite3_column_int(stmt, 1);
			lws_dll2_add_tail(&row->list, &owner);
		}
		sqlite3_finalize(stmt);

		p += lws_snprintf((char *)p, lws_ptr_diff_size_t(end, p),
				  "{\"schema\":\"com.warmcat.sai.branchlist\","
				  "\"project\":\"%s\",\"branches\":[",
				  lws_json_purify(esc, pesc,
						  sizeof(esc) - 1, NULL));
		first_elem = 1;
		lws_start_foreach_dll_safe(struct lws_dll2 *, d, d1,
					   owner.head) {
			struct bl_row *row = lws_container_of(d,
						struct bl_row, list);
			if (lws_ptr_diff_size_t(end, p) < 96) {
				saiw_ws_browser_queue_REQUIRES_LWS_PRE(pss,
					start, lws_ptr_diff_size_t(p, start),
					lws_write_ws_flags(LWS_WRITE_TEXT,
							   !sent_any, 0));
				sent_any = 1;
				p = start;
			}
			p += lws_snprintf((char *)p,
				lws_ptr_diff_size_t(end, p), "%s\"%s\"",
				first_elem ? "" : ",",
				lws_json_purify(esc, row->ref,
						sizeof(esc) - 1, NULL));
			first_elem = 0;
		} lws_end_foreach_dll_safe(d, d1);

		/*
		 * Parallel ref -> latest-state map.  Kept as a separate object
		 * so the existing "branches" string array stays unchanged for
		 * older clients that ignore the extra field.
		 */
		p += lws_snprintf((char *)p, lws_ptr_diff_size_t(end, p),
				  "],\"branch_states\":{");
		first_elem = 1;
		lws_start_foreach_dll_safe(struct lws_dll2 *, d, d1,
					   owner.head) {
			struct bl_row *row = lws_container_of(d,
						struct bl_row, list);
			if (lws_ptr_diff_size_t(end, p) < 96) {
				saiw_ws_browser_queue_REQUIRES_LWS_PRE(pss,
					start, lws_ptr_diff_size_t(p, start),
					lws_write_ws_flags(LWS_WRITE_TEXT,
							   !sent_any, 0));
				sent_any = 1;
				p = start;
			}
			p += lws_snprintf((char *)p,
				lws_ptr_diff_size_t(end, p), "%s\"%s\":%d",
				first_elem ? "" : ",",
				lws_json_purify(esc, row->ref,
						sizeof(esc) - 1, NULL),
				row->state);
			first_elem = 0;
		} lws_end_foreach_dll_safe(d, d1);

		p += lws_snprintf((char *)p, lws_ptr_diff_size_t(end, p),
				  "}}");
		lwsac_free(&ac);

		saiw_ws_browser_queue_REQUIRES_LWS_PRE(pss, start,
				lws_ptr_diff_size_t(p, start),
				LWS_WRITE_TEXT);
		goto ok;
	}

	case SAIM_WS_BROWSER_RX_CLONEINFO:
		/*
		 * Admin wants to seed an ad-hoc build from this task; answer
		 * locally with what the dialog needs, nothing goes to the
		 * server until he submits the taskclone
		 */
		ei = (sai_browse_rx_evinfo_t *)a.dest;
		if (!saiw_id_ok(ei->event_hash, 64)) {
			lwsl_notice("%s: bad cloneinfo uuid\n", __func__);
			goto soft_error;
		}
		saiw_browser_send_cloneinfo(vhd, pss, ei->event_hash);
		goto ok;

	case SAIM_WS_BROWSER_RX_FINDINGS:
		saiw_browser_send_findings(vhd, pss);
		goto ok;

	case SAIM_WS_BROWSER_RX_FINDINGGET:
		saiw_browser_send_finding(vhd, pss,
					  (sai_findingset_t *)a.dest);
		goto ok;

	case SAIM_WS_BROWSER_RX_FINDINGSET:
	{
		sai_findingset_t *fs = (sai_findingset_t *)a.dest;

		/* sai-server checks these again */
		if (!sai_pool_name_ok(fs->pool) || strlen(fs->group) != 16 ||
		    !sai_is_git_hash(fs->group) ||
		    sai_str_has_shell_metachars(fs->repo)) {
			lwsl_notice("%s: dropping malformed findingset\n",
				    __func__);
			goto soft_error;
		}
		break; /* forward it to sai-server with the rest */
	}

	case SAIM_WS_BROWSER_RX_TASKCLONE:
	{
		sai_browse_rx_taskclone_t *tc =
				(sai_browse_rx_taskclone_t *)a.dest;

		/*
		 * Sanity-check before forwarding; sai-server validates again
		 * and resolves the ref to a hash itself.  A string that fills
		 * its array exactly was truncated by lws_struct on the way in
		 * and can't be what the user meant.
		 */
		if (!saiw_id_ok(tc->seed_uuid, 64) ||
		    strncmp(tc->ref, "refs/", 5) || !sai_is_safe_ref(tc->ref) ||
		    strlen(tc->ref) >= sizeof(tc->ref) - 1 ||
		    !tc->build[0] ||
		    strlen(tc->build) >= sizeof(tc->build) - 1) {
			lwsl_notice("%s: dropping malformed taskclone\n",
				    __func__);
			goto soft_error;
		}

		lwsl_notice("%s: forwarding taskclone: seed %s, ref %s\n",
			    __func__, tc->seed_uuid, tc->ref);
		break; /* forward it to sai-server with the rest */
	}

	case SAIM_WS_BROWSER_RX_TASKREMOVEALLTRIES:
	case SAIM_WS_BROWSER_RX_TASKRESET:

		/*
		 * User is asking us to reset / rebuild this task
		 */

		ei = (sai_browse_rx_evinfo_t *)a.dest;
		break;

	case SAIM_WS_BROWSER_RX_STAY:
		lwsl_notice("%s: web: received stay req\n", __func__);

		/*
		 * User is asking us to set or release a stay on a builder
		 */
		break;

	case SAIM_WS_BROWSER_RX_PCON_CONTROL:
		lwsl_warn("%s: web: received pcon control req (len %d)\n", __func__, (int)bl);

		/* Forward to sai-server via websrv link */
		if (sai_ss_queue_frag_on_buflist_REQUIRES_LWS_PRE(vhd->h_ss_websrv,
			&((saiw_websrv_t *)lws_ss_to_user_object(vhd->h_ss_websrv))->wbltx,
			buf, bl, ss_flags))
			lwsl_err("%s: failed to queue pcon control to server\n", __func__);
		else
			lwsl_warn("%s: queued pcon control to server OK\n", __func__);

		goto ok;

	case SAIM_WS_BROWSER_RX_TASKREBUILDLASTSTEP:

		/*
		 * User is asking us to rebuild the last step of this task
		 */

		ei = (sai_browse_rx_evinfo_t *)a.dest;
		break;

	case SAIM_WS_BROWSER_RX_EVENTRESET:

		/*
		 * User is asking us to reset / rebuild every task in the event
		 */

		ei = (sai_browse_rx_evinfo_t *)a.dest;

		lwsl_notice("%s: received request to reset event %s\n",
			    __func__, ei->event_hash);
		break;

	case SAIM_WS_BROWSER_RX_EVENTDELETE:
		/*
		 * User is asking us to delete the whole event
		 */

		ei = (sai_browse_rx_evinfo_t *)a.dest;

		lwsl_notice("%s: received request to delete event %s\n",
			    __func__, ei->event_hash);

		break;

	case SAIM_WS_BROWSER_RX_TASKCANCEL:

		/*
		 * Browser is informing us of task's STOP button clicked, we
		 * need to inform any builder that might be building it
		 */
		can = (sai_cancel_t *)a.dest;

		lwsl_notice("%s: received request to cancel task %s\n",
			    __func__, can->task_uuid);
		break; /* forward it to sai-server with the rest */

	case SAIM_WS_BROWSER_RX_REBUILD:
		/*
		 * User is asking us to rebuild a builder
		 */
		break;

	case SAIM_WS_BROWSER_RX_PLATRESET:
		/*
		 * User is asking us to reset / rebuild a whole platform
		 */
		break;

	case SAIM_WS_BROWSER_RX_BUILDERDELETE:
		/*
		 * User is asking us to delete a builder
		 */
		break;

	case SAIM_WS_BROWSER_RX_OPENSHELL:
		/*
		 * This browser opened a shell; only it will be sent the
		 * shell's ptydata.  The forward to sai-server happens with
		 * the rest below.
		 */
		saiw_pss_shell_open(pss,
			    ((sai_openshell_t *)a.dest)->task_uuid);
		break;

	case SAIM_WS_BROWSER_RX_CLOSESHELL:
		saiw_pss_shell_close(pss,
			    ((sai_closeshell_t *)a.dest)->task_uuid);
		break;

	case SAIM_WS_BROWSER_RX_PTYDATA:
		break;

	/*
	 * Load reports flow builder -> server -> us -> browsers; a browser
	 * sending one is meaningless.  Drop it locally rather than forward
	 * it, sai-server does not accept this schema on the web link and
	 * would tear the link down trying to decode it.
	 */
	case SAIM_WS_BROWSER_RX_LOADREPORT:
		lwsl_notice("%s: dropping loadreport from browser\n", __func__);
		goto ok;

	/*
	 * Watcher services are a server config-file concern; the schema only
	 * ever flows from us towards browsers.  A browser sending one is
	 * meaningless: drop it locally rather than forward it, sai-server
	 * does not accept this schema on the web link.
	 */
	case SAIM_WS_BROWSER_RX_WATCHER_SERVICES:
		lwsl_notice("%s: dropping watcher_services from browser\n",
				__func__);
		goto ok;

	default:
		/*
		 * No schema in the map today reaches here.  If one is added
		 * to the map without a case above, log and drop it rather
		 * than assert (remote-crashable) or forward an unknown
		 * schema on the server link.
		 */
		lwsl_notice("%s: unhandled schema index %d from browser, dropping\n",
				__func__, a.top_schema_index);
		goto ok;
	}

	sai_ss_queue_frag_on_buflist_REQUIRES_LWS_PRE(vhd->h_ss_websrv,
		&((saiw_websrv_t *)lws_ss_to_user_object(vhd->h_ss_websrv))->wbltx,
		buf, bl, ss_flags);

ok:
	ret = 0;

soft_error:
bail:
	lwsac_free(&a.ac);

	return ret;
}

static void
saiw_retry_logs(lws_sorted_usec_list_t *sul)
{
	struct pss *pss = lws_container_of(sul, struct pss, sul_logcache);

	saiw_broadcast_logs_batch(pss->vhd, pss);
}

/*
 * Tell a browser to throw away the log lines it is showing for the task it is
 * subscribed to, and start again from the first row.
 *
 * The task's logs can go away underneath a browser that is looking at them:
 * "remove all tries" deletes them outright, and a builder disconnecting or a
 * rebuild starts a new run.  Without this the pane keeps showing lines that no
 * longer exist, and for a fresh run appends the new ones underneath the old.
 */

static void
saiw_browser_logs_reset(struct pss *pss)
{
	uint8_t buf[LWS_PRE + 192], *start = buf + LWS_PRE;
	char esc[132];
	int n;

	pss->sub_uid		= 0;
	pss->sub_timestamp	= 0;

	n = lws_snprintf((char *)start, sizeof(buf) - LWS_PRE,
			 "{\"schema\":\"com.warmcat.sai.logs_reset\","
			  "\"task_hash\":\"%s\",\"run\":%d}",
			 lws_json_purify(esc, pss->sub_task_uuid,
					 sizeof(esc) - 1, NULL), pss->sub_run);

	saiw_ws_browser_queue_REQUIRES_LWS_PRE(pss, start, (size_t)n,
					       LWS_WRITE_TEXT);
}

/*
 * The cursor can never legitimately be ahead of the newest row that exists, so
 * if it is, the rows it was counting have been deleted (or we have moved to a
 * run that has not produced any yet) and the browser has to start over.
 */

static int
saiw_browser_logs_cursor_stale(struct vhd *vhd, struct pss *pss)
{
	char event_uuid[33], q[256], pesc[132];
	uint64_t max_uid = 0;
	sqlite3 *pdb = NULL;

	if (!pss->sub_uid || !pss->sub_task_uuid[0])
		return 0;

	sai_task_uuid_to_event_uuid(event_uuid, pss->sub_task_uuid);

	if (sai_event_db_ensure_open(vhd->context, &vhd->sqlite3_cache,
				     vhd->sqlite3_path_lhs, event_uuid, 0, &pdb))
		/* the event is gone, which the log query itself will report */
		return 0;

	lws_sql_purify(pesc, pss->sub_task_uuid, sizeof(pesc));
	lws_snprintf(q, sizeof(q),
		     "select coalesce(max(uid), 0) from logs where "
		     "task_uuid='%s' and run=%d", pesc, pss->sub_run);

	if (sqlite3_exec(pdb, q, sai_sql3_get_uint64_cb, &max_uid, NULL) !=
								SQLITE_OK)
		max_uid = pss->sub_uid; /* don't guess on a query failure */

	sai_event_db_close(&vhd->sqlite3_cache, &pdb);

	return max_uid < pss->sub_uid;
}

static void
saiw_retry_overview(lws_sorted_usec_list_t *sul)
{
	struct pss *pss = lws_container_of(sul, struct pss, sul_overview);

	saiw_browser_queue_overview(pss->vhd, pss);
}

int
saiw_broadcast_logs_batch(struct vhd *vhd, struct pss *pss)
{
	char event_uuid[33];

	if (!pss->subs_list.owner)
		return 0;

	if (lws_buflist2_total_len(&pss->raw_tx) > SAIW_BROWSER_TX_DEFER) {
		lws_sul_schedule(vhd->context, 0, &pss->sul_logcache,
				 saiw_retry_logs, 250 * LWS_US_PER_MS);
		return 0;
	}

	/*
	 * For efficiency, let's try to grab the next 100 at
	 * once from sqlite and work our way through sending
	 * them
	 */

	//if (pss->log_cache_index == pss->log_cache_size)
	{
		sqlite3 *pdb = NULL;
		char esc[256], pesc[132];
		int sr;

		sai_task_uuid_to_event_uuid(event_uuid, pss->sub_task_uuid);

		lwsac_free(&pss->logs_ac);

		/* uuid is db-derived, purify keeps the literal safe anyway */
		lws_sql_purify(pesc, pss->sub_task_uuid, sizeof(pesc));

		/*
		 * Page on uid, the logs table's autoincrement primary key, not
		 * on timestamp: the timestamps are the builder's
		 * CLOCK_MONOTONIC, so they restart from near zero whenever a
		 * builder VM reboots, and rows issued below the cursor are
		 * never delivered.  uid only ever increases, and the rows come
		 * back in uid order anyway.
		 */

		lws_snprintf(esc, sizeof(esc),
		     "and task_uuid='%s' and run=%d and uid > %llu",
		     pesc, pss->sub_run,
		     (unsigned long long)pss->sub_uid);

		// lwsl_notice("%s: collecting logs %s\n", __func__, esc);

		if (sai_event_db_ensure_open(vhd->context, &vhd->sqlite3_cache,
					     vhd->sqlite3_path_lhs, event_uuid,
					     0, &pdb)) {
			uint8_t buf[LWS_PRE + 128];
			int n1;
			lwsl_notice("%s: unable to open event-specific database\n",
					__func__);

			n1 = lws_snprintf((char *)buf + LWS_PRE, sizeof(buf) - LWS_PRE,
				     "{\"schema\":\"com.warmcat.sai.event_deleted\"}");
			saiw_ws_browser_queue_REQUIRES_LWS_PRE(pss, buf + LWS_PRE, (size_t)n1,
						       LWS_WRITE_TEXT);

			return 0;
		}

		sr = lws_struct_sq3_deserialize(pdb, esc,
						"uid ",
						lsm_schema_sq3_map_log,
						&pss->logs_owner,
						&pss->logs_ac, 0, 50);

		sai_event_db_close(&vhd->sqlite3_cache, &pdb);

		if (sr) {

			lwsl_err("%s: subs failed\n", __func__);

			return 0;
		}

		pss->log_cache_index = 0;
		pss->log_cache_size = (int)pss->logs_owner.count;
	}

	while (pss->log_cache_index++ < pss->log_cache_size) {
		sai_log_t *log = lws_container_of(pss->logs_owner.head,
						  sai_log_t, list);
		lws_struct_serialize_t *js;
		char buf[1200 + LWS_PRE];
		char fi = 1;
		int n;

		lws_dll2_remove(&log->list);

		/*
		 * Turn it back into JSON so we can give it to
		 * the browser
		 */

		js = lws_struct_json_serialize_create(lsm_schema_json_map_log,
						      1, 0, log);
		if (!js) {
			lwsl_notice("%s: json ser fail\n", __func__);
			return 0;
		}

		do {
			size_t w;
			n = lws_struct_json_serialize(js, (uint8_t *)buf + LWS_PRE,
						      sizeof(buf) - LWS_PRE, &w);

			if (n != LSJS_RESULT_CONTINUE)
				lws_struct_json_serialize_destroy(&js);
			if (n == LSJS_RESULT_ERROR)
				return 1;

			saiw_ws_browser_queue_REQUIRES_LWS_PRE(pss, buf + LWS_PRE, w,
					lws_write_ws_flags(LWS_WRITE_TEXT,
						fi, n == LSJS_RESULT_FINISH));

			fi = 0;
			pss->sub_timestamp = log->timestamp;
			pss->sub_uid = (uint64_t)log->uid;
		} while (n != LSJS_RESULT_FINISH);
	}

	lwsac_free(&pss->logs_ac);

	lws_sul_schedule(vhd->context, 0, &pss->sul_logcache,
			 saiw_retry_logs,
			 pss->log_cache_size == 50 ? 500 : 250 * LWS_US_PER_MS);

	return 0;
}

/*
 * For sidebar-scoped overviews we don't ship the full per-event task array
 * (it can be hundreds of tasks per event and blow the 2MiB buflist sanity
 * limit when many events match).  Instead we compute a short summary string
 * server-side, matching the format the browser's summarize_build_situation()
 * produces ("All N passed", "OK: g, Bad: b, Building: o, Wait: p", ...).
 *
 * The task state values are the SAIES_* enum, bucketed the same way as the
 * browser: 0 -> pending, {1,2,6} -> ongoing, 3 -> good, {4,5} -> bad.
 *
 * The rss feed reports the same counts, see w-rss.c.
 */
void
saiw_event_summary_string(sqlite3 *pdb_event, const char *event_uuid,
			  char *out, size_t out_len,
			  unsigned int *p_good, unsigned int *p_bad,
			  unsigned int *p_ongoing, unsigned int *p_pending,
			  unsigned int *p_total)
{
	sqlite3_stmt *stmt = NULL;
	unsigned int good = 0, bad = 0, ongoing = 0, pending = 0, total = 0;
	char q[160];
	int rc;

	if (p_good)    *p_good = 0;
	if (p_bad)     *p_bad = 0;
	if (p_ongoing) *p_ongoing = 0;
	if (p_pending) *p_pending = 0;
	if (p_total)   *p_total = 0;

	if (!pdb_event || !event_uuid || !out || out_len < 16)
		goto empty;

	/*
	 * tasks holds one row per (task, run); the browser summarises on the
	 * latest (max) run per task uuid.  Pick exactly that row per uuid using
	 * a correlated max-run subquery, so we count each task once.
	 *
	 * Idle tasks don't count, they're not part of the event's result.
	 */
	lws_snprintf(q, sizeof(q),
			"SELECT state FROM tasks t WHERE idle=0 AND rowid = ("
			"SELECT rowid FROM tasks t2 WHERE t2.uuid = t.uuid "
			"ORDER BY t2.run DESC LIMIT 1)");

	if (sqlite3_prepare_v2(pdb_event, q, -1, &stmt, NULL) != SQLITE_OK)
		goto empty;

	while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
		int s = sqlite3_column_int(stmt, 0);
		total++;
		switch (s) {
		case 0:  pending++;  break;
		case 1:
		case 2:
		case 6:  ongoing++; break;
		case 3:  good++;    break;
		case 4:
		case 5:  bad++;     break;
		default: break;
		}
	}
	sqlite3_finalize(stmt);

	if (p_good)    *p_good = good;
	if (p_bad)     *p_bad = bad;
	if (p_ongoing) *p_ongoing = ongoing;
	if (p_pending) *p_pending = pending;
	if (p_total)   *p_total = total;

	if (!total)
		goto empty;

	if (good == total)
		lws_snprintf(out, out_len, "All %u passed", good);
	else if (bad == total)
		lws_snprintf(out, out_len, "All %u failed", bad);
	else if (pending == total)
		lws_snprintf(out, out_len, "%u pending", total);
	else {
		char *o = out;
		size_t l = out_len, n;
		int first = 1;
#define EMIT(fmt, ...) do { \
		n = (size_t)lws_snprintf(o, l, "%s" fmt, first ? "" : ", ", ##__VA_ARGS__); \
		if (n >= l) { o = out + out_len - 1; break; } o += n; l -= n; first = 0; \
	} while (0)
		if (good)    EMIT("OK: %u", good);
		if (bad)     EMIT("Bad: %u", bad);
		if (ongoing) EMIT("Building: %u", ongoing);
		if (pending) EMIT("Wait: %u", pending);
#undef EMIT
	}
	return;

empty:
	if (out && out_len)
		out[0] = '\0';
}

int
saiw_browser_queue_overview(struct vhd *vhd, struct pss *pss)
{
	char buf[4096 + LWS_PRE], *start = buf + LWS_PRE, *p = start,
	     *end = buf + sizeof(buf);
	char esc[256], filt[448], subsequent;
	struct lwsac *task_ac = NULL, *ac = NULL;
	lws_dll2_owner_t task_owner, owner;
	unsigned int task_index = 0;
	lws_struct_serialize_t *js;
	sqlite3 *pdb = NULL;
	lws_dll2_t *walk;
	sai_task_t *t;
	int n;
	size_t w;

	if (lws_buflist2_total_len(&pss->raw_tx) > SAIW_BROWSER_TX_DEFER) {
		/*
		 * Our own tx towards this browser is backed-up (he is not
		 * draining, or a previous overview is still in flight).  The
		 * overview can be megabytes on a populated db, so composing
		 * another one now would just pile it onto the backlog; come
		 * back from a timer when it has drained.
		 */
		lws_sul_schedule(vhd->context, 0, &pss->sul_overview,
				 saiw_retry_overview, 250 * LWS_US_PER_MS);

		return 0;
	}

	filt[0] = '\0';
	esc[0] = '\0';
	n = -6;

	if (pss->specific_task[0] && !pss->resolved_task_offset) {
		char event_uuid[33];
		char q[256];
		sqlite3_stmt *stmt = NULL;
		uint64_t ev_created = 0;

		sai_task_uuid_to_event_uuid(event_uuid, pss->specific_task);
		lws_snprintf(q, sizeof(q), "SELECT created FROM events WHERE uuid='%s'", event_uuid);
		if (sqlite3_prepare_v2(vhd->pdb, q, -1, &stmt, NULL) == SQLITE_OK) {
			if (sqlite3_step(stmt) == SQLITE_ROW) {
				ev_created = (uint64_t)sqlite3_column_int64(stmt, 0);
			}
			sqlite3_finalize(stmt);
		}
		if (ev_created > 0) {
			unsigned int events_newer = 0;
			lws_snprintf(q, sizeof(q), "SELECT COUNT(*) FROM events WHERE state != %d AND created > %llu%s", SAIES_DELETED, (unsigned long long)ev_created, saiw_visible_sql(vhd));
			if (sqlite3_prepare_v2(vhd->pdb, q, -1, &stmt, NULL) == SQLITE_OK) {
				if (sqlite3_step(stmt) == SQLITE_ROW) {
					events_newer = (unsigned int)sqlite3_column_int(stmt, 0);
				}
				sqlite3_finalize(stmt);
			}
			pss->overview_offset = (events_newer / 6) * 6;
		}
		pss->resolved_task_offset = 1;
	}

	/*
	 * The vhost may only show some projects: that clause goes right after
	 * the first one, so if anything is going to be truncated off the end
	 * of filt, it isn't that.
	 */

	if (pss->specific_project[0]) {
		/*
		 * gitohashi /git/<project> URL-locked mode: lock to that one
		 * project, no row cap.
		 */
		lws_sql_purify(esc, pss->specific_project, sizeof(esc) - 1);
		lws_snprintf(filt, sizeof(filt),
			 " and state != %d%s and repo_name='%s'",
			 SAIES_DELETED, saiw_visible_sql(vhd), esc);
		n = -1;
	} else {
		size_t fl;

		/*
		 * Base filter: hide deleted events.  We keep appending extra
		 * " and ..." clauses here; the COUNT query below skips the
		 * leading " and " with filt + 5 and uses the rest verbatim.
		 */
		lws_snprintf(filt, sizeof(filt), " and state != %d%s",
			 SAIES_DELETED, saiw_visible_sql(vhd));

		/*
		 * A specific event selection (com.warmcat.sai.eventinfo from
		 * the browser clicking an event) takes precedence: scope to
		 * just that one event uuid.  Full task data is emitted for it
		 * (the summary-only path below is skipped in this case).
		 */
		if (pss->event_tasks_uuid[0]) {
			fl = strlen(filt);
			lws_sql_purify(esc, pss->event_tasks_uuid,
				       sizeof(esc) - 1);
			lws_snprintf(filt + fl, sizeof(filt) - fl,
				     " and uuid='%s'", esc);
			n = -1;
		}

		/*
		 * Sidebar runtime selection: scope to the browser's currently
		 * selected project and (if set) branch, capping at the newest
		 * 100 matching events.
		 */
		if (!pss->event_tasks_uuid[0] && pss->selected_project[0]) {
			fl = strlen(filt);
			lws_sql_purify(esc, pss->selected_project,
				       sizeof(esc) - 1);
			lws_snprintf(filt + fl, sizeof(filt) - fl,
					     " and repo_name='%s'", esc);
			n = -100;
		}

		if (!pss->event_tasks_uuid[0] && pss->selected_ref[0]) {
			fl = strlen(filt);
			lws_sql_purify(esc, pss->selected_ref,
				       sizeof(esc) - 1);
			lws_snprintf(filt + fl, sizeof(filt) - fl,
					     " and ref='%s'", esc);
			if (n == -6)
				n = -100;
		}
	}

	unsigned int total_events = 0;
	{
		char q[64 + sizeof(filt)];
		sqlite3_stmt *stmt;
		lws_snprintf(q, sizeof(q), "SELECT COUNT(*) FROM events WHERE %s", filt + 5);
		if (sqlite3_prepare_v2(vhd->pdb, q, -1, &stmt, NULL) == SQLITE_OK) {
			if (sqlite3_step(stmt) == SQLITE_ROW)
				total_events = (unsigned int)sqlite3_column_int(stmt, 0);
			sqlite3_finalize(stmt);
		}
	}

	pss->wants_event_updates = 1;
	if (lws_struct_sq3_deserialize(vhd->pdb, filt[0] ? filt : NULL,
				       "created ", lsm_schema_sq3_map_event,
				       &owner, &ac, (int)pss->overview_offset, n)) {
		lwsl_notice("%s: OVERVIEW 2 failed\n", __func__);

		return 0;
	}

	/*
	 * we get zero or more sai_event_t laid out in pss->query_ac,
	 * and listed in pss->query_owner
	 */

	p += (size_t)lws_snprintf((char *)p, lws_ptr_diff_size_t(end, p),
		"{\"schema\":\"sai.warmcat.com.overview\","
		" \"api_version\":%u,"
		" \"alang\":\"%s\","
		" \"total_events\":%u,"
		" \"offset\":%u,"
		"\"overview\":[", SAIW_API_VERSION,
		lws_json_purify(esc, pss->alang, sizeof(esc) - 1, NULL),
		total_events, pss->overview_offset
	);

	saiw_ws_browser_queue_REQUIRES_LWS_PRE(pss, start,
					       lws_ptr_diff_size_t(p, start),
					       lws_write_ws_flags(LWS_WRITE_TEXT, 1, 0));
	p = start;


	/*
	 * Walk through events
	 */

	if (pss->specificity && pss->specificity != SAIM_SPECIFIC_TASK)
		walk = lws_dll2_get_head(&owner);
	else
		walk = lws_dll2_get_tail(&owner);

	subsequent = 0;

	if (!owner.count) /* nothing to do */
		goto so_finish;

	while (walk) {
		sai_event_t *e = lws_container_of(walk, sai_event_t, list);

		if (pss->specificity && pss->specificity != SAIM_SPECIFIC_TASK) {
			if (!strcmp(pss->specific_ref, "refs/heads/master") &&
			    !strcmp(e->ref, "refs/heads/main"))
				; // any = 1;
			else {
				if (strcmp(e->hash, pss->specific_ref) &&
				    strcmp(e->ref, pss->specific_ref)) {
					walk = walk->next;
					continue;
				}
				// any = 1;
			}
		}

		{
			char wfilt[128], wesc[70];
			struct lwsac *ac_watchers = NULL;
			lws_dll2_owner_clear(&e->watcher_owner);
			/* uuid is db-derived, purify keeps the literal safe anyway */
			lws_sql_purify(wesc, e->uuid, sizeof(wesc));
			lws_snprintf(wfilt, sizeof(wfilt), " and event_hash='%s'", wesc);
			if (lws_struct_sq3_deserialize(vhd->pdb, wfilt, "created",
						   lsm_schema_sq3_map_watcher, &e->watcher_owner, &ac_watchers, 0, 0) < 0)
				lwsl_err("%s: watchers deserialize failed\n", __func__);

		js = lws_struct_json_serialize_create(
			lsm_schema_json_map_event,
			LWS_ARRAY_SIZE(lsm_schema_json_map_event), 0, e);
		if (!js) {
			lwsl_err("%s: json ser fail\n", __func__);
			return 1;
		}
		if (lws_ptr_diff_size_t(end, p) < 128) {
			saiw_ws_browser_queue_REQUIRES_LWS_PRE(pss, start,
							       lws_ptr_diff_size_t(p, start),
							       lws_write_ws_flags(LWS_WRITE_TEXT, 0, 0));
			p = start;
		}

		if (subsequent)
			*p++ = ',';
		subsequent = 1;

		p += lws_snprintf((char *)p, lws_ptr_diff_size_t(end, p), "{\"e\":");


		do {
			n = (int)lws_struct_json_serialize(js, (uint8_t *)p, lws_ptr_diff_size_t(end, p), &w);
			switch (n) {
			case LSJS_RESULT_ERROR:
				lwsl_err("%s: json ser error\n", __func__);
				lws_struct_json_serialize_destroy(&js);
				return 1;

			case LSJS_RESULT_FINISH:
				lws_struct_json_serialize_destroy(&js);
				p += w;
				break;

			case LSJS_RESULT_CONTINUE:
				p += w;
				saiw_ws_browser_queue_REQUIRES_LWS_PRE(pss, start,
								       lws_ptr_diff_size_t(p, start),
								       lws_write_ws_flags(LWS_WRITE_TEXT, 0, 0));
				p = start;
				break;
			}
		} while (n == LSJS_RESULT_CONTINUE);

		if (ac_watchers)
			lwsac_free(&ac_watchers);
		}

		if (lws_ptr_diff_size_t(end, p) < 128) {
			saiw_ws_browser_queue_REQUIRES_LWS_PRE(pss, start,
							       lws_ptr_diff_size_t(p, start),
							       lws_write_ws_flags(LWS_WRITE_TEXT, 0, 0));
			p = start;
		}

		task_index = 0;

		/*
		 * For sidebar-scoped overviews (project / branch selected in
		 * the LHS pane), we don't ship the full per-event task array:
		 * it can be hundreds of tasks per event and overflow the 2MiB
		 * buflist when many events match.  The sidebar only needs the
		 * event metadata + a short summary string, so emit an empty
		 * "t":[] plus a server-computed "summary" the browser shows
		 * verbatim.  The full task list is fetched on demand when the
		 * user selects a specific event.
		 */
		if ((pss->selected_project[0] || pss->selected_ref[0]) &&
		    !pss->event_tasks_uuid[0]) {
			/*
			 * Sidebar-scoped path: emit a tiny per-event payload
			 * (empty task array + server-computed summary) instead
			 * of the full task list, so many events fit the buflist.
			 * We close the {"e":...} wrapper object here with '}'
			 * and skip the shared "]" below (which closes the task
			 * array opened by the unscoped path).
			 */
				char sum[96], esc_sum[128];
				unsigned int sg = 0, sb = 0, so = 0, sp = 0, st = 0;

				e = lws_container_of(walk, sai_event_t, list);
				sum[0] = '\0';
				if (!sai_event_db_ensure_open(vhd->context,
						&vhd->sqlite3_cache,
						vhd->sqlite3_path_lhs, e->uuid, 0, &pdb)) {
					saiw_event_summary_string(pdb, e->uuid, sum,
								  sizeof(sum),
								  &sg, &sb, &so, &sp, &st);
					sai_event_db_close(&vhd->sqlite3_cache, &pdb);
				}

				if (lws_ptr_diff_size_t(end, p) < 160) {
					saiw_ws_browser_queue_REQUIRES_LWS_PRE(pss,
						start, lws_ptr_diff_size_t(p, start),
						lws_write_ws_flags(LWS_WRITE_TEXT, 0, 0));
					p = start;
				}
				p += lws_snprintf((char *)p, lws_ptr_diff_size_t(end, p),
					", \"t\":[], \"summary\":\"%s\","
					"\"sum_counts\":{\"good\":%u,\"bad\":%u,"
					"\"ongoing\":%u,\"pending\":%u,\"total\":%u}}",
					lws_json_purify(esc_sum, sum, sizeof(esc_sum) - 1,
							NULL),
					sg, sb, so, sp, st);

			/* advance to the next event like the unscoped path */
			if (pss->specificity && pss->specificity != SAIM_SPECIFIC_TASK)
				walk = walk->next;
			else
				walk = walk->prev;

			if (walk && (!pss->specificity ||
				     pss->specificity == SAIM_SPECIFIC_TASK))
				continue;
			goto so_finish;
		} else {
		p += lws_snprintf((char *)p, lws_ptr_diff_size_t(end, p), ", \"t\":[");


		/*
		 * Enumerate the tasks associated with this event...
		 */

		e = lws_container_of(walk, sai_event_t, list);
		lws_dll2_owner_clear(&task_owner);

		task_index = 0;

		if (sai_event_db_ensure_open(vhd->context, &vhd->sqlite3_cache,
				      vhd->sqlite3_path_lhs, e->uuid, 0, &pdb)) {
			lwsl_err("%s: unable to open event-specific database\n",
					__func__);
		} else {
			task_ac = NULL;
			lws_dll2_owner_clear(&task_owner);
			if (lws_struct_sq3_deserialize(pdb, NULL, "taskname, platform",
					lsm_schema_sq3_map_task, &task_owner,
					&task_ac, 0, 999)) {
				lwsl_err("%s: OVERVIEW 1 failed\n", __func__);
			} else {
				lws_start_foreach_dll(struct lws_dll2 *, pt, task_owner.head) {
					t = lws_container_of(pt, sai_task_t, list);

					if (lws_ptr_diff_size_t(end, p) < 128) {
						saiw_ws_browser_queue_REQUIRES_LWS_PRE(pss, start,
										       lws_ptr_diff_size_t(p, start),
										       lws_write_ws_flags(LWS_WRITE_TEXT, 0, 0));
						p = start;
					}

					if (task_index)
						*p++ = ',';

					/*
					 * We don't want to send everyone the artifact nonces...
					 * the up nonce is a key for uploading artifacts on to
					 * this task, it should only be stored in the server db
					 * and sent to the builder to use.
					 *
					 * The down nonce is used in generated links, but still
					 * you should have to acquire such a link via whatever
					 * auth rather than be able to cook them up yourself
					 * from knowing the task uuid.
					 */

					t->art_up_nonce[0] = '\0';
					t->art_down_nonce[0] = '\0';

					t->rebuildable = (t->state == SAIES_FAIL || t->state == SAIES_CANCELLED) &&
						(lws_now_secs() - (t->started + t->duration / 1000000) < 24 * 3600);

					js = lws_struct_json_serialize_create(
						lsm_schema_json_map_task,
						LWS_ARRAY_SIZE(lsm_schema_json_map_task), 0, t);

					t->build[0] = '\0';

					do {
						n = (int)lws_struct_json_serialize(js, (uint8_t *)p, lws_ptr_diff_size_t(end, p), &w);
						switch (n) {
						case LSJS_RESULT_ERROR:
							lwsl_err("%s: json ser error for task\n", __func__);
							lws_struct_json_serialize_destroy(&js);
							return 1;

						case LSJS_RESULT_FINISH:
							lws_struct_json_serialize_destroy(&js);
							p += w;
							break;

						case LSJS_RESULT_CONTINUE:
							p += w;
							saiw_ws_browser_queue_REQUIRES_LWS_PRE(pss, start,
											       lws_ptr_diff_size_t(p, start),
											       lws_write_ws_flags(LWS_WRITE_TEXT, 0, 0));
							p = start;
							break;
						}
					} while (n == LSJS_RESULT_CONTINUE);

					task_index++;
				} lws_end_foreach_dll(pt);
			}

			lwsac_free(&task_ac);
			sai_event_db_close(&vhd->sqlite3_cache, &pdb);
		}
		} /* end of full-task (unscoped) path */

		/* none left to do, go back up a level */

		if (lws_ptr_diff_size_t(end, p) < 128) {
			saiw_ws_browser_queue_REQUIRES_LWS_PRE(pss, start,
							       lws_ptr_diff_size_t(p, start),
							       lws_write_ws_flags(LWS_WRITE_TEXT, 0, 0));
			p = start;
		}

		p += lws_snprintf((char *)p, lws_ptr_diff_size_t(end, p), "]}");

		if (pss->specificity && pss->specificity != SAIM_SPECIFIC_TASK)
			walk = walk->next;
		else
			walk = walk->prev;

		if (walk && (!pss->specificity || pss->specificity == SAIM_SPECIFIC_TASK))
			continue;
	}

so_finish:
	if (lws_ptr_diff_size_t(end, p) < 16) {
		saiw_ws_browser_queue_REQUIRES_LWS_PRE(pss, start,
						       lws_ptr_diff_size_t(p, start),
						       lws_write_ws_flags(LWS_WRITE_TEXT, 0, 0));
		p = start;
	}

	p += lws_snprintf((char *)p, lws_ptr_diff_size_t(end, p), "]}");

	saiw_ws_browser_queue_REQUIRES_LWS_PRE(pss, start,
					       lws_ptr_diff_size_t(p, start),
					       lws_write_ws_flags(LWS_WRITE_TEXT, 0, 1));

	/*
	 * Consume the one-shot single-event hint (set by eventinfo) so later
	 * overview pushes for the multi-event sidebar list aren't locked to it.
	 */
	pss->event_tasks_uuid[0] = '\0';

	return 0;
}

struct sai_dyn_buf {
	char *buf;
	size_t len;
	size_t alloc;
};

static int
sai_dyn_buf_ensure(struct sai_dyn_buf *d, size_t needed)
{
	if (d->len + needed <= d->alloc)
		return 0;
	size_t na = d->alloc ? d->alloc * 2 : 4096;
	while (d->len + needed > na)
		na *= 2;
	char *nb = realloc(d->buf, na);
	if (!nb)
		return 1;
	d->buf = nb;
	d->alloc = na;
	return 0;
}

static inline int
sai_dyn_buf_append(struct sai_dyn_buf *d, const void *p, size_t len)
{
	if (sai_dyn_buf_ensure(d, len))
		return 1;
	memcpy(d->buf + d->len, p, len);
	d->len += len;
	return 0;
}

static int
saiw_dedup_and_queue(struct pss *pss, int idx, struct sai_dyn_buf *d)
{
	int changed = 1;

	/* check if we changed versus last payload */
	if (pss->last_bps[idx] && pss->last_bps_len[idx] == d->len - LWS_PRE &&
	    !memcmp(pss->last_bps[idx], d->buf + LWS_PRE, d->len - LWS_PRE)) {
		changed = 0;
	} else {
		free(pss->last_bps[idx]);
		pss->last_bps[idx] = NULL;
		pss->last_bps_len[idx] = 0;
		pss->last_bps[idx] = malloc(d->len - LWS_PRE);
		if (pss->last_bps[idx]) {
			memcpy(pss->last_bps[idx], d->buf + LWS_PRE, d->len - LWS_PRE);
			pss->last_bps_len[idx] = d->len - LWS_PRE;
		}
	}

	if (changed)
		saiw_ws_browser_queue_REQUIRES_LWS_PRE(pss, d->buf + LWS_PRE,
						       d->len - LWS_PRE,
						       lws_write_ws_flags(LWS_WRITE_TEXT, 1, 1));

	free(d->buf);
	d->buf = NULL;
	return 0;
}

int
saiw_browser_broadcast_queue_pcon_energy(struct vhd *vhd, struct pss *pss, sai_pcon_energy_report_t *energy)
{
	struct sai_dyn_buf d;
	char buf[1024];
	lws_struct_serialize_t *js;
	lws_struct_json_serialize_result_t r;
	size_t w;

	if (pss && !pss->wants_builder_info)
		return 0;

	if (!vhd || !energy || !pss)
		return 0;

	memset(&d, 0, sizeof(d));

	/* Reserve LWS_PRE header space */
	memset(buf, 0, LWS_PRE);
	if (sai_dyn_buf_append(&d, buf, LWS_PRE))
		return 1;

	js = lws_struct_json_serialize_create(
		lsm_schema_pcon_energy,
		LWS_ARRAY_SIZE(lsm_schema_pcon_energy),
		0, energy);
	if (!js) {
		free(d.buf);
		return 1;
	}

	do {
		r = lws_struct_json_serialize(js, (uint8_t *)buf, sizeof(buf), &w);

		if (w && sai_dyn_buf_append(&d, buf, w)) {
			lws_struct_json_serialize_destroy(&js);
			free(d.buf);
			return 1;
		}

		if (r == LSJS_RESULT_ERROR) {
			lws_struct_json_serialize_destroy(&js);
			free(d.buf);
			return 1;
		}
	} while (r == LSJS_RESULT_CONTINUE);

	lws_struct_json_serialize_destroy(&js);

	return saiw_dedup_and_queue(pss, 2, &d);
}

int
saiw_browser_broadcast_queue_pcons(struct vhd *vhd, struct pss *pss)
{
	struct sai_dyn_buf d;
	char buf[1024]; /* temp buffer for serialization before append */
	lws_struct_serialize_t *js;
	sai_power_managed_builders_t pmb;
	lws_struct_json_serialize_result_t r;
	size_t w;

	if (pss && !pss->wants_builder_info)
		return 0;

	if (!vhd || !vhd->pcons || !pss)
		return 0;

	memset(&d, 0, sizeof(d));

	/* Reserve LWS_PRE header space */
	memset(buf, 0, LWS_PRE);
	if (sai_dyn_buf_append(&d, buf, LWS_PRE))
		return 1;

	memset(&pmb, 0, sizeof(pmb));
	pmb.power_controllers = vhd->pcons_owner;

	js = lws_struct_json_serialize_create(
		lsm_schema_power_managed_builders,
		LWS_ARRAY_SIZE(lsm_schema_power_managed_builders),
		0, &pmb);
	if (!js) {
		free(d.buf);
		return 1;
	}

	do {
		r = lws_struct_json_serialize(js, (uint8_t *)buf, sizeof(buf), &w);

		if (sai_dyn_buf_append(&d, buf, w)) {
			lws_struct_json_serialize_destroy(&js);
			free(d.buf);
			return 1;
		}

		if (r == LSJS_RESULT_ERROR) {
			lws_struct_json_serialize_destroy(&js);
			free(d.buf);
			return 1;
		}
	} while (r == LSJS_RESULT_CONTINUE);

	lws_struct_json_serialize_destroy(&js);

	return saiw_dedup_and_queue(pss, 1, &d);
}

int
saiw_browser_broadcast_queue_builders(struct vhd *vhd, struct pss *pss)
{
	struct sai_dyn_buf d;
	char buf[1024]; /* temp buffer for serialization before append */
	lws_struct_serialize_t *js;
	char esc[256];
	lws_dll2_t *walk = NULL;
	char subsequent;
	size_t w;
	int n;

	if (pss && !pss->wants_builder_info)
		return 0;

	if (!vhd || !vhd->builders || !pss)
		return 0;

	saiw_browser_broadcast_queue_pcons(vhd, pss);

	memset(&d, 0, sizeof(d));

	/* Reserve LWS_PRE header space */
	memset(buf, 0, LWS_PRE);
	if (sai_dyn_buf_append(&d, buf, LWS_PRE))
		return 1;

	n = lws_snprintf(buf, sizeof(buf),
			  "{\"schema\":\"com.warmcat.sai.builders\","
			  " \"alang\":\"%s\","
			  " \"builders\":[",
			  lws_json_purify(esc, pss->alang, sizeof(esc) - 1,
					  NULL));
	if (sai_dyn_buf_append(&d, buf, (size_t)n)) {
		free(d.buf);
		return 1;
	}

	if (vhd && vhd->builders)
		walk = lws_dll2_get_head(&vhd->builders_owner);

	subsequent = 0;

	while (walk) {
		sai_plat_t *b = lws_container_of(walk, sai_plat_t, sai_plat_list);
		lws_struct_json_serialize_result_t r;
		char start_of_this_builder = 1;

		lwsl_info("%s: processing builder '%s' (online %d)\n", __func__, b->name, b->online);

		js = lws_struct_json_serialize_create(
			lsm_schema_map_plat_simple,
			LWS_ARRAY_SIZE(lsm_schema_map_plat_simple),
			0, b);
		if (!js) {
			free(d.buf);
			return 1;
		}

		do {
			if (subsequent && start_of_this_builder) {
				if (sai_dyn_buf_append(&d, ",", 1)) {
					lws_struct_json_serialize_destroy(&js);
					free(d.buf);
					return 1;
				}
				start_of_this_builder = 0;
			}

			r = lws_struct_json_serialize(js, (uint8_t *)buf, sizeof(buf), &w);

			if (w && sai_dyn_buf_append(&d, buf, w)) {
				lws_struct_json_serialize_destroy(&js);
				free(d.buf);
				return 1;
			}

			switch (r) {
			case LSJS_RESULT_ERROR:
				lws_struct_json_serialize_destroy(&js);
				free(d.buf);
				return 1;
			case LSJS_RESULT_CONTINUE:
			case LSJS_RESULT_FINISH:
				break;
			}
		} while (r == LSJS_RESULT_CONTINUE);

		lws_struct_json_serialize_destroy(&js);

		subsequent = 1;
		walk = walk->next;
	}

	n = lws_snprintf(buf, sizeof(buf), " \n]}");
	if (sai_dyn_buf_append(&d, buf, (size_t)n)) {
		free(d.buf);
		return 1;
	}

	
	saiw_browser_broadcast_queue_power_history(vhd, pss);

	return saiw_dedup_and_queue(pss, 0, &d);
}

/*
 * This should be called from the browser-facing websocket protocol handler
 * on LWS_CALLBACK_ESTABLISHED and LWS_CALLBACK_CLOSED events to keep an
 * accurate real-time list of connected browsers.
 */
void
saiw_browser_state_changed(struct pss *pss, int established)
{
	if (established)
		lws_dll2_add_tail(&pss->same, &pss->vhd->browsers);
	else
		lws_dll2_remove(&pss->same);

	/*
	 * After any change, recalculate the total and inform the server
	 */
	saiw_update_viewer_count(pss->vhd);
}

/*
 * Track which builder shells this browser opened itself (the rx side only
 * lets admins send openshell/closeshell).  Shell ptydata coming back from
 * the server is delivered only to connections on this list.
 */
void
saiw_pss_shell_open(struct pss *pss, const char *task_uuid)
{
	unsigned int n;

	if (!task_uuid[0])
		return;

	for (n = 0; n < pss->shell_count; n++)
		if (!strcmp(pss->shell_task_uuid[n], task_uuid))
			return;

	if (pss->shell_count >= SAIW_MAX_SHELLS) {
		lwsl_wsi_notice(pss->wsi,
				"shell tracking full, ptydata for %s will "
				"not be delivered here", task_uuid);
		return;
	}

	lws_strncpy(pss->shell_task_uuid[pss->shell_count], task_uuid,
		    sizeof(pss->shell_task_uuid[0]));
	pss->shell_count++;
}

void
saiw_pss_shell_close(struct pss *pss, const char *task_uuid)
{
	unsigned int n;

	for (n = 0; n < pss->shell_count; n++) {
		if (!strcmp(pss->shell_task_uuid[n], task_uuid)) {
			memmove(&pss->shell_task_uuid[n],
				&pss->shell_task_uuid[n + 1],
				(pss->shell_count - n - 1) *
					sizeof(pss->shell_task_uuid[0]));
			pss->shell_count--;

			return;
		}
	}
}

int
saiw_pss_owns_shell(struct pss *pss, const char *task_uuid)
{
	unsigned int n;

	for (n = 0; n < pss->shell_count; n++)
		if (!strcmp(pss->shell_task_uuid[n], task_uuid))
			return 1;

	return 0;
}




typedef struct pcon_watts {
	lws_dll2_t list;
	char name[64];
	unsigned int active_power_w;
} pcon_watts_t;

void
saiw_update_global_power_history(struct vhd *vhd, sai_pcon_energy_report_t *energy)
{
	unsigned int total_w;
	char buf[1024];

	if (!vhd || !energy)
		return;

	/* Record or update individual PCON wattages */
	lws_start_foreach_dll(struct lws_dll2 *, p, energy->items.head) {
		sai_pcon_energy_report_item_t *item = lws_container_of(p, sai_pcon_energy_report_item_t, list);
		pcon_watts_t *pw = NULL;

		lws_start_foreach_dll(struct lws_dll2 *, pt, vhd->pcon_watts_owner.head) {
			pcon_watts_t *pw_iter = lws_container_of(pt, pcon_watts_t, list);
			if (!strcmp(pw_iter->name, item->name)) {
				pw = pw_iter;
				break;
			}
		} lws_end_foreach_dll(pt);

		if (!pw) {
			pw = malloc(sizeof(*pw));
			if (pw) {
				memset(pw, 0, sizeof(*pw));
				lws_strncpy(pw->name, item->name, sizeof(pw->name));
				lws_dll2_add_tail(&pw->list, &vhd->pcon_watts_owner);
			}
		}
		if (pw)
			pw->active_power_w = item->data.active_power_w;

	} lws_end_foreach_dll(p);

	/* Calculate global watts across all tracked PCONs */
	total_w = 0;
	lws_start_foreach_dll(struct lws_dll2 *, pt, vhd->pcon_watts_owner.head) {
		pcon_watts_t *pw = lws_container_of(pt, pcon_watts_t, list);
		total_w += pw->active_power_w;
	} lws_end_foreach_dll(pt);

	/* Record this sample */
	if (vhd->power_history_count == 150) {
		memmove(vhd->power_history, vhd->power_history + 1, sizeof(unsigned int) * 149);
		vhd->power_history[149] = total_w;
	} else {
		vhd->power_history[vhd->power_history_count++] = total_w;
	}

	if (total_w > vhd->max_total_power_w) {
		vhd->max_total_power_w = total_w;
		if (vhd->pdb) {
			lws_snprintf(buf, sizeof(buf), "INSERT OR REPLACE INTO saiweb_state (key, val) VALUES ('max_power', %u)", total_w);
			sai_sqlite3_statement(vhd->pdb, buf, "update max_power");
		}
	}
}

int
saiw_browser_broadcast_queue_power_history(struct vhd *vhd, struct pss *pss)
{
	struct sai_dyn_buf d;
	char buf[2048];
	int n, i;

	if (pss && !pss->wants_builder_info)
		return 0;

	if (!vhd || !pss)
		return 0;

	memset(&d, 0, sizeof(d));

	/* Reserve LWS_PRE header space */
	memset(buf, 0, LWS_PRE);
	if (sai_dyn_buf_append(&d, buf, LWS_PRE))
		return 1;

	n = lws_snprintf(buf, sizeof(buf),
		"{\"schema\":\"com.warmcat.sai.power_history\","
		" \"max_w\":%u,"
		" \"samples\":[", vhd->max_total_power_w);

	if (sai_dyn_buf_append(&d, buf, (size_t)n)) {
		free(d.buf);
		return 1;
	}

	for (i = 0; i < vhd->power_history_count; i++) {
		n = lws_snprintf(buf, sizeof(buf), "%s%u",
				 i ? "," : "",
				 vhd->power_history[i]);
		if (sai_dyn_buf_append(&d, buf, (size_t)n)) {
			free(d.buf);
			return 1;
		}
	}

	n = lws_snprintf(buf, sizeof(buf), "]}");
	if (sai_dyn_buf_append(&d, buf, (size_t)n)) {
		free(d.buf);
		return 1;
	}

	return saiw_dedup_and_queue(pss, 3, &d);
}