Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

fix(jev-browser): a leftover browser window no longer kills the viewer (#157)

Deecd04d89af63b
diff --git a/store/agents/jev-browser/test/viewer.test.mjs b/store/agents/jev-browser/test/viewer.test.mjs
index baa5ad1f3..3fa659b64 100644
--- a/store/agents/jev-browser/test/viewer.test.mjs
+++ b/store/agents/jev-browser/test/viewer.test.mjs
@@ -465,3 +465,33 @@ test('a whole site is enough: Jev walks to the page the person meant', { skip: n
assert.equal(walled.steps, 1, 'it stops at the wall rather than trying more pages')
} finally { await chrome.close(); await site.close(); delete process.env.JEV_OFFLINE }
})
+
+test('a browser left open on the profile is reported, and does not take the viewer down', { skip: noChrome }, async () => {
+ // Chrome will not open a second window on a profile another Chrome holds: it hands the request
+ // over and quits. That rejection used to be unheard, which killed the viewer process, so the
+ // pane went silent and no browser appeared.
+ const profileDir = mkdtempSync(join(tmpdir(), 'jev-browser-clash-'))
+ const first = await openChrome({ profileDir, show: false, allowedHosts: ['127.0.0.1'] })
+ try {
+ await assert.rejects(() => openChrome({ profileDir, show: false, allowedHosts: ['127.0.0.1'] }),
+ /still open on this harness's profile|would not start/)
+ assert.equal(first.alive, true, 'the first one is untouched')
+ } finally { await first.close() }
+
+ // And a job that starts itself says so rather than going quiet.
+ process.env.JEV_OFFLINE = '1'
+ const ws = mkdtempSync(join(tmpdir(), 'jev-browser-quiet-'))
+ writeFileSync(join(ws, 'browse.json'), '{}')
+ const v = await startBrowserViewer({ workspace: ws, port: 0, show: false })
+ try {
+ const r = await (await fetch(`${v.url}/control`, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ cmd: 'setJob', start: 'not a web address at all', columns: '', want: 'anything' }) })).json()
+ const end = Date.now() + 15000
+ for (;;) {
+ const s = await (await fetch(`${v.url}/state`)).json()
+ if (s.feed.some((e) => e.kind === 'bad')) { assert.equal(s.phase, 'idle', 'it goes back to idle rather than sitting on "running"'); break }
+ if (Date.now() > end) assert.fail(`nothing was ever said: phase ${s.phase}, feed ${JSON.stringify(s.feed.slice(0, 3))}`)
+ await new Promise((r2) => setTimeout(r2, 200))
+ }
+ assert.ok(r.ok !== false || r.error)
+ } finally { await v.close(); delete process.env.JEV_OFFLINE }
+})
diff --git a/store/agents/jev-browser/toolchain/chrome.mjs b/store/agents/jev-browser/toolchain/chrome.mjs
index 99201482c..13d3bdbc3 100644
--- a/store/agents/jev-browser/toolchain/chrome.mjs
+++ b/store/agents/jev-browser/toolchain/chrome.mjs
@@ -75,18 +75,38 @@ export async function openChrome({ profileDir, show = true, allowedHosts = [], w
]
if (!show) args.unshift('--headless=new')
const proc = spawn(chromePath, args, { stdio: 'ignore', detached: false })
- const dead = new Promise((_, reject) => proc.once('exit', (code) => reject(new Error(`Chrome closed (code ${code})`))))
+ // Chrome exiting is expected at the end of every run, so this must never be an unheard rejection:
+ // an unhandled one takes the whole viewer down with it, and then nothing at all happens.
+ let exited = null
+ proc.once('exit', (code) => { exited = code ?? 0 })
+ proc.once('error', () => { exited = -1 })
+ const dead = new Promise((resolve) => proc.once('exit', resolve))
+ dead.catch(() => {})
// Wait for the debugging port. Chrome takes a moment on a cold profile.
let version = null
- for (let i = 0; i < 100 && !version; i++) {
+ for (let i = 0; i < 100 && !version && exited === null; i++) {
try { version = await (await fetch(`http://127.0.0.1:${port}/json/version`)).json() } catch { await sleep(100) }
}
- if (!version) { proc.kill(); throw new Error('Chrome started but never opened its debugging port') }
+ if (!version) {
+ proc.kill()
+ // Chrome will not open a second window on a profile another Chrome is holding: it hands the
+ // request to that window and quits (code 21 on macOS). The fix is to close the other window,
+ // and the person needs telling that, not "it did not start".
+ if (exited !== null) {
+ throw new Error(exited === 21 || exited === 0
+ ? 'A browser window from an earlier run is still open on this harness\'s profile. Close that window and press Go again.'
+ : `Chrome would not start (it exited with code ${exited}).`)
+ }
+ throw new Error('Chrome started but never opened its debugging port')
+ }
const target = await (await fetch(`http://127.0.0.1:${port}/json/new?about:blank`, { method: 'PUT' })).json()
const ws = new WebSocket(target.webSocketDebuggerUrl)
- await Promise.race([new Promise((r, j) => { ws.onopen = r; ws.onerror = () => j(new Error('Chrome refused the debugging connection')) }), dead])
+ await Promise.race([
+ new Promise((r, j) => { ws.onopen = r; ws.onerror = () => j(new Error('Chrome refused the debugging connection')) }),
+ dead.then(() => { throw new Error('Chrome closed before it could be driven') }),
+ ])
let nextId = 1
const pending = new Map()
diff --git a/store/agents/jev-browser/viewer/viewer.mjs b/store/agents/jev-browser/viewer/viewer.mjs
index f95bd031a..a8364505d 100644
--- a/store/agents/jev-browser/viewer/viewer.mjs
+++ b/store/agents/jev-browser/viewer/viewer.mjs
@@ -167,8 +167,9 @@ export async function startBrowserViewer({ workspace, port = 0, show = null } =
demo ??= await startDemoSite()
job = { ...job, start: demo.url, hosts: [...new Set([...job.hosts, '127.0.0.1', 'localhost'])] }
}
- const opened = await openBrowser()
- if (opened.ok === false) return opened
+ let opened
+ try { opened = await openBrowser() } catch (e) { const error = clean(e?.message ?? e); say(error, 'bad'); phase = 'idle'; push(); verdictSoon(); return { ok: false, error } }
+ if (opened.ok === false) { say(opened.error, 'bad'); push(); return opened }
rows = []; links = []; feed = []
// Nobody should have to name the columns. Jev reads one page and proposes them, in about a
// second, and the answer is written back into browse.json so the job stays the recipe.
@@ -342,7 +343,13 @@ export async function startBrowserViewer({ workspace, port = 0, show = null } =
// twice: the person who typed it, or the agent that wrote the file for them, has already said so.
if (changed && !configError && job.autoStart && !running) {
clearTimeout(autoTimer)
- autoTimer = setTimeout(() => { if (!running && !configError) start().catch(() => {}) }, 400)
+ autoTimer = setTimeout(() => {
+ if (running || configError) return
+ // A job that starts itself must still say when it could not. Swallowing this is what made
+ // a browser that would not open look like nothing happening at all.
+ start().then((r) => { if (r?.ok === false) { say(r.error, 'bad'); phase = 'idle'; push(); verdictSoon() } })
+ .catch((e) => { say(clean(e?.message ?? e), 'bad'); phase = 'idle'; proposing = false; push(); verdictSoon() })
+ }, 400)
}
}