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.
TypeScript 3d-modelingagent-frameworkagent-harnessai-agentscadclaude-codecodexcoding-agentsdeveloper-toolsesp32game-developmentmcp-servermulti-agentopen-hardwareopencodeopenharnesspcbroboticsterminaltmux
yosys: the pane's server survives a malformed URL; the schematic renderer moves to viewer/lib/render.mjs and a timed-out job no longer kills the next worker
dee80c969bc05bc
diff --git a/store/agents/yosys/viewer.mjs b/store/agents/yosys/viewer.mjsindex 8b5bae8da..52552d54e 100644--- a/store/agents/yosys/viewer.mjs+++ b/store/agents/yosys/viewer.mjs@@ -14,13 +14,13 @@// Nothing here runs a tool or writes a file: the agent's flow.sh does the work, and the pane reads.// No network, no CDN: the page is plain modules served from this folder.import { createServer } from 'node:http'-import { existsSync, readFileSync, statSync, watch } from 'node:fs'+import { readFileSync, statSync, watch } from 'node:fs'import { dirname, extname, join, normalize, resolve, sep } from 'node:path'import { fileURLToPath } from 'node:url'-import { Worker } from 'node:worker_threads'import { parseVcd, vcdMeta, vcdSignal } from './viewer/lib/vcd.mjs'import { buildChip, packagePins } from './viewer/lib/chip.mjs'-import { hierarchy, moduleForRender, moduleIndex, skinTypes } from './viewer/lib/netlist.mjs'+import { hierarchy, moduleIndex } from './viewer/lib/netlist.mjs'+import { createRenderer } from './viewer/lib/render.mjs'import { fileInfo, findTop, flowState, parsePcf, readJson, readText, sources, topPorts } from './viewer/lib/workspace.mjs'const here = dirname(fileURLToPath(import.meta.url))@@ -43,8 +43,10 @@ function inside(root, rel) {}function send(res, status, body, type = 'application/json') {+ // The body first: nothing that can throw happens once the headers are out.+ const payload = typeof body === 'string' || Buffer.isBuffer(body) ? body : JSON.stringify(body)res.writeHead(status, { 'content-type': type, 'cache-control': 'no-store' })- res.end(typeof body === 'string' || Buffer.isBuffer(body) ? body : JSON.stringify(body))+ res.end(payload)}function serveFile(req, res, full) {@@ -102,42 +104,10 @@ function netlist(top) {}// netlistsvg runs in a worker so a slow layout never stalls the event stream.-const skinPath = join(here, 'node_modules', 'netlistsvg', 'lib', 'default.svg')-let skin = null, known = new Set()-let worker = null, jobs = new Map(), jobSeq = 0-function renderSvg(net) {- if (!skin) {- if (!existsSync(skinPath)) return Promise.reject(new Error('netlistsvg is not installed here — run toolchain/setup.sh'))- skin = readFileSync(skinPath, 'utf8')- known = skinTypes(skin)- }- if (!worker) {- worker = new Worker(join(here, 'viewer', 'lib', 'schematic-worker.mjs'))- worker.on('message', ({ id, svg, error }) => {- const job = jobs.get(id)- if (!job) return- jobs.delete(id)- clearTimeout(job.timer)- if (error || !svg) job.reject(new Error(error || 'netlistsvg drew nothing'))- else job.resolve(svg)- })- worker.on('error', (e) => { for (const j of jobs.values()) j.reject(e); jobs.clear(); worker = null })- worker.unref()- }- return new Promise((resolveJob, rejectJob) => {- const id = ++jobSeq- const timer = setTimeout(() => {- jobs.delete(id)- // A layout that takes this long will not finish usefully; start a fresh worker for the next.- worker?.terminate(); worker = null- for (const j of jobs.values()) j.reject(new Error('interrupted'))- jobs.clear()- rejectJob(new Error('this module is too large to lay out in the pane (over 90 s)'))- }, 90_000)- jobs.set(id, { resolve: resolveJob, reject: rejectJob, timer })- worker.postMessage({ id, skin, netlist: moduleForRender(net, net.__module, known) })- })-}+const renderSvg = createRenderer({+ skinPath: join(here, 'node_modules', 'netlistsvg', 'lib', 'default.svg'),+ workerPath: join(here, 'viewer', 'lib', 'schematic-worker.mjs'),+})// --------------------------------------------------------------------------------------- API@@ -224,7 +194,7 @@ async function api(req, res, url) {try {return send(res, 200, await job, 'image/svg+xml')} catch (e) {- return send(res, 500, String(e.message ?? e), 'text/plain')+ return send(res, 500, e.message, 'text/plain')}}@@ -259,9 +229,9 @@ async function api(req, res, url) {}createServer(async (req, res) => {- const url = new URL(req.url ?? '/', `http://127.0.0.1:${port}`)- const path = decodeURIComponent(url.pathname)+ const url = new URL(req.url, `http://127.0.0.1:${port}`)try {+ const path = decodeURIComponent(url.pathname) // throws on a malformed escape: a 500, not a crashif (path === '/' || path === '/index.html') return serveFile(req, res, join(publicDir, 'index.html'))if (path.startsWith('/app/')) return serveFile(req, res, inside(publicDir, path.slice(5)))if (path.startsWith('/api/')) return await api(req, res, url)@@ -277,8 +247,7 @@ createServer(async (req, res) => {return serveFile(req, res, inside(workspace, path.replace(/^\/+/, '')))} catch (error) {console.error('[yosys]', error)- if (!res.headersSent) send(res, 500, { error: String(error.message ?? error) })- else res.end()+ send(res, 500, { error: error.message })}}).listen(port, '127.0.0.1', () => console.log(`[yosys] listening on http://127.0.0.1:${port}/ (workspace: ${workspace})`))@@ -295,6 +264,7 @@ function flush() {}try {watch(workspace, { recursive: true }, (_event, name) => {+ /* c8 ignore next */ // fs.watch documents a null filename where the OS gives none; macOS and Linux always give oneconst n = String(name ?? '').split(sep).join('/')if (!n || n.includes('node_modules') || n.startsWith('.git/') || n.endsWith('.vvp') || n.startsWith('.claude')) returnpending.add(n)diff --git a/store/agents/yosys/viewer/lib/render.mjs b/store/agents/yosys/viewer/lib/render.mjsnew file mode 100644index 000000000..db5c11326--- /dev/null+++ b/store/agents/yosys/viewer/lib/render.mjs@@ -0,0 +1,54 @@+// The schematic renderer the server hands requests to: netlistsvg in a worker thread, so a slow+// layout never stalls the event stream. One worker at a time, jobs matched to replies by id, a+// layout that runs past the timeout abandoned (and its worker replaced) rather than waited on.+import { existsSync, readFileSync } from 'node:fs'+import { Worker as ThreadWorker } from 'node:worker_threads'+import { moduleForRender, skinTypes } from './netlist.mjs'++/**+ * @param {{skinPath: string, workerPath: string, timeoutMs?: number, Worker?: typeof ThreadWorker}} options+ * @returns {(net: object) => Promise<string>} draws `net.__module` of the netlist `net`+ */+export function createRenderer({ skinPath, workerPath, timeoutMs = 90_000, Worker = ThreadWorker }) {+ let skin = null, known = new Set()+ let worker = null+ const jobs = new Map()+ let jobSeq = 0+ // A failed job's timer goes with it: left running, it would fire later and kill the next worker.+ const failAll = (error) => {+ for (const j of jobs.values()) { clearTimeout(j.timer); j.reject(error) }+ jobs.clear()+ }+ return function renderSvg(net) {+ if (!skin) {+ if (!existsSync(skinPath)) return Promise.reject(new Error('netlistsvg is not installed here — run toolchain/setup.sh'))+ skin = readFileSync(skinPath, 'utf8')+ known = skinTypes(skin)+ }+ if (!worker) {+ worker = new Worker(workerPath)+ worker.on('message', ({ id, svg, error }) => {+ const job = jobs.get(id)+ if (!job) return+ jobs.delete(id)+ clearTimeout(job.timer)+ if (error || !svg) job.reject(new Error(error || 'netlistsvg drew nothing'))+ else job.resolve(svg)+ })+ worker.on('error', (e) => { failAll(e); worker = null })+ worker.unref()+ }+ return new Promise((resolveJob, rejectJob) => {+ const id = ++jobSeq+ const timer = setTimeout(() => {+ jobs.delete(id)+ // A layout that takes this long will not finish usefully; start a fresh worker for the next.+ worker.terminate(); worker = null+ failAll(new Error('interrupted'))+ rejectJob(new Error(`this module is too large to lay out in the pane (over ${Math.round(timeoutMs / 1000)} s)`))+ }, timeoutMs)+ jobs.set(id, { resolve: resolveJob, reject: rejectJob, timer })+ worker.postMessage({ id, skin, netlist: moduleForRender(net, net.__module, known) })+ })+ }+}diff --git a/store/agents/yosys/viewer/test/render.test.mjs b/store/agents/yosys/viewer/test/render.test.mjsnew file mode 100644index 000000000..3f5a2f523--- /dev/null+++ b/store/agents/yosys/viewer/test/render.test.mjs@@ -0,0 +1,135 @@+// The schematic renderer's job bookkeeping, on a fake worker: replies, failures, the timeout.+import { test } from 'node:test'+import assert from 'node:assert/strict'+import { EventEmitter } from 'node:events'+import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'+import { tmpdir } from 'node:os'+import { join } from 'node:path'+import { setTimeout as sleep } from 'node:timers/promises'+import { createRenderer } from '../lib/render.mjs'++const NET = {+ modules: {+ top: { attributes: {}, ports: {}, netnames: {}, cells: { a: { type: '$and' }, b: { type: '$xor' }, u: { type: 'sub' } } },+ sub: { attributes: {}, ports: {}, netnames: {}, cells: {} },+ },+ __module: 'top',+}++function fixture(t) {+ const dir = mkdtempSync(join(tmpdir(), 'yosys-render-'))+ t.after(() => rmSync(dir, { recursive: true, force: true }))+ const skinPath = join(dir, 'default.svg')+ writeFileSync(skinPath, '<svg><g s:type="$and"/><g s:alias val="$or"/></svg>')+ const made = []+ class FakeWorker extends EventEmitter {+ constructor(path) { super(); this.path = path; this.posted = []; this.terminated = false; made.push(this) }+ postMessage(message) { this.posted.push(message) }+ terminate() { this.terminated = true }+ unref() { this.unrefed = true }+ }+ return { dir, skinPath, made, FakeWorker }+}++/** Settles to ['ok', value] or ['error', message] — or stays 'pending'. */+function track(promise) {+ const box = { state: 'pending' }+ promise.then((v) => { box.state = ['ok', v] }, (e) => { box.state = ['error', e.message] })+ return box+}++test('without netlistsvg installed there is nothing to draw with, and no worker is started', async (t) => {+ const { dir, made, FakeWorker } = fixture(t)+ const render = createRenderer({ skinPath: join(dir, 'missing.svg'), workerPath: '/w.mjs', Worker: FakeWorker })+ await assert.rejects(render(NET), /netlistsvg is not installed here — run toolchain\/setup\.sh/)+ assert.equal(made.length, 0)+})++test('one worker draws every module; a reply settles its own job only', async (t) => {+ const { skinPath, made, FakeWorker } = fixture(t)+ const render = createRenderer({ skinPath, workerPath: '/w.mjs', Worker: FakeWorker })+ const first = render(NET)+ const second = track(render({ ...NET, __module: 'sub' }))+ assert.equal(made.length, 1)+ const [w] = made+ assert.equal(w.path, '/w.mjs')+ assert.equal(w.unrefed, true)+ const [m1, m2] = w.posted+ assert.deepEqual([m1.id, m2.id], [1, 2])+ assert.match(m1.skin, /s:type="\$and"/)+ // a type the skin draws keeps its $, an unknown internal cell loses it, an instance is named+ assert.deepEqual(Object.fromEntries(Object.entries(m1.netlist.modules.top.cells).map(([k, c]) => [k, c.type])),+ { a: '$and', b: 'xor', u: 'sub u' })+ assert.deepEqual(Object.keys(m2.netlist.modules), ['sub'])++ w.emit('message', { id: 99, svg: '<svg>stray</svg>' }) // nobody asked for this one+ w.emit('message', { id: 1, svg: '<svg>top</svg>' })+ assert.equal(await first, '<svg>top</svg>')+ await sleep(0)+ assert.equal(second.state, 'pending')+ w.emit('message', { id: 2, error: 'Cannot read properties of undefined' })+ await sleep(0)+ assert.deepEqual(second.state, ['error', 'Cannot read properties of undefined'])++ const empty = render(NET)+ w.emit('message', { id: 3 })+ await assert.rejects(empty, /netlistsvg drew nothing/)+ assert.equal(made.length, 1)+})++test('a worker that dies fails what it had, and the next request gets a new one', async (t) => {+ const { skinPath, made, FakeWorker } = fixture(t)+ const render = createRenderer({ skinPath, workerPath: '/w.mjs', Worker: FakeWorker, timeoutMs: 120 })+ const pending = [track(render(NET)), track(render(NET))]+ await sleep(60)+ made[0].emit('error', new Error('worker crashed'))+ await sleep(0)+ assert.deepEqual(pending.map((p) => p.state), [['error', 'worker crashed'], ['error', 'worker crashed']])+ const next = track(render(NET))+ assert.equal(made.length, 2)+ // past the dead jobs' deadline, not yet the new job's: their timers must not reach the new worker+ await sleep(90)+ assert.equal(made[1].terminated, false, 'a dead job timed out later and killed the new worker')+ assert.equal(next.state, 'pending')+ made[1].emit('message', { id: made[1].posted[0].id, svg: '<svg/>' })+ await sleep(0)+ assert.deepEqual(next.state, ['ok', '<svg/>'])+})++test('a layout past the timeout is abandoned: its worker goes, the jobs sharing it are interrupted', async (t) => {+ const { skinPath, made, FakeWorker } = fixture(t)+ const render = createRenderer({ skinPath, workerPath: '/w.mjs', Worker: FakeWorker, timeoutMs: 100 })+ let next+ const slow = track(render(NET).catch((e) => {+ next = track(render(NET)) // asked for again at once, as the page does+ throw e+ }))+ await sleep(50)+ const sharing = track(render(NET)) // due at 150 ms; the new job at 200+ await sleep(125)+ assert.deepEqual(slow.state, ['error', 'this module is too large to lay out in the pane (over 0 s)'])+ assert.deepEqual(sharing.state, ['error', 'interrupted'])+ assert.equal(made[0].terminated, true)+ assert.equal(made.length, 2)+ // a reply from the abandoned worker is nobody's+ made[0].emit('message', { id: made[0].posted[0].id, svg: '<svg>late</svg>' })+ assert.equal(made[1].terminated, false, 'an interrupted job timed out later and killed the new worker')+ assert.equal(next.state, 'pending')+ made[1].emit('message', { id: made[1].posted[0].id, svg: '<svg/>' })+ await sleep(0)+ assert.deepEqual(next.state, ['ok', '<svg/>'])+})++test('the default timeout reads as the pane says it', async (t) => {+ const { skinPath, FakeWorker } = fixture(t)+ const realSetTimeout = globalThis.setTimeout+ let delay, job+ globalThis.setTimeout = (fn, ms) => { delay = ms; return realSetTimeout(fn, 0) } // and run out at once+ try {+ job = createRenderer({ skinPath, workerPath: '/w.mjs', Worker: FakeWorker })(NET)+ } finally {+ globalThis.setTimeout = realSetTimeout+ }+ assert.equal(delay, 90_000)+ await assert.rejects(job, /this module is too large to lay out in the pane \(over 90 s\)/)+})