ReviewOS

also looking at this

loghqorg/loghq

Promote main to production: signed-out chrome and the subscriber tables

#17
Merged glennmichael123 wants to merge main into production
54 files +4252 -1353

Review threads live on the whole diff, not on one commit, so none are shown here - a thread's line means something in the branch's final form, and painting it into an intermediate step would put it on code it is not about.

scripts/validate-visual.tsmodified+62-3
Changes to scripts/validate-visual.ts
@@ -70,6 +70,48 @@ async function waitFor<T>(read: () => Promise<T> | T, timeout = 12_000): Promise
7070 return value
7171}
7272
73/**
74 * Wait for Chrome to publish its DevTools port, and say something useful when it
75 * does not.
76 *
77 * This used to be a plain `waitFor(() => existsSync(portFile), 10_000)` against a
78 * Chrome spawned with stderr discarded, so every launch failure — missing shared
79 * library, unwritable profile, sandbox refusal, OOM — arrived as the same four
80 * words, `Timed out after 10000ms`, with the one line that would have explained it
81 * thrown away. It failed twice in three CI runs and was undiagnosable both times.
82 *
83 * Two changes: the process is polled as well as the file, so a Chrome that exits
84 * reports immediately instead of burning the whole timeout first; and whatever it
85 * wrote to stderr is attached to the error.
86 */
87async function waitForDevToolsPort(
88 portFile: string,
89 chrome: { exitCode: number | null },
90 stderr: () => string,
91 chromeBinary: string,
92 timeout = 30_000,
93): Promise<void> {
94 const started = Date.now()
95 const describe = (headline: string): string => {
96 const tail = stderr().trim().split('\n').slice(-12).join('\n')
97 return [
98 headline,
99 ` binary: ${chromeBinary}`,
100 ` profile: ${portFile.replace(/\/DevToolsActivePort$/, '')}`,
101 tail ? ` stderr:\n${tail.replace(/^/gm, ' ')}` : ' stderr: (nothing was written)',
102 ].join('\n')
103 }
104
105 while (!existsSync(portFile)) {
106 // A dead Chrome will never write the file, so there is nothing to wait for.
107 if (chrome.exitCode !== null)
108 throw new Error(describe(`Chrome exited with code ${chrome.exitCode} before opening a debugging port.`))
109 if (Date.now() - started >= timeout)
110 throw new Error(describe(`Chrome did not open a debugging port within ${timeout}ms.`))
111 await Bun.sleep(100)
112 }
113}
114
73115class Cdp {
74116 private id = 0
75117 private pending = new Map<number, Pending>()
@@ -237,8 +279,9 @@ async function main(): Promise<void> {
237279 },
238280 })
239281 const profile = mkdtempSync(join(tmpdir(), 'hq-visual-'))
282 const chromeBinary = chromePath()
240283 const chrome = Bun.spawn([
241 chromePath(),
284 chromeBinary,
242285 '--headless=new',
243286 '--remote-debugging-port=0',
244287 `--user-data-dir=${profile}`,
@@ -255,13 +298,28 @@ async function main(): Promise<void> {
255298 '--disable-dev-shm-usage',
256299 '--disable-gpu',
257300 'about:blank',
258 ], { stdout: 'ignore', stderr: 'ignore' })
301 ], { stdout: 'ignore', stderr: 'pipe' })
302
303 // Drained continuously rather than read on failure: a chatty Chrome can fill the
304 // pipe buffer and block on its own output, which would look exactly like the
305 // startup hang this is here to explain.
306 let chromeStderr = ''
307 const drainingStderr = (async () => {
308 try {
309 const decoder = new TextDecoder()
310 for await (const chunk of chrome.stderr as ReadableStream<Uint8Array>)
311 chromeStderr += decoder.decode(chunk, { stream: true })
312 }
313 catch {
314 // The process was killed mid-read; whatever arrived is still worth having.
315 }
316 })()
259317
260318 const failures: string[] = []
261319 let screenshots = 0
262320 try {
263321 const portFile = join(profile, 'DevToolsActivePort')
264 await waitFor(() => existsSync(portFile), 10_000)
322 await waitForDevToolsPort(portFile, chrome, () => chromeStderr, chromeBinary)
265323 const port = Number(readFileSync(portFile, 'utf8').split('\n')[0])
266324 const targets = await waitFor(async () => {
267325 const items = await fetch(`http://127.0.0.1:${port}/json/list`).then(response => response.json()).catch(() => [])
@@ -321,6 +379,7 @@ async function main(): Promise<void> {
321379 finally {
322380 chrome.kill()
323381 await chrome.exited
382 await drainingStderr
324383 server.stop(true)
325384 rmSync(profile, { force: true, recursive: true })
326385 }